• When universal analytics are enabled, the “Additional tracking code
    ( before tracker initialization )” is executed before the tracker object has been created. For Traditional, the tracker object is created and then the before code is run.

    I’m trying to set a page level dimension variable using Universal Analytics and can not do it because the ‘ga’ doesn’t exist when the before code is executed. The after won’t work since the page view has already been sent.

    Is this a bug or was ‘before’ intentionally put before the tracker is created for Universal?

    https://www.ads-software.com/plugins/google-analyticator/

Viewing 2 replies - 1 through 2 (of 2 total)
  • I run into this same problem, like trying to add ga('require', 'displayfeatures'); for interest-tracking.

    UPDATE 2014-08-18
    It looks like the beta version of this plugin available here fixes the issue identified by this post, as well several other bugs.

    This is certainly a bug with the plugin. By taking a look at the add_google_analytics function, I can see that the options for the “before code” is placed before the new Universal Analytics GA snippet is even placed on the page.

    It’s a pretty simple matter of moving the conditional that checks to see if you have put any text in the “before code” box below the line that creates the tracker, and above the line that sends the pageview (which is where this “before code” should actually reside).

    Now I know modifying a plugin is a terrible idea, however without the ability to provide patches, and since I have a desire to use a plugin and not just add the code to the head of my template, I will provide below a working chunk of code to fix this issue.

    Here is the original section, with the incorrect placement (starting at line 1170 from the most recent version of the plugin, version 6.4.7.3):

    # Add any tracking code before the trackPageview
        do_action('google_analyticator_extra_js_before');
        if ( '' != $extra )
                echo "	$extra\n";
    	?>
    	(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', '<?php echo $uid; ?>', 'auto');
    <?php if(get_option(key_ga_enhanced_link_attr) == ga_enabled): ?>
    	ga('require', 'linkid', 'linkid.js');
    <?php endif; ?>
    	ga('send', 'pageview');
    <?php if ($need_to_annon == '1' ): ?>
    	ga('set', 'anonymizeIp', true);
    <?php endif; ?>

    Here is the updated version, with the code placed in the correct spot:

    ?>
    	(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', '<?php echo $uid; ?>', 'auto');
    	<?php
    	# Add any tracking code before the trackPageview
        do_action('google_analyticator_extra_js_before');
        if ( '' != $extra ){
        	echo "$extra\n";
        }
    	?>
    <?php if(get_option(key_ga_enhanced_link_attr) == ga_enabled): ?>
    	ga('require', 'linkid', 'linkid.js');
    <?php endif; ?>
    	ga('send', 'pageview');
    <?php if ($need_to_annon == '1' ): ?>
    	ga('set', 'anonymizeIp', true);
    <?php endif; ?>

    Here is a diff file, if anyone is interested in patching the plugin:

    --- google-analyticator/google-analyticator.php	Mon Aug 18 10:21:44 2014
    +++ google-analyticator-new.php	Mon Aug 18 10:33:20 2014
    @@ -1167,17 +1167,20 @@ function add_google_analytics()
     	})();
     <?php
     }else{
    -	    # Add any tracking code before the trackPageview
    -    do_action('google_analyticator_extra_js_before');
    -    if ( '' != $extra )
    -            echo "	$extra\n";
    -	?>
    +?>
     	(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', '<?php echo $uid; ?>', 'auto');
    +	<?php
    +	# Add any tracking code before the trackPageview
    +    do_action('google_analyticator_extra_js_before');
    +    if ( '' != $extra ){
    +    	echo "$extra\n";
    +    }
    +	?>
     <?php if(get_option(key_ga_enhanced_link_attr) == ga_enabled): ?>
     	ga('require', 'linkid', 'linkid.js');
     <?php endif; ?>

    Hope this helps.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Additional tracking code and Universal Analytics’ is closed to new replies.