Copied to clipboard

Flag this post as spam?

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


  • Tom 713 posts 954 karma points
    Jun 30, 2011 @ 10:16
    Tom
    0

    Analytics Razor Macro

    Hi Guys,

    trying to implement a google analytics razor macro.. in my root node I have the properties googleTrackingEnabled as a true/false.. and googleTrackingAccountID..

    I'm having issues getting my script to load: Error loading Razor Script GoogleAnalytics.cshtml

    'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'googleTrackingEnabled'

    am I supposed to be checking for null even on the variable assignment?!

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{ var useAnalytics = @Model.AncestorsOrSelf().googleTrackingEnabled; }
    
    if(!string.IsNullOrEmpty(@useAnalytics.ToString()) && @useAnalytics) 
    {
      @{ var googleTrackingAccountID = @Model.AncestorsOrSelf().googleTrackingAccountID;  }
      @if(!string.IsNullOrEmpty(@googleTrackingAccountID.ToString()))
      {
        <script type="text/javascript">
        //<![CDATA[
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', '@googleTrackingAccountID']);
        _gaq.push(['_trackPageview']);
    
        (function () {
          var ga = document.createElement('script'); ga.type = 'text/javascript';
          ga.async = true;
          ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
        //]]>
        </script>
      }
    }
  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jun 30, 2011 @ 11:07
    Dirk De Grave
    2

    Hi Tom,

    AncestorsOrSelf() returns a DynamicNodeList, that's why you get the error, if your property is on the root node, you should use 

    @Model.AncestorOrSelf(1).googleTrackingEnabled;

    (Deleted the 's' from Ancestors)

    which look up the variable on the node specified on the level passed as parameter.

     

    Hope this helps.

    Regards,

    /Dirk

Please Sign in or register to post replies

Write your reply to:

Draft