Skip to content

Commit

Permalink
Avoid example duplication
Browse files Browse the repository at this point in the history
Keep a canonical example that can be copy and pasted within the
analytics documentation.

* Include link to docs from within tracker.js
* Remove old example-init.js
  • Loading branch information
fofr committed Mar 10, 2015
1 parent c4054db commit bff50a4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 53 deletions.
62 changes: 33 additions & 29 deletions docs/analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,42 @@ The toolkit provides an abstraction around analytics to make tracking pageviews,
## Create an analytics tracker

The minimum you need to use the analytics function is:
1. Include the following files from /javascripts/govuk/analytics in your project:
1. google-analytics-classic-tracker.js
2. google-analytics-universal-tracker.js
3. tracker.js
2. Copy the file example-init/example-init.js from the same folder to your own project and replace the dummy IDs with your own (they begin with `UA-`)

Load and create the analytics tracker at the earliest opportunity so that:
* the time until the first pageview is tracked is kept small and pageviews aren’t missed
* javascript that depends on `GOVUK.analytics` runs after the tracker has been created

An excerpt from the [`example-init.js`](/javascripts/govuk/analytics/example-init/example-init.js) file:
1. Include the following files from /javascripts/govuk/analytics in your project:
* google-analytics-classic-tracker.js
* google-analytics-universal-tracker.js
* tracker.js
2. Copy the following `init` script into your own project and replace the dummy IDs with your own (they begin with `UA-`).
* This initialisation can occur immediately as this API has no dependencies.
* Load and create the analytics tracker at the earliest opportunity so that:
* the time until the first pageview is tracked is kept small and pageviews aren’t missed
* javascript that depends on `GOVUK.analytics` runs after the tracker has been created

```js
// Load Google Analytics libraries
GOVUK.Tracker.load();

// Use document.domain in dev, preview and staging so that tracking works
// Otherwise explicitly set the domain as www.gov.uk (and not gov.uk).
var cookieDomain = (document.domain === 'www.gov.uk') ? '.www.gov.uk' : document.domain;

// Configure profiles, setup custom vars, track initial pageview
GOVUK.analytics = new GOVUK.Tracker({
universalId: 'UA-XXXXXXXX-X',
classicId: 'UA-XXXXXXXX-X',
cookieDomain: cookieDomain
});

// Set custom dimensions before tracking pageviews
// GOVUK.analytics.setDimension(…)

// Track initial pageview
GOVUK.analytics.trackPageview();
(function() {
"use strict";

// Load Google Analytics libraries
GOVUK.Tracker.load();

// Use document.domain in dev, preview and staging so that tracking works
// Otherwise explicitly set the domain as www.gov.uk (and not gov.uk).
var cookieDomain = (document.domain === 'www.gov.uk') ? '.www.gov.uk' : document.domain;

// Configure profiles and make interface public
// for custom dimensions, virtual pageviews and events
GOVUK.analytics = new GOVUK.Tracker({
universalId: 'UA-XXXXXXXX-X',
classicId: 'UA-XXXXXXXX-X',
cookieDomain: cookieDomain
});

// Set custom dimensions before tracking pageviews
// GOVUK.analytics.setDimension(…)

// Track initial pageview
GOVUK.analytics.trackPageview();
})();
```

Once instantiated, the `GOVUK.analytics` object can be used to track virtual pageviews, custom events and custom dimensions.
Expand Down
24 changes: 0 additions & 24 deletions javascripts/govuk/analytics/example-init/example-init.js

This file was deleted.

3 changes: 3 additions & 0 deletions javascripts/govuk/analytics/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"use strict";
window.GOVUK = window.GOVUK || {};

// For usage and initialisation see:
// https://github.com/alphagov/govuk_frontend_toolkit/blob/master/docs/analytics.md#create-an-analytics-tracker

var Tracker = function(config) {
this.universal = new GOVUK.GoogleAnalyticsUniversalTracker(config.universalId, config.cookieDomain);
this.classic = new GOVUK.GoogleAnalyticsClassicTracker(config.classicId, config.cookieDomain);
Expand Down

0 comments on commit bff50a4

Please sign in to comment.