Asynchronous tracking - Multiple Trackers Syntax

Hi

I’ve tried to search & create my own asynchronous tracking tag to include multiple trackers (we want to track sub-domains separately to our main domain).

Could anyone give an example of how to set up multiple trackers asynchronously?

I have tried to replicate the GA way like this:


    <script type="text/javascript">
        //Load Piwik.js file
        var pkBaseURL = (("https:" == document.location.protocol) ? "https://10.0.0.169/piwik/piwik/" : "http://10.0.0.169/piwik/piwik/" );
        document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E") );
    </script>

    <script type="text/javascript">
try {
            
            var thisUrl;
            thisUrl = document.URL;
            thisUrl = thisUrl.toLowerCase();
            
            var _paq = _paq || [];
            (function(u, d, t) {

                //intranet - first tracker
                if (thisUrl.indexOf("intranet.mycompany.com" ) > 0) {
                    _paq.push(['i.setSiteId', 1], ['i.setTrackerUrl', pkBaseURL + 'piwik.php'], ['i.trackPageView']);
                   }
                                        
                //Intelligence - New Tracker
                if (thisUrl.indexOf("intelligence.intranet.mycompany.com" ) > 0 ) {
                    _paq.push(['iz.setSiteId', 5], ['iz.setTrackerUrl', pkBaseURL + 'piwik.php'], ['iz.trackPageView']);
                }
              // + a few others...
                var g = d.createElement(t),
			     s = d.getElementsByTagName(t)[0];
                g.async = true;
                g.src = pkBaseURL + 'piwik.js';
                s.parentNode.insertBefore(g, s);
            })('//10.0.0.169/piwik/', document, 'script');
                          
        } catch (err) { }
 </script>

  • As you can guess, I’m trying to evaluate the document URL and get new trackers as necessary. Will this work? the syntax appears to be wrong somewhere as it isn’t working quite right.

Many thanks in adviance.

That won’t work.

The short answer is that the asynchronous method can’t be used in that way with multiple trackers because _paq is global and there’s only one instance (a singleton) of the asynchronous tracker object.

You can, however, push functions. Try this:


if (thisUrl.indexOf("intranet.mycompany.com" ) > 0) {
    _paq.push(function () {
        var tracker = Piwik.getTracker();
        tracker.setSiteId(1);
        tracker.setTrackerUrl(pkBaseUrl + 'piwik.php');
        tracker.trackPageView();
    }]);
}

thanks, thats solved it.

BTW, for anyone else you need to put the opening [ in:


if (thisUrl.indexOf("intranet.mycompany.com" ) > 0) {
    _paq.push([function () {
        var tracker = Piwik.getTracker();
        tracker.setSiteId(1);
        tracker.setTrackerUrl(pkBaseUrl + 'piwik.php');
        tracker.trackPageView();
       //......more trackers can go here
    }]);
}

Good catch. Sorry about the typo…I was responding from my iPhone.

this looks interesting to track subdomains.
As this is all new to me, what would be the full code ?

Would it be possible to place it here ?

Many thanks.

Hi,

I am trying to track goals for multiple trackers in piwik. Something like,

but how do i have to track goals for jusy piwik2 tracker. If I do this:
var piwik2 = piwik2 || [ ];
and call the function
piwik2.trackGoal(1);

it is throwing an error like this:

Uncaught TypeError: piwik3.trackGoal is not a function

Can anyone help me with a solution?

This is now documented here under section “Multiple Piwik trackers”:
https://developer.piwik.org/guides/tracking-javascript-guide