Socket
Socket
Sign inDemoInstall

universal-ga

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    universal-ga

Universal Google Analytics module for node


Version published
Weekly downloads
250
decreased by-2.34%
Maintainers
1
Install size
22.6 kB
Created
Weekly downloads
 

Readme

Source

universal-ga

Build Status Coverage Status npm

A Universal Google Analytics module for node and the browser. You can either include this referenced via a script tag globally, or use a bundling process such as webpack or browserify. Once universal-ga has been initialized, you can any of the tracking methods to send your analytics data to Google.

Currently supported features:

  • Plugins
  • Pageviews
  • Screenviews
  • Events
  • User timings
  • Exceptions
  • Custom Dimensions/Metrics
  • Ecommerce Tracking

Install

$ npm install --save universal-ga

Getting Started

To initialize universal-ga, you will need to first pass in your analytics tracking id.

browserify/webpack
var analytics = require('universal-ga');
...
analytics.initialize('UA-XXXXX-YYY');
Global script (browser)
<script src="analytics.js"></script>
...
<script>analytics.initialize('UA-XXXXX-YYY');</script>

Initialization is dependant upon window being available at the time you call it, so be sure you call this once you can attach to window and before you call any of the other tracking methods.

Documentation

analytics.initialize( trackingID, options )
NameDescription
trackingIDstring Your analytics tracking id, i.e. UA-XXXXX-YY.
options.debugbool (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
analytics.initialize('UA-12345-12');

Additional options can also be sent to analytics.create by including them as additional properties on the options object.

Example
analytics.initialize('UA-12345-12', { storage: 'none' });
analytics.create( trackingID, options )
NameDescription
trackingIDstring Another analytics tracking id, i.e. UA-XXXXX-YY.
optionsobject or string (optional) additional options to pass to the tracker.

Allows you to create multiple tracking ids. If you just need to add an additional tracking id, options can just be the name of your additional tracker. This can be used in combination with the .name() method.

Example
analytics.initialize('UA-12345-1');
analytics.create('UA-12345-2', 'anotherTracker');
...
analytics.pageview('/home');
analytics.name('anotherTracker').pageview('/home');

However, if you need to combine additional options with a name, you will need to name your tracker as part of the options object.

Example
analytics.initialize('UA-12345-1');
analytics.create('UA-12345-2', {
  name: 'anotherTracker',
  clientId: generateUUID()
});

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:

['send', 'pageview', '/home'],
['anotherTracker.send', 'pageview', 'home']
analytics.name( name )
NameDescription
namestring Send next value for the namespaced tracking id.

Namespaces the next set of values sent to analytics.

Example
analytics
  .name('anotherTracker')
  .pageView('/home')
  .timing('load', 'page', 123);

The above would send the following data to analytics:

['anotherTracker.send', 'pageview', '/home'],
['anotherTracker.send', 'timing', 'load', 'page', 123]
analytics.set( key, value )
NameDescription
keystring Key to send to analytics.
valuestring Value for the key.

Set key/value pairs for analytics.

Example
analytics.set('page', '/about');
analytics.plugin( name , options )
NameDescription
namestring Plugin to require.
optionsobject (optional) Additional options.

Allows you to add plugins to analytics. Additional options for plugins can be seen in the plugins documentation.

Example
analytics.plugin('foo');
analytics.pageview( pagename , options )
NameDescription
pagenamestring Pagename to send to analytics.
optionsobject (optional) Additional options.

Allows you to send a pageview to analytics. Additional options for the pageview can be seen in the pages documentation.

Example
analytics.pageview('/about');
analytics.screenview( screenname, options )
NameDescription
screennamestring Screenname to send to analytics.
optionsobject (optional) Additional options.

Send a screenview to analytics. Additional options for the screenview can be seen in the app screens documentation.

Example
analytics.screenview('/about');
analytics.event( category, action, options )
NameDescription
categorystring Event category.
actionstring Event action.
optionsobject (optional) Additional options.

Send event data and event metrics to analytics. Additional options for events can be seen in the event tracking documentation.

Example
analytics.event('category', 'action', { eventLabel: 'label' });
analytics.timing( category, var, value, options )
NameDescription
categorystring Timing category.
varstring Timing variable.
valueint Timing value (in milliseconds).
optionsobject (optional) Additional options.

Send timing data to analytics. Additional options for timing can be seen in the user timing documentation.

Example
analytics.timing('load', 'DOMContentLoaded', 123);
analytics.exception( message, isFatal )
NameDescription
messagestring Exception message.
isFatalbool (optional) Is fatal event.

Send exception data to analytics. You can specify whether or not the event was fatal with the optional boolean flag.

analytics.custom( key, value )
NameDescription
keystring Custom dimension/metric key.
valuestring Custom dimension/metric value.

Send custom dimension/metrics to analytics. You need to first configure caustom dimensions/metrics through the analytics management interface. This allows you to specify a metric/dimension index to track custom values. See custom dimensions/metrics documentation for more details.

Dimensions and metrics keys will be a key of dimension[0-9] or metric[0-9].

Example
analytics
  .custom('dimension01', 1234)
  .custom('metric04', 'my custom dimension');

You can additional combine custom metrics/dimensions with other analytics properties (such as events or pageviews) as additional data.

analytics
  .event('category', 'action', { metric05: 'custom metric data' })
  .pageview('/index', { dimension02: 'custom dimension data' });
analytics.ecAddTransaction( transaction )
NameDescription
transactionobject a transaction.

An ecommerce transaction represents the entire transaction that occurs on your site. Seeecommerce documentation for more details. A transaction object can consist of the following objects:

NameTypeDescription
idtextThe transaction ID. (e.g. 1234).
affiliationtext(optional) The store or affiliation from which this transaction occurred (e.g. Acme Clothing).
revenuecurrency(optional) No Specifies the total revenue or grand total associated with the transaction (e.g. 11.99). This value may include shipping, tax costs, or other adjustments to total revenue that you want to include as part of your revenue calculations.
shippingcurrency(optional) No Specifies the total shipping cost of the transaction. (e.g. 5).
taxcurrency(optional) No Specifies the total tax of the transaction. (e.g. 1.29).
Example
let transaction = {
    'id': '1234',                     // Transaction ID. Required.
    'affiliation': 'Acme Clothing',   // Affiliation or store name.
    'revenue': '11.99',               // Grand Total.
    'shipping': '5',                  // Shipping.
    'tax': '1.29'                     // Tax.
};

analytics.ecAddTransaction(transaction);
analytics.ecAddItem( productItem )
NameDescription
productItemobject individual product.

An item represents the individual products that were in the shopping cart, and contains the following values::

NameTypeDescription
idtextThe transaction ID. This ID is what links items to the transactions to which they belong. (e.g. 1234).
nametextThe item name. (e.g. Fluffy Pink Bunnies).
skutext(optional) Specifies the SKU or item code. (e.g. SKU47).
categorytext(optional) The category to which the item belongs (e.g. Party Toys).
pricetext(optional) The individual, unit, price for each item. (e.g. 11.99).
quantityinteger(optional) The number of units purchased in the transaction. If a non-integer value is passed into this field (e.g. 1.5), it will be rounded to the closest integer value.
Example
let productItem = {
    'id': '1234',                     // Transaction ID. Required.
    'name': 'Fluffy Pink Bunnies',    // Product name. Required.
    'sku': 'DD23444',                 // SKU/code.
    'category': 'Party Toys',         // Category or variation.
    'price': '11.99',                 // Unit price.
    'quantity': '1'                   // Quantity.
};

analytics.ecAddItem(productItem);
analytics.ecSend()

Finally, once you have configured all your ecommerce data in the shopping cart, you send the data to Google Analytics using the ecSend command.

analytics.ecClear()

If you need to manually clear the shopping cart of all transactions and items, you use this command.

Keywords

FAQs

Last updated on 17 Oct 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc