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

tracktics

v1.0.0

Published

Framework-agnostic, pluggable dispatcher for tracking analytics data

Downloads

5

Readme

Tracktics

Tracktics provides a framework-agnostic, pluggable inversion-of-control container for analytics tracking. Analytics events may be specified manually using the tracker API, or declaratively using data-tracktics-* DOM element attributes.

npm Version Build Status Test Coverage Dependency Status

Installation

Install using npm:

$ npm install tracktics

Browser Support

IE9+

Examples

Basic Usage

Basic usage that integrates with Google Analytics in a simple jQuery app:

'use strict';

var $ = require('jquery'),
    tracktics = require('tracktics'),
    tracker = tracktics();

// Register the Google Analytics plugin.
tracker.use(require('tracktics-google-analytics')());

$(document).ready(function() {
    // Add event listeners for mouse events on elements that have had
    // data-tracktics-* attributes defined.
    tracker.bind();
});

Basic Usage with Page Tracking

Similar to the previous example, but adds a hook into the Backbone router to provide additional page navigation.

'use strict';

var $ = require('jquery'),
    Backbone = require('backbone'),
    tracktics = require('tracktics'),
    tracker = tracktics(),
    app = require('./app');

tracker.use(require('tracktics-google-analytics')());

$(document).ready(function() {
    app.init();

    Backbone.history.on('route', function() {
        var url = Backbone.history.root + Backbone.history.getFragment();
        tracker.trackPage(url);
    });

    Backbone.history.start();
});

Supported Analytics Providers

API

tracktics()

var tracktics = require('tracktics'),
    tracker = tracktics();

The main Tracktics export, tracktics is a factory function for generating Tracktics tracker instances. Calling this method returns a tracker object that can be used to register one or more analytics provider plugins. Once any number of plugins have been registered, the tracker can then be used to manually dispatch events to the providers, or to bind event handlers for having events of interest be dispatched automatically.

#use(<AnalyticsProviderPlugin>)

tracker.use(myAnalyticsProvider);

Register a Tracktics analytics provider plugin. A provider plugin can be any object that exposes either or both methods trackPage and trackEvent. It's then up to the plugin to handle event data and payload.

#bind()

tracker.bind();

Bind event listeners for handling mouse events on DOM elements that include Tracktics data-tracktics-* attributes. These listeners are bound to document.body and operate via event delgation to keep the footprint low.

#unbind()

tracker.unbind();

Unbind event listeners for handling Tracktics mouse events.

#trackPage(url, [options])

tracker.trackPage('/some/url');

Method for manual page tracking. Options will vary depending on the provider.

#trackEvent(eventName, [options])

tracker.trackEvent('someEvent');

Method for manual event tracking. Options will vary depending on the provider.

Declarative Analytics Tracking

Tracktics allows and encourages the use of declarative markup for configuring event-based tracking. This is done by adding data-tracktics-* attributes to any DOM elements of interest, and then calling .bind() on the Tracktics tracker instance:

<button id="purchase-button" type="button" data-tracktics-on="click">Purchase</button>
var tracktics = require('tracktics'),
    tracker = tracktics();

// ... register provider plugin(s), then:
tracker.bind();

data-tracktics-on (required)

The only required attribute for event delegation to function, data-tracktics-on is used to identify DOM elements of interest and specify what DOM event type should be listened to for the given element. Valid values for this attribute include "click", "mouseup", and "mousedown". If no value is specified, "click" is the default behavior. Provider plugin trackEvent functions will receive this value as the first argument.

data-tracktics-event

Use this attribute to manually specify the event name that gets dispatched to trackEvent. If this attribute is missing, the DOM element's innerText is used instead. Event data will be passed along to the registered provider plugins via the event key in the options argument.

data-tracktics-*

Any other data-tracktics-* attributes may be specified. This data will be passed along to the registered provider plugins via the options object. For example, if a DOM element were to define data-tracktics-category="Sales", the provider would receive { category: "Sales" } via options. This gives powerful flexibility to provider plugins, and the means to be able to support a wide array of capabilities.

Provider Plugins

Any JavaScript object can serve as a Tracktics provider plugin, provided that it exposes a trackPage and/or trackEvent method. These methods will be called on registered provider plugins when an analytics event is dispatched, and it is up to the plugin to define how that event gets handled.

#trackPage(url, [options])

Vendor-specific handler for page tracking. Tracktics page tracking is currently a manual process. This method will be called on registered provider plugins with the exact same data that was passed to the tracker's trackPage method.

#trackEvent(eventName, [options])

Vendor-specific handler for event tracking. Event tracking can be done manually via the tracker's trackEvent method, or declaratively via data-tracktics-* attributes. This method will be called on registered provider plugins with the DOM event name (click/mouseup/mousedown) and any optional associated data for the event.

License

MIT