thomaswelton / requirejs-google-analytics

Simple google analytics loader.
MIT License
21 stars 6 forks source link

requirejs-google-analytics

Build Status Dependency Status DevDependency Status Bitdeli Badge

Asynchronously load Google Analytics using requirejs.

Setup

This module utilises requirejs module configuration. It requires the following JS to be added to the page

requirejs.config({
    config: {
        'GA': {
            'id' : 'ACCOUNT_ID'
        }
    },
    paths: {
        EventEmitter: 'bower_components/event-emitter/dist/EventEmitter'
        GA: 'bower_components/requirejs-google-analytics/dist/GoogleAnalytics'
    }
});

Installation

Installation via bower

{
  "dependencies": {
    "requirejs-google-analytics": "~0.1.10"
  }
}

Usage

GA.ready(cb)

require(['GA'], function (GA) {
    GA.ready(function (ga) {
        // GA is fully loaded
        console.log(ga);
    });
});

GA.view(override)

Page Tracking

GA.event(category, action, label, value, fields)

Event Tracking

GA.social(network, action, target, fields)

Social Interactions

GA.timing(category, action, label, value, fields)

User Timings

Ecommerce

GA.ecomTran(data)

Adding a Transaction

GA.ecomItem(data)

Adding Items

GA.ecomSend()

Sending Data

GA.ecomClear()

Clearing Data

Advanced Usage

GA.newTracker(config)

Multiple Trackers

GA.set(field, value)

Fields

Create Only Fields

requirejs.config({
    config: {
        'GA': {
            'id' : 'ACCOUNT_ID',
            'fields': {
                // Create only fields go here:
                name: 'myTracker'
            }
        }
    },
    paths: {
        EventEmitter: 'bower_components/event-emitter/dist/EventEmitter'
        GA: 'bower_components/requirejs-google-analytics/dist/GoogleAnalytics'
    }
});

Experiments

requirejs.config({
    config: {
        'GA': {
            'id' : 'ACCOUNT_ID',
            // Experiment ID and Var go here:
            'expId' : $expermentId,
            'expVar' : $expermentVar,
        }
    },
    paths: {
        EventEmitter: 'bower_components/event-emitter/dist/EventEmitter'
        GA: 'bower_components/requirejs-google-analytics/dist/GoogleAnalytics'
    }
});