Copied to clipboard

Flag this post as spam?

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


  • Michael Jensen 29 posts 98 karma points
    Oct 28, 2015 @ 12:03
    Michael Jensen
    0

    Google analytics tracking script

    Hey,

    iam making a set og website i one umbraco installation all3 sites uses the same markup so i only need to do one Master page, as all 3 sites have thear own domain and therfor a trackin ID i like a way to input it from the backen end whit out the ned to edit the master.

    i have look i search but i can seam to finde eny treads about it.

    i wath way can i make it so XXXXXXXXXXX is input from content end.

    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
      ga('create', 'XXXXXXXXXXXX', 'auto');
      ga('send', 'pageview');
    
    </script>
    

    Michael

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Oct 28, 2015 @ 12:22
    Steve Morgan
    100

    Hi,

    If you create a textstring property at the homepage level (in this example called "ga Code"..

    Then you can just get the root (homepage) node from within the page and then get the GA Code from there. This is doing it in the master / base template (the @RenderBody calls the child).

    I'd usually put all logic in a Partial Macro View but hopefully it will give you the idea.

     @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = null;
        var rootNode = Model.Content.AncestorOrSelf(1);
        string gaCode = rootNode.GetPropertyValue<string>("gaCode");
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
    </head>
    <body>
        <h1>Hi!</h1>
        @RenderBody()
    </body>
    
    
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
      ga('create', '@gaCode', 'auto');
      ga('send', 'pageview');
    
    </script>
    
    
    </html>
    
  • Lars Gyrup Brink Nielsen 6 posts 76 karma points
    Nov 03, 2015 @ 20:16
    Lars Gyrup Brink Nielsen
    0

    Please do yourself a favor and make the whole script tag editable (including the <script> tags themselves). At some point an SEO will come and say: “I need to add the line ga('require', 'displayfeatures');. How do I do that?” or the Google Analytics boilerplate will change again, or a marketing campaign calls for a Facebook Pixel code block, etc.

    The basic boilerplate code is copy-pasted from Google Analytics anyways. Some SEOs wouldn't know how to extract the property identifier from the code to insert it into your CMS input field.

Please Sign in or register to post replies

Write your reply to:

Draft