Title: Additional tracking code and Universal Analytics
Last modified: August 21, 2016

---

# Additional tracking code and Universal Analytics

 *  [mchowla](https://wordpress.org/support/users/mchowla/)
 * (@mchowla)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/additional-tracking-code-and-universal-analytics/)
 * 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://wordpress.org/plugins/google-analyticator/](https://wordpress.org/plugins/google-analyticator/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Daniel J. Lewis](https://wordpress.org/support/users/djosephdesign/)
 * (@djosephdesign)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/additional-tracking-code-and-universal-analytics/#post-4902848)
 * I run into this same problem, like trying to add `ga('require', 'displayfeatures');`
   for interest-tracking.
 *  [skadu](https://wordpress.org/support/users/skadu/)
 * (@skadu)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/additional-tracking-code-and-universal-analytics/#post-4902861)
 * **UPDATE 2014-08-18**
    It looks like the beta version of this plugin available
   [here](http://www.analyticatorpro.com/beta/beta-6-4-7-4/) 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.

 * ![](https://s.w.org/plugins/geopattern-icon/google-analyticator_fefefe.svg)
 * [Analyticator](https://wordpress.org/plugins/google-analyticator/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/google-analyticator/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/google-analyticator/)
 * [Active Topics](https://wordpress.org/support/plugin/google-analyticator/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/google-analyticator/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/google-analyticator/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [skadu](https://wordpress.org/support/users/skadu/)
 * Last activity: [11 years, 9 months ago](https://wordpress.org/support/topic/additional-tracking-code-and-universal-analytics/#post-4902861)
 * Status: not resolved