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

trak.js

v1.0.0

Published

Universal event tracking API wrapper

Downloads

148

Readme

trak.js - Universal analytics event tracking API wrapper

Put simply, trak.js is a wrapper for any analytics API. By default it uses Google Universal Analytics but you can override this with the older ga.js or Google Tag Manager if you wish, or you can even add custom event trackers as well, instead of Google Analytics.

npm version Build Status Code Climate NPM

Getting the Library

Direct downloads

Bower

bower install trak

NPM

npm install trak.js --save

Usage

Include trak.js in your JavaScript bundle or add it to your HTML page like this:

<script type='application/javascript' src='/path/to/trak.js'></script>

or with NPM/Browserify

var trak = require('trak.js');

then run trak.start(); when the DOM is ready (after version 0.3.0 this has changed, run trak(); before version 0.3.0):

// Native JS
document.addEventListener('DOMContentLoaded', function(e) {
  trak.start();
}

// jQuery
$(function(){
  trak.start();
});

API Reference

There are two main ways to use trak.js, in your js code or as data-trak attributes in your markup.

JS implementation:

trak.event({category: '', action: '', label: '' , value: '', nonInteraction: '', eventName: ''})

Fires an analytics event

Arguments object: (these are all optional)

category: A string value of the category value to set action: A string value of the action value to set label: A string value of the label value to set value: An integer trigger: A string value of a valid event name: click, focus, mouseover etc nonInteraction: An integer eventName: A string value used only with Google Tag Manager. Define your GTM event name here

If any property is left undefined, the browser's default value will be used instead.

trak.event({category: 'category value', action: 'action value'});
trak.event({category: 'category value', action: 'action value', label: 'label value'});
trak.event({category: 'category value', action: 'action value', label: 'label value' , value: '', nonInteraction: '', eventName: ''});
Example:
el.addEventListener('click', function() {
  trak.event({category: 'engagement', action: 'signpost', label: 'page.href'});
}
el.addEventListener('mouseover', function() {
  trak.event({
    category: 'engagement',
    action: 'signpost',
    label: 'page.href',
    value: 10,
    nonInteraction: true,
    eventName: 'This is a Google Tag Manager event name'
  });
}

data-trak attr implementation

<a data-trak='{"category":"Rating","action":"Comparison notepad","label":"Up"}' href="#">link</a>

Custom trigger type (new as of v0.4.0)

data-trak attrs can also define a custom trigger type instead of click. Now mouseover, touchstart, focus, blur or any other valid event can be used to trigger a trak event.

<!-- Triggered on focus -->
<a data-trak='{"trigger":"focus","category":"Test category","action":"Test action","label":"Test label"}' href="#pagehref">Custom trigger type</a>

Wildcards

Wildcards can be used to specify certain options like the page title or url.

page.href: Uses window.location.href
<a data-trak='{"category":"Rating","action":"page.href","label":"Up"}' href="#">link</a>
page.title: Uses document.title
<a data-trak='{"category":"Rating","action":"page.title","label":"Up"}'href="#" >link</a>
link.href: Uses this.href
<a data-trak='{"category":"Rating","action":"link.href","label":"Up"}' href="#">link</a>
link.title: Uses this.title
<a data-trak='{"category":"Rating","action":"link.title","label":"Up"}' href="#">link</a>
referrer: Uses document.referrer
<a data-trak='{"category":"Rating","action":"document.referrer","label":"Up"}' href="#">link</a>

Using data-trak attr options with but fire event with js

You can also use data-* attr options but fire events in js. To do this, add the relevant data-trak data and also a data-trakwithjs boolean attribute. This means that the event will only fire when you run it in your js. To run in your js, use the trak.attrEvent method like we have below:

<a data-trakwithjs data-trak='{"category":"Tracked with JS not attr call","action":"link.href","label":"this is a label"}' href="#pagehref">trakwithjs</a>

<script>
  el.addEventListener('click', function() {
    trak.attrEvent.call(this);
  });
</script>

See this in use in the trak demo.


Options

Various default trak.js options can be overridden:

trak.options.clean

Choose whether you'd like to clean the provided category, action and labels

Type: boolean Default: true

trak.options.delimeter

trak.js includes a cleaning method to normalise the arguments that are passed to it. Spaces are converted to an underscore by default but can be overridden by reassigning this value.

Type: string Default: _

trak.options.trackType

Type: string Default: ga

Alternatives:

  • ga : Google Analytics (Universal
  • _gaq : Google Analytics (ga.js) Old version
  • gtm : Google Tag Manager

Use this to change your default tracking provider.

trak.options.additionalTypes

Type: function Default: undefined

Add any other event tracking providers. See below for example:

trak.options.additionalTypes = function(opts) {
  UDM.evq.push(['trackEvent', trak.clean(opts.category), trak.clean(opts.action)]); // trak.clean(opts.label)
  console.log('Fire additional event:', opts);
}

trak.options.debug

Type: boolean Default: false

Show debug logs in the javascript console


Which tracking API's are used?

The default implementation uses latest version of Google Analytics (ga.js) but trak.js also supports the older _gaq type or Google Tag Manager.


Browser Compatibility

trak.js has been tested in the following browsers:

  • Chrome
  • Firefox 3+
  • Opera 10+
  • Internet Explorer 8+