• Resolved ryuui

    (@ryuui)


    Greetings,

    quick question.
    Is it possible to add multiple named tracker instances through the plugin?
    I’ve been browsing through the documentation, but have not found any mention of it.

    Just to clarify, i’m trying to make something like this.

    
    <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','https://www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-XXXXXXXX-XX', 'auto');
      ga('create', 'UA-XXXXXXXX-XX', 'auto', 'other');
      ga('send', 'pageview');
      ga('other.send', 'pageview');
    </script>
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,

    On GADWP v5.x that’s possible, here’s a Gist example on how to manipulate the tracking code.

    Anyway, in your case I would just add the second tracking code, inline, separately, using a standard WordPress hook. There’s no requirement for the commands belonging to two different trackers to be intercalated like that. You could have it like this:

    <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','https://www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-XXXXXXXX-XX', 'auto');
      ga('send', 'pageview');  
    </script>
    <!--Nothing or any other code snippet in between-->
    <script>
    ga('create', 'UA-XXXXXXXX-XX', 'auto', 'other');
    ga('other.send', 'pageview');
    </script>
    Thread Starter ryuui

    (@ryuui)

    Awesome,

    thank you for both the quick reply and the very helpful answer.
    I have taken a look at the Gist example and modified it accordingly.
    The end result is exactly what I wanted.

    /**
     * Analytics
     */
    function gadwp_addcode($gadwp) {
      $commands = $gadwp->get(); // Get commands array
    
      $fields = array();
      $fields['trackingId'] = 'UA-XXXXXXXX-XX';
      $fields['cookieDomain'] = 'auto';
      $fields['name'] = 'other';
      $commands[] = $gadwp->prepare( 'create', $fields ); // Add a new line to the commands array
    
      $fields = array();
      $fields['hitType'] = 'pageview';
      $commands[] = $gadwp->prepare( 'other.send', $fields ); // Add a new line to the commands array
    
      $gadwp->set($commands); // Store the new commands array
    }
    add_action( 'gadwp_analytics_commands',  'gadwp_addcode', 10, 1 );
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Multiple named tracker instances’ is closed to new replies.