Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom Jones 19 posts 150 karma points
    Jun 02, 2023 @ 14:18
    Tom Jones
    0

    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

    Composition

    Document Type

    Document Type

    Generated HTML

    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.

    Any idea how best to approach this?

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Jun 03, 2023 @ 10:11
    Marc Goodson
    1

    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

     @(Model.Value<string>("gAMeasurementID",fallback: Fallback.ToAncestors))
    

    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

  • Tom Jones 19 posts 150 karma points
    Jun 04, 2023 @ 19:51
    Tom Jones
    0

    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?

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Jun 04, 2023 @ 19:58
    Marc Goodson
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft