npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

universal-ga

v1.2.0

Published

Universal Google Analytics module for node

Downloads

1,063

Readme

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 )

|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
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 )

|Name|Description| |-----|-----| |trackingID| string Another analytics tracking id, i.e. UA-XXXXX-YY. |options| object 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 )

|Name|Description| |-----|-----| |name| string 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 )

|Name|Description| |-----|-----| |key| string Key to send to analytics. |value| string Value for the key.

Set key/value pairs for analytics.

Example
analytics.set('page', '/about');

analytics.plugin( name , options )

|Name|Description| |-----|-----| |name| string Plugin to require. |options| object (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 )

|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.

Example
analytics.pageview('/about');

analytics.screenview( screenname, options )

|Name|Description| |-----|-----| |screenname| string Screenname to send to analytics. |options| object (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 )

|Name|Description| |-----|-----| |category| string Event category. |action| string Event action. |options| object (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 )

|Name|Description| |-----|-----| |category| string Timing category. |var| string Timing variable. |value| int Timing value (in milliseconds). |options| object (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 )

|Name|Description| |-----|-----| |message| string Exception message. |isFatal| bool (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 )

|Name|Description| |-----|-----| |key| string Custom dimension/metric key. |value| string 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 )

|Name|Description| |-----|-----| |transaction| object 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:

|Name|Type|Description| |-----|-----|-----| |id| text| The transaction ID. (e.g. 1234). |affiliation| text| (optional) The store or affiliation from which this transaction occurred (e.g. Acme Clothing). |revenue| currency| (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. |shipping| currency| (optional) No Specifies the total shipping cost of the transaction. (e.g. 5). |tax| currency| (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 )

|Name|Description| |-----|-----| |productItem| object individual product.

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

|Name|Type|Description| |-----|-----|-----| |id| text| The transaction ID. This ID is what links items to the transactions to which they belong. (e.g. 1234). |name| text| The item name. (e.g. Fluffy Pink Bunnies). |sku| text| (optional) Specifies the SKU or item code. (e.g. SKU47). |category| text| (optional) The category to which the item belongs (e.g. Party Toys). |price| text| (optional) The individual, unit, price for each item. (e.g. 11.99). |quantity| integer| (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.