Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cross domain tracking to Tracker object #185

Merged
merged 2 commits into from
May 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,15 @@ Pull `print-intent.js` into your project, and initialise it after analytics (see
## Error tracking

Pull `error-tracking.js` into your project, and initialise it after analytics (see [Create an analytics tracker, above](#create-an-analytics-tracker)), to track JavaScript errors.

## Tracking across domains

Once a Tracker instance has been created, tracking across domains can be set up
for pages like:

```js
GOVUK.analytics.addLinkedTrackerDomain(trackerIdHere, nameForTracker, domainToLinkTo);
```

Once this is done hits to that page will be tracked in both your local and the
named tracker, and sessions will persist to the other domain.
23 changes: 23 additions & 0 deletions javascripts/govuk/analytics/google-analytics-universal-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@
});
};

/*
https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain
trackerId - the UA account code to track the domain against
name - name for the tracker
domain - the domain to track
*/
GoogleAnalyticsUniversalTracker.prototype.addLinkedTrackerDomain = function(trackerId, name, domain) {
sendToGa('create',
trackerId,
'auto',
{'name': name});
// Load the plugin.
sendToGa('require', 'linker');
sendToGa(name + '.require', 'linker');

// Define which domains to autoLink.
sendToGa('linker:autoLink', [domain]);
sendToGa(name + '.linker:autoLink', [domain]);

sendToGa(name + '.set', 'anonymizeIp', true);
sendToGa(name + '.send', 'pageview');
};

// https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
GoogleAnalyticsUniversalTracker.prototype.setDimension = function(index, value) {
sendToGa('set', 'dimension' + index, String(value));
Expand Down
7 changes: 7 additions & 0 deletions javascripts/govuk/analytics/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,12 @@
this.classic.setCustomVariable(index, value, name, scope);
};

/*
Add a beacon to track a page in another GA account on another domain.
*/
Tracker.prototype.addLinkedTrackerDomain = function(trackerId, name, domain) {
this.universal.addLinkedTrackerDomain(trackerId, name, domain);
};

GOVUK.Tracker = Tracker;
})();
21 changes: 21 additions & 0 deletions spec/unit/analytics/TrackerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,25 @@ describe("GOVUK.Tracker", function() {
});
});

describe('when adding a linked domain', function() {
beforeEach(function() {
tracker = new GOVUK.Tracker(this.config);
});

it('adds a linked domain to universal only', function() {
window._gaq = [];
tracker.addLinkedTrackerDomain('1234', 'test', 'www.example.com');

expect(window._gaq).toEqual([]);
var allArgs = window.ga.calls.allArgs()
expect(allArgs).toContain(['create', '1234', 'auto', {'name': 'test'}]);
expect(allArgs).toContain(['require', 'linker']);
expect(allArgs).toContain(['test.require', 'linker']);
expect(allArgs).toContain(['linker:autoLink', ['www.example.com']]);
expect(allArgs).toContain(['test.linker:autoLink', ['www.example.com']]);
expect(allArgs).toContain(['test.set', 'anonymizeIp', true]);
expect(allArgs).toContain(['test.send', 'pageview']);
});
});

});