Hi folks, I've added the standard GA script snippets to the master page of my website. The snippet is hard-coded with the exception of the ID, which I want to be admin editable.
At the moment I have a TrackingProperties composition, which contains a field accepting the GA tracking ID. I can attach this composition to any applicable document types (e.g. "Home"), populate the ID field, and the tag generates as expected.
Composition
Document Type
Generated HTML
The issue here is that I want it to only be available as a Master page property, rather than each individual child page.
I tried creating a Master page document type and attaching the TrackingProperties composition to that instead, but this causes all of the child pages to throw runtime errors because they don't also implement ITrackingProperties.
So if you are on a page like, I don't know, About Us, there will be no gAMeasurementId property on that page type, so it will look to the page above, and the page above that, until finally it looks at the Homepage and discovers the property and can write out the value!
Hi Marc, I can't check yet but this seems like a workable solution, so thanks for your reply! My main issue however, if I've understood correctly, is that I would need to include this variable/ancestor reference on every single child view, and remember to add them on all future ones?
Admin editable Google Analytics snippet ID
Hi folks, I've added the standard GA script snippets to the master page of my website. The snippet is hard-coded with the exception of the ID, which I want to be admin editable.
At the moment I have a
TrackingProperties
composition, which contains a field accepting the GA tracking ID. I can attach this composition to any applicable document types (e.g. "Home"), populate the ID field, and the tag generates as expected.Composition
Document Type
Generated HTML
The issue here is that I want it to only be available as a Master page property, rather than each individual child page.
I tried creating a Master page document type and attaching the
TrackingProperties
composition to that instead, but this causes all of the child pages to throw runtime errors because they don't also implementITrackingProperties
.Any idea how best to approach this?
Hi Tom
If you only add it to your HomePage, then you can use the 'fallback to ancestors' technique of reading the value from the other page types...
eg
So if you are on a page like, I don't know, About Us, there will be no gAMeasurementId property on that page type, so it will look to the page above, and the page above that, until finally it looks at the Homepage and discovers the property and can write out the value!
regards
Marc
Hi Marc, I can't check yet but this seems like a workable solution, so thanks for your reply! My main issue however, if I've understood correctly, is that I would need to include this variable/ancestor reference on every single child view, and remember to add them on all future ones?
Hi Tom
Presumably you have some kind of master.cshtml view that all your views inherit the layout from and which contains your header and footer html.
I imagine your script tag where you want to insert the if from umbraco lives there?
So hopefully just one place to wire it up once!
Regards
Marc
is working on a reply...