A tiny (323B) client-side module for tracking with Google Analytics
Switch branches/tags
Clone or download
Latest commit 048d5dd Oct 13, 2018
Permalink
Failed to load latest commit information.
src refactor: inline everything! Oct 13, 2018
test begin tests, laughable Jul 24, 2017
.editorconfig initial commit Jul 23, 2017
.gitignore chore: ignore built files Oct 13, 2018
.travis.yml add travis.yml Jul 24, 2017
builder.js chore: import custom builder Oct 13, 2018
license initial commit Jul 23, 2017
package.json 3.1.0 Oct 13, 2018
readme.md docs: update size Oct 13, 2018

readme.md

ganalytics Build Status

A tiny (323B) client-side module for tracking with Google Analytics

This module exposes three module definitions:

  • ES Module: dist/ganalytics.mjs
  • CommonJS: dist/ganalytics.js
  • UMD: dist/ganalytics.min.js

Please see Releases for changelog!

Install

$ npm install --save ganalytics

Usage

const GAnalytics = require('ganalytics');

const ga = new GAnalytics('UA-XXXXXXXX-X', { aid:1 });
// or
const ga = GAnalytics('UA-XXXXXXXX-X', { aid:1 });

ga.send('pageview');
ga.send('pageview', { dt:'Foobar', dp:'/foo' });

ga.send('event', { ec:'Video', ea:'Play', el:'Home Hero' });

API

GAnalytics(trackerID, options, toWait)

trackerID

Type: String

Your Google Analytics tracker ID; eg UA-XXXXXXXX-X

options.aip

Type: Integer
Default: 0

Anonymize the sender's IP address. See Anonymize IP.

options.an

Type: String

Specifies the application's name. See Application Name.

options.aid

Type: String

Specifies the application identifier. See Application ID.

options.aiid

Type: String

Specifies the application installer identifier. See Application Installer ID.

options.av

Type: String

Specifies the application verison. See Application Version.

options.ds

Type: String

Indicates the data source type of the hit; eg web or app. See Data Source.

toWait

Type: Boolean
Default: false

When truthy, a pageview event will not be sent immediately upon initialization.

ga.send(type, params)

type

Type: String

The type of hit to send. Must be one of these: pageview, screenview, event, transaction, item, social, exception, or timing.

params

Type: Object

The parameters to send based on the type of hit.

Please follow the links for each available parameter set:

For pageview hits only, if no params are provided, then the document.title and location.href values will be auto-filled. This allows you to send valid requests by writing:

ga.send('pageview');
// is the same as:
//=> ga.send('pageview', { dt:document.title, dl:location.href })

License

MIT © Luke Edwards