Package detail

@segment/analytics.js-integration

segmentio105.4kSEE LICENSE IN LICENSE3.3.4-alpha.5

Base factory for analytics.js integrations

analytics.js, segment

readme

analytics.js-integration

CircleCI Codecov

The base integration factory used to create custom analytics integrations for Analytics.js.

The factory returns a barebones integration that has no logic, so that we can share common pieces of logic—like queueing before an integration is ready, providing a way to default options, etc—in one place.

Integrating with Segment

Interested in integrating your service with us? Check out our Partners page for more details.

Example

var integration = require('@segment/analytics.js-integration');

var Custom = integration('Custom Analytics')
  .global('_custom')
  .assumesPageview()
  .readyOnInitialize();

Custom.prototype.track = function (event, properties) {
  window._custom.push(['track', event, properties]);
};

Facade

This library relies on segmentio/facade which is a helper that makes working with the input to Analytics.js easier, by handling lots of common cases in one place.

API

integration(name)

Create a new Integration constructor with the given integration name. name is the key with which users can initialize the integration.

.option(key, default)

Register a new option for the integration by key, with a default value.

.mapping(key)

Add a new mapping option by key. The option will be an array that the user can pass in of key -> value mappings. This will also generated a #KEY method on the integration's prototype for easily accessing the mapping.

For example if your integration only supports a handful of events like Signed Up and Completed Order, you might create an mapping option called events that the user would pass in, like so:

var MyIntegration = Integration('MyIntegration')
  .mapping('events');

Which means that when the integration is initialized, it would be passed a mapping of events to use, like so:

new MyIntegration({
  events: [
    { key: 'Signed Up', value: 'Register' },
    { key: 'Completed Order', value: 'Purchase' }
  ]
});

Then later on, you can easily get all of the entries with a specific key, by calling this.events(key). For example:

MyIntegration.prototype.track = function(track){
  var matches = this.events(track.event());
  each(matches, function(value){
    window._myglobal.push(value);
  });
};

.global(key)

Register a new global variable key that the Integration uses. If this key already exists on window when initialize is called, it will return early, thus ensuring that setup logic and libraries aren't loaded twice.

.assumesPageview()

Mark the Integration as assuming an initial pageview has happened when its Javascript library loads. This is important for integrations whose libraries assume a "pageview" in their interface as soon as the library loads, instead of exposing a .page() method or similar to call via Javascript.

This option changes the integration so that the very first call to page actually initializes the integration, ensuring that the pageviews aren't accidentally duplicated.

.readyOnInitialize()

Mark the Integration as being ready to accept data after initialize is called. This is true of integrations that create queues in their snippets so that they can record data before their library has been downloaded.

.readyOnLoad()

Mark the Integration as being ready to accept data after load is called. This is true for integrations that need to wait for their library to load on the page to start recording data.

#initialize([page])

Initialize the integration. This is where the typical 3rd-party Javascript snippet logic should be. If the integration assumes an initial pageview, initialize will be called with the page method's arguments.

#load([callback])

Load the integration's 3rd-party Javascript library, and callback(err, e). The loading logic should be pulled out of the snippet from initialize and placed here.

#identify(facade)

Identify the current user for the integration given an Identify facade. See the identify method docs for more information.

#group(facade)

Group the current account/organization/group/etc for the integration given an Group facade. See the group method docs for more information.

#page(facade)

Transform a Page facade into a page view for the integration. See the page method docs for more information.

Identify a user.

#track(facade)

Track an event with the integration, given a Track facade. See the track method docs for more information.

#alias(facade)

Alias two user identities given an Alias facade. See the alias method docs for more information.

changelog

3.3.2 / 2020-04-01

  • Upgrade extend to ^3.0.2

3.3.1 / 2019-11-20

  • remove after dependency (#79)

3.3.0 / 2018-04-23

  • Don't swallow integration errors
  • Fix test that verifies invoke error behaviour.

3.2.3 / 2018-03-27

  • Migrate to Circle 2.0

3.2.2 / 2018-02-09

  • Replace lodash deepclone with extend to lower ajs size

3.2.1 / 2018-02-06

  • Replace ndhoule clone with lodash deepclone to handle circular references in objects

3.2.0 / 2016-11-07

  • Fix a bug that prevented .ready() callback from being invoked
  • Updated .assumePageview to let integrations initialize while nooping first .page() call

3.1.0 / 2016-06-13

3.0.0 / 2016-06-06

  • Update to new major analytics-events to support spec v2

2.1.0 / 2016-06-13

  • Remove window.setTimeout,setInterval restoration to prevent interference with timer mocking

2.0.1 / 2016-05-25

  • Add missing dependency to package.json dependencies field

2.0.0 / 2016-05-25

  • Remove Duo support, add Browserify support
  • Switch from Travis CI to Circle CI
  • Modernize test harness
  • Various reorganizations/cleanups

1.1.1 / 2016-05-23

  • Fix bad dependency pinning

1.1.0 / 2016-05-07

  • Update Facade dependency to 2.x, fix it to be a dev dependency

1.0.1 / 2015-06-24

  • Fix prototype#map regression

1.0.0 / 2015-06-24

  • Release as 1.0.0 (note: this change does not introduce any breaking changes, just for semver purposes)

0.4.0 / 2015-06-24

  • Add support for array data type in #events call

0.3.12 / 2015-05-22

  • Fix Integration.prototype.map regression and add test

0.3.11 / 2015-05-22

  • Add ESLint to test harness
  • Refactor existing code for sanity, ESLint compliance

0.3.10 / 2015-02-13

  • Added loadIframe method

0.3.9 / 2015-01-08

  • make: test-browser re-build on refresh
  • deps: pin them all
  • fix: ignore attributes that are not explicitly included

0.3.8 / 2014-10-17

  • Merge pull request #33 from segmentio/noop
  • .load(): default fn to noop

0.3.7 / 2014-10-17

  • deps: upgrade analytics-events
  • tests: fix typo

0.3.6 / 2014-10-16

  • .load(): respect errors

0.3.5 / 2014-10-07

  • remove assert
  • ocd: remove .load() dupe
  • reset(): restore onerror
  • travis typo

0.3.4 / 2014-09-26

  • deps: pin fmt to catch JSON fix for ie7
  • travis: add sauce creds
  • package.json: rm .script
  • travis: browser matrix
  • tests: append port
  • tests: ie8..10
  • tests: support ie*

0.3.3 / 2014-09-23

  • pin component deps

0.3.2 / 2014-09-04

  • updating analytics-events and the makefile

0.3.1 / 2014-07-11

  • emit ready on next frame

0.3.0 / 2014-07-11

  • ready on initialize by default
  • duo creds
  • use duo-test
  • make: remove server
  • bump node version to 0.11
  • make new load method load tags (script/img/iframe). so, you don't need to implement .load when building a new integration.
  • add .tag to the DSL, where you define script/img/iframe tags, instead of loading them manually. they use mustache-like templates, getting the options and whatever other "locals" you send to the .load method
  • remove old load method

0.2.4 / 2014-07-08

  • port to duo
  • swap callback and ready event

0.2.3 / 2014-07-02

  • reset window defaults
  • update readme
  • version component

0.2.2 / 2014-05-16

  • allow camelCase and snake_case names

0.2.1 / 2014-04-29

  • .mapping(): make sure it returns the integration for chaining
  • make: move rm node_modules to distclean target, for speed
  • add .mapping(name)

0.2.0 / 2014-04-26

  • add #events method to find events.
  • component: add githubusercontent to remotes

0.1.9 / 2014-04-16

  • make sure initialize is called with page

0.1.8 / 2014-04-15

  • make sure all methods return the real values

0.1.7 / 2014-01-30

  • pass all arguments to track()
  • dont wait until "ready" event to call track() directly

0.1.6 / 2014-01-30

  • add eCommerce api

0.1.5 - November 26, 2013

  • adding readyOnLoad fix to still call load callback

0.1.4 - November 12, 2013

  • debug: upgrade to 0.7.3

0.1.2 - November 11, 2013

  • rename section argument to category

0.1.1 - November 11, 2013

  • add section argument to page method signature

0.1.0 - November 10, 2013

  • change exists to loaded and check in load

0.0.7 - November 10, 2013

  • add construct event to integration

0.0.6 - November 6, 2013

  • fix ready to always be emitted asynchronously

0.0.5 - November 6, 2013

  • add exists check for globals

0.0.4 - November 5, 2013

  • move initialize on page logic into page
  • move initialized state into initialize
  • remove handling of initialPageview option

0.0.3 - October 29, 2013

  • change initialize to be called with page args

0.0.2 - October 29, 2013

  • change globals to be an array of keys

0.0.1 - October 29, 2013

:sparkles: