universal-ga
Advanced tools
Comparing version 1.0.0-prerelease-1 to 1.0.0-prerelease-2
@@ -24,3 +24,3 @@ 'use strict'; | ||
args[0] = this._namespace + '.' + args[0]; | ||
delete this._namespace; | ||
this._namespace = null; | ||
} | ||
@@ -46,4 +46,7 @@ | ||
if(options && options.debug) { | ||
options = options || {}; | ||
if(options.debug) { | ||
src += 'analytics_debug.js'; | ||
delete options.debug; | ||
} else { | ||
@@ -68,3 +71,4 @@ src += 'analytics.js'; | ||
if(trackingID) { | ||
this.create(trackingID); | ||
options = JSON.stringify(options) === "{}" ? undefined : options; | ||
this.create(trackingID, options); | ||
} | ||
@@ -71,0 +75,0 @@ }, |
{ | ||
"name": "universal-ga", | ||
"version": "1.0.0-prerelease-1", | ||
"version": "1.0.0-prerelease-2", | ||
"description": "Universal Google Analytics module for node", | ||
"main": "lib/analytics.js", | ||
"scripts": { | ||
"test": "node_modules/.bin/mocha test" | ||
"test": "node_modules/.bin/mocha test", | ||
"coverage": "node_modules/.bin/istanbul cover --dir ./reports node_modules/.bin/_mocha test" | ||
}, | ||
@@ -28,2 +29,3 @@ "repository": { | ||
"grunt-contrib-jshint": "^0.11.3", | ||
"istanbul": "^0.3.22", | ||
"matchdep": "^1.0.0", | ||
@@ -30,0 +32,0 @@ "mocha": "^2.3.3", |
175
README.md
# universal-ga | ||
Universal Google Analytics module for node | ||
[![Build Status](https://travis-ci.org/daxko/universal-ga.svg?branch=master)](https://travis-ci.org/daxko/universal-ga) [![Coverage Status](https://coveralls.io/repos/daxko/universal-ga/badge.svg?branch=master)](https://coveralls.io/r/daxko/universal-ga?branch=master) | ||
A Universal Google Analytics module for node. | ||
Currently supported features: | ||
* Pageviews | ||
* Screenviews | ||
* Events | ||
* User timings | ||
* Exceptions | ||
* Custom Dimensions/Metrics | ||
# Install | ||
```bash | ||
$ npm install --save universal-ga | ||
``` | ||
# Getting Started | ||
To initialize `universal-ga`, you will need to first pass in your analytics tracking id. | ||
```js | ||
var analytics = require('universal-ga'); | ||
... | ||
analytics.initialize('UA-XXXXX-YYY'); | ||
``` | ||
## Documentation | ||
#### analytics.initialize( *trackingID*, *options* ) | ||
|Name|Description| | ||
|-----|-----| | ||
|trackingID| `string` Your analytics tracking id, i.e. `UA-XXXXX-YY`. | ||
|options.debug| `bool` (optional) If set to `true`, will use `analytics_debug.js` for some additional console logging. | ||
Before anything else will work, you must first initialize analytics by passing an initial tracking id. | ||
###### Example | ||
```js | ||
analytics.initialize('UA-12345-12'); | ||
``` | ||
#### analytics.create( *trackingID*, *name* ) | ||
|Name|Description| | ||
|-----|-----| | ||
|trackingID| `string` Another analytics tracking id, i.e. `UA-XXXXX-YY`. | ||
|name| `string` Namespace additional tracking ids. | ||
Allows you to track multiple tracking ids, used in combination with `.name()`. | ||
###### Example | ||
```js | ||
analytics.initialize('UA-12345-1'); | ||
analytics.create('UA-12345-2', 'anotherTracker'); | ||
... | ||
analytics.pageview('/home'); | ||
analytics.name('anotherTracker').pageview('/home'); | ||
``` | ||
This will namespace any additional values, allowing you to specify which values to send to which tracker. The above example would send the following data to analytics: | ||
```js | ||
['send', 'pageview', '/home'], | ||
['anotherTracker.send', 'pageview', 'home'] | ||
``` | ||
#### analytics.name( *name* ) | ||
|Name|Description| | ||
|-----|-----| | ||
|name| `string` Send next value for the namespaced tracking id. | ||
Namespaces the next value that is sent to the tracker. | ||
###### Example | ||
```js | ||
analytics.name('anotherTracker').pageView('/home'); | ||
analytics.name('anotherTracker').timing('load', 'page', 123); | ||
``` | ||
The above would send the following data to analytics: | ||
```js | ||
['anotherTracker.send', 'pageview', '/home'], | ||
['anotherTracker.send', 'timing', 'load', 'page', 123] | ||
``` | ||
#### analytics.set( *key*, *value* ) | ||
|Name|Description| | ||
|-----|-----| | ||
|key| `string` Key to send to analytics. | ||
|value| `string` Value for the key. | ||
Set key/value pairs for the tracker. | ||
###### Example | ||
```js | ||
analytics.set('page', '/about'); | ||
``` | ||
#### analytics.pageview( *pagename* , *options* ) | ||
|Name|Description| | ||
|-----|-----| | ||
|pagename| `string` Pagename to send to analytics. | ||
|options| `object` (optional) Additional options. | ||
Allows you to send a pageview to analytics. Additional options for the pageview can be seen in the [pages documentation](https://developers.google.com/analytics/devguides/collection/analyticsjs/pages). | ||
###### Example | ||
```js | ||
analytics.pageview('/about'); | ||
``` | ||
#### analytics.screenview( *screenname*, *options* ) | ||
|Name|Description| | ||
|-----|-----| | ||
|screenname| `string` Screenname to send to analytics. | ||
|options| `object` (optional) Additional options. | ||
Allows you to send a screenview to analytics. Additional options for the screenview can be seen in the [app screens documentation](https://developers.google.com/analytics/devguides/collection/analyticsjs/screens). | ||
###### Example | ||
```js | ||
analytics.screenview('/about'); | ||
``` | ||
#### analytics.event( *category*, *action*, *options* ) | ||
|Name|Description| | ||
|-----|-----| | ||
|category| `string` Event category. | ||
|action| `string` Event action. | ||
|options| `object` (optional) Additional options. | ||
#### analytics.timing( *category*, *var*, *value*, *options* ) | ||
|Name|Description| | ||
|-----|-----| | ||
|category| `string` Timing category. | ||
|var| `string` Timing variable. | ||
|value| `int` Timing value (in milliseconds). | ||
|options| `object` (optional) Additional options. | ||
#### analytics.exception( *message*, *isFatal* ) | ||
|Name|Description| | ||
|-----|-----| | ||
|message| `string` Exception message. | ||
|isFatal| `bool` Is fatal event. | ||
#### analytics.custom( *key*, *value* ) | ||
|Name|Description| | ||
|-----|-----| | ||
|key| `string` Custom dimension/metric key. | ||
|value| `string` Custom dimension/metric value. |
@@ -6,3 +6,4 @@ 'use strict'; | ||
, sinon = require('sinon') | ||
, analytics = require('../lib/analytics'); | ||
, analytics = require('../lib/analytics') | ||
, sandbox; | ||
@@ -17,7 +18,10 @@ describe('analytics', function() { | ||
beforeEach(function() { | ||
sinon.stub(console, 'warn'); | ||
sandbox = sinon.sandbox.create(); | ||
sandbox.stub(console, 'warn'); | ||
global.window = global; | ||
global.document = { | ||
createElement: () => ({}), | ||
getElementsByTagName: () => ([{ parentNode: { insertBefore: () => {} } }]) | ||
createElement: function() { return {}; }, | ||
getElementsByTagName: function() { | ||
return [{ parentNode: { insertBefore: function(){} }}]; | ||
} | ||
}; | ||
@@ -27,3 +31,3 @@ }); | ||
afterEach(function() { | ||
console.warn.restore(); | ||
sandbox.restore(); | ||
delete global.ga; | ||
@@ -43,3 +47,3 @@ delete global.window; | ||
it('should initialize analytics in debug mode', function() { | ||
var createElement = sinon.spy(global.document, 'createElement'); | ||
var createElement = sandbox.spy(global.document, 'createElement'); | ||
analytics.initialize({ debug: true }); | ||
@@ -50,3 +54,3 @@ assert.match(createElement.returnValues[0].src, /analytics_debug\.js$/i); | ||
it('should initialize analytics in production mode', function() { | ||
var createElement = sinon.spy(global.document, 'createElement'); | ||
var createElement = sandbox.spy(global.document, 'createElement'); | ||
analytics.initialize(); | ||
@@ -56,7 +60,20 @@ assert.match(createElement.returnValues[0].src, /analytics\.js$/i); | ||
it('should initialize analytics with tracking id', function() { | ||
it('should pass tracking id to create', function() { | ||
var create = sandbox.spy(analytics, 'create'); | ||
analytics.initialize('UA-XXXXX-Y'); | ||
assert.deepEqual(analyticsArgs().pop(), ['create', 'UA-XXXXX-Y', 'auto']); | ||
assert.isTrue(create.calledWith('UA-XXXXX-Y')); | ||
}); | ||
it('should pass tracking id to create with options', function() { | ||
var create = sandbox.spy(analytics, 'create'); | ||
analytics.initialize('UA-XXXXX-Y', { clientId: '32816aa5-9dab-4e9c-8f1b-0f53a1be5497' }); | ||
assert.isTrue(create.calledWith('UA-XXXXX-Y', { clientId: '32816aa5-9dab-4e9c-8f1b-0f53a1be5497' })); | ||
}); | ||
it('should pass tracking id to create with options excluding debug flag', function() { | ||
var create = sandbox.spy(analytics, 'create'); | ||
analytics.initialize('UA-XXXXX-Y', { clientId: '32816aa5-9dab-4e9c-8f1b-0f53a1be5497', debug: true }); | ||
assert.isTrue(create.calledWith('UA-XXXXX-Y', { clientId: '32816aa5-9dab-4e9c-8f1b-0f53a1be5497' })); | ||
}); | ||
}); | ||
@@ -63,0 +80,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
22667
390
176
7