Search code examples
google-analyticsanalytics

_trackEvent on two accounts/subdomains, only one tracks properly


I want to track blog.domain.com on it's separate Google Analytics account, as well as the the main account for www.domain.com.

GA snippet in head on blog.domain.com:

var request_uri = '/blog/article0001/';
_gaq.push(
    // blog.domain.com
    ['_setAccount', 'UA-99999990-1'],
    ['_trackPageview'],
    // www.domain.com
    ['maintracker._setAccount', 'UA-99999991-1'],
    ['maintracker._setDomainName', '.domain.com'],
    ['maintracker._trackPageview', '/blog' + request_uri]
);

When a blog comment is posted using Ajax:

_gaq.push(
    // blog.domain.com
    ['_trackPageview', request_uri + 'commented/'],
    ['_trackEvent', 'Comment', 'Added comment',
    'Article title', ga_event_value],
    // www.domain.com
    ['maintracker._trackPageview', '/blog' + request_uri + 'commented/'],
    ['maintracker._trackEvent', 'Blog - Comment', 'Added comment',
    'Article title', ga_event_value]
);

All the four _trackPageView works fine. _trackEvent works fine for blog.domain.com, but not for maintracker.

GA reports "123 of your visits sent events", but no events actually shows in the statistics. I've waited 48 hours since the event fired.

Google Analytics Debugger for Chrome reports _gaq.push processing : "[maintracker._trackEvent,Blog - Comment,Added comment,Article title,2]" and Tracking beacon sent!.

The tracking code on the main site www.domain.com looks like this:

_gaq.push(
    ['_setAccount', 'UA-99999991-1'],
    ['_setDomainName', '.domain.com'],
    ['_trackPageview'],
);

Solution

  • After some more testing, it turns out that multiple trackers can indeed be combined with cross-subdomain tracking.

    The problem was not the tracking code, but a filter with a typo on the www.domain.com account.

    So I can confirm that the following actually works:

    www.domain.com:

    _gaq.push(
        // www.domain.com
        ['_setAccount', 'UA-99999991-1'],
        ['_setDomainName', '.domain.com'],
        ['_trackPageview'],
    );
    

    blog.domain.com:

    var request_uri = '/blog/article0001/';
    _gaq.push(
        ['_setAccount', 'UA-99999990-1'],
        ['_setDomainName', '.blog.domain.com'], // Not required, but gives overview
        ['_trackPageview'],
        // www.domain.com
        ['maintracker._setAccount', 'UA-99999991-1'],
        ['maintracker._setDomainName', '.domain.com'],
        ['maintracker._trackPageview', '/blog' + request_uri]
    );
    

    Analytics seems to grab all utm cookies, choose the correct cookie for each tracker (based on the hash at the start of all cookie values), and then write correctly to the cookie on the proper domain. Nice!