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

sparga

v0.2.4

Published

A convenience library for SPAs that use Google Analytics.

Downloads

18

Readme

npm travis Dependency Status

sparga

Single-Page Application Relay for Google Analytics

Are you a Spargan?

You can be if you need to relay hits from your single-page application to Google Analytics.

Index

Installation

npm install --save sparga

Examples

Quick Start

// for ES6 clients
import Sparga from 'sparga'
// for non-ES6 clients
const Sparga = require('sparga').default

const sparga = new Sparga()

sparga.init('UA-123456-7')

//
// Now all page views in your SPA will
// automatically be tracked in GA.
//
// Additionally, all unhandled JavaScript
// exceptions will also be tracked.
//
// NOTE: Automatic tracking of page views
// and JavaScript exceptions can be turned
// off by setting respective flags to false
// on the options object. See the API docs
// for specifics.
//

Automatic click-stream handling


const sparga = new Sparga()

sparga.init({
   gaSettings: 'UA-123456-7',
   autoCaptureClickEvents: true
})

//
// Now all mouse clicks in your SPA will
// automatically be tracked in GA.
//

Setting custom dimensions using developer friendly logical names

For more info, refer to the GA Field Reference


const sparga = new Sparga()

sparga.init({
   gaSettings: 'UA-123456-7',
   //
   // your dimension map will likely be governed
   // by a central team as GA only allows a limited
   // number of custom dimensions, depending on your
   // account type...as such, this map is usually
   // defined once and used many times for an organization
   //
   dimensionMap: {
      variantTestName: 'dimension1',
      variantTestSampling: 'dimension9',
      userRole: 'dimension17'
   }
})

sparga.setDimension('variantTestName', 'Variant Testing for Our Exciting New Feature')
sparga.setDimension('variantTestSampling', 'Customers in US having more than 100 users.')
sparga.setDimension('userRole', 'non-admin')

Setting custom metrics using developer friendly logical names

For more info, refer to the GA Field Reference


const sparga = new Sparga()

sparga.init({
   gaSettings: 'UA-123456-7',
   //
   // your metric map will likely be governed
   // by a central team as GA only allows a limited
   // number of custom metrics, depending on your
   // account type...as such, this map is usually
   // defined once and used many times for an organization
   //
   metricMap: {
      isNewUser: 'metric6',
      isUserAdmin: 'metric8',
      isViewSetAsPersistent: 'metric14'
   }
})

sparga.setMetric('isNewUser', 1)
sparga.setMetric('isUserAdmin', 0)
sparga.setMetric('isViewSetAsPersistent', 1)

Using multiple tracking IDs (i.e. multiple named trackers)

Sometimes a site/page needs to report to multiple tracking IDs, which is only possible if named trackers are used.


const sparga = new Sparga()

// NOTE: If a trackerMap is provided, then the
// gaSetting.trackingId will be ignored.

sparga.init({
   gaSettings: {
      trackingId: 'THIS WILL NOW BE IGNORED!'
   },
   trackerMap: {
      myTracker1: 'UA-123456-7',
      myTracker2: 'UA-123456-8',
      myTracker3: 'UA-123456-9'
   }
})

//
// Now all of the above tracking IDs will be sent pageview
// and exception events.
//
// Additionally, all of the Sparga helper methods will also
// use the above tracking IDs--or a subset of them, if specified
//

// send a custom event to ALL trackers (i.e. all tracking IDs)
sparga.sendEvent(
   'MyCategory-1', 'MyAction-1', 'MyLabel-1', 'MyValue-1'
)

// send a custom event to only TWO of the three trackers
sparga.sendEvent(
   'MyCategory-2', 'MyAction-2', 'MyLabel-2', 'MyValue-2',
   [ 'myTracker1', 'myTracker3']
)

Using native GA settings

All native options for creating a GA tracking session are exposed via the gaSettings property on Sparga's intitialization object. The following example shows how to create a custom tracker by initializing with a gaSettings object.


const sparga = new Sparga()

sparga.init({
   gaSettings: {
      trackingId: 'UA-123456-7',
      userId: 'user123',
      sessionControl: 'start',
      cookieExpires: 86400
   }
})

Default GA Settings

Sparga sets the following GA settings by default, which are different than the defaults that GA applies. As with all settings, these are overridable by passing in the respective key-value pairs on the gaSettings property of the options object.

  • allowAnchor: false
  • alwaysSendReferrer: true
  • forceSSL: true
  • cookieDomain: 'auto'
  • siteSpeedSampleRate: 100
  • storeGac: false

Other Helper Functions

  • sendEvent
  • sendPageView
  • sendException
  • sendSocial
  • sendTiming

Refer to the API.doc for more details.