|
| 1 | +# analytics.js-integration |
| 2 | + |
| 3 | +[](https://circleci.com/gh/segmentio/analytics.js-integration) |
| 4 | +[](https://codecov.io/gh/segmentio/analytics.js-integration) |
| 5 | + |
| 6 | +The base integration factory used to create custom analytics integrations for [Analytics.js](https://github.com/segmentio/analytics.js). |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +## Integrating with Segment |
| 11 | + |
| 12 | +Interested in integrating your service with us? Check out our [Partners page](https://segment.com/partners/) for more details. |
| 13 | + |
| 14 | +## Example |
| 15 | + |
| 16 | +```js |
| 17 | +var integration = require('integration'); |
| 18 | + |
| 19 | +var Custom = integration('Custom Analytics') |
| 20 | + .global('_custom') |
| 21 | + .assumesPageview() |
| 22 | + .readyOnInitialize(); |
| 23 | + |
| 24 | +Custom.prototype.track = function (event, properties) { |
| 25 | + window._custom.push(['track', event, properties]); |
| 26 | +}; |
| 27 | +``` |
| 28 | + |
| 29 | +## Facade |
| 30 | + |
| 31 | +This library relies on [`segmentio/facade`](https://github.com/segmentio/facade) which is a helper that makes working with the input to [Analytics.js](https://github.com/segmentio/analytics.js) easier, by handling lots of common cases in one place. |
| 32 | + |
| 33 | +## API |
| 34 | + |
| 35 | +### integration(name) |
| 36 | + |
| 37 | +Create a new `Integration` constructor with the given integration `name`. `name` is the key with which users can `initialize` the integration. |
| 38 | + |
| 39 | +### .option(key, default) |
| 40 | + |
| 41 | +Register a new option for the integration by `key`, with a `default` value. |
| 42 | + |
| 43 | +### .mapping(key) |
| 44 | + |
| 45 | +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. |
| 46 | + |
| 47 | +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: |
| 48 | + |
| 49 | +```js |
| 50 | +var MyIntegration = Integration('MyIntegration') |
| 51 | + .mapping('events'); |
| 52 | +``` |
| 53 | + |
| 54 | +Which means that when the integration is initialized, it would be passed a mapping of `events` to use, like so: |
| 55 | + |
| 56 | +```js |
| 57 | +new MyIntegration({ |
| 58 | + events: [ |
| 59 | + { key: 'Signed Up', value: 'Register' }, |
| 60 | + { key: 'Completed Order', value: 'Purchase' } |
| 61 | + ] |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +Then later on, you can easily get all of the entries with a specific key, by calling `this.events(key)`. For example: |
| 66 | + |
| 67 | +```js |
| 68 | +MyIntegration.prototype.track = function(track){ |
| 69 | + var matches = this.events(track.event()); |
| 70 | + each(matches, function(value){ |
| 71 | + window._myglobal.push(value); |
| 72 | + }); |
| 73 | +}; |
| 74 | +``` |
| 75 | + |
| 76 | +### .global(key) |
| 77 | + |
| 78 | +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. |
| 79 | + |
| 80 | +### .assumesPageview() |
| 81 | + |
| 82 | +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. |
| 83 | + |
| 84 | +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. |
| 85 | + |
| 86 | +### .readyOnInitialize() |
| 87 | + |
| 88 | +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. |
| 89 | + |
| 90 | +### .readyOnLoad() |
| 91 | + |
| 92 | +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. |
| 93 | + |
| 94 | +### #initialize([page]) |
| 95 | + |
| 96 | +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. |
| 97 | + |
| 98 | +### #load([callback]) |
| 99 | + |
| 100 | +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. |
| 101 | + |
| 102 | +### #identify(facade) |
| 103 | + |
| 104 | +Identify the current user for the integration given an `Identify` [`facade`](https://github.com/segmentio/facade). See the [`identify` method docs](https://segment.io/docs/tracking-api/identify/) for more information. |
| 105 | + |
| 106 | +### #group(facade) |
| 107 | + |
| 108 | +Group the current account/organization/group/etc for the integration given an `Group` [`facade`](https://github.com/segmentio/facade). See the [`group` method docs](https://segment.io/docs/tracking-api/group/) for more information. |
| 109 | + |
| 110 | +### #page(facade) |
| 111 | + |
| 112 | +Transform a `Page` [`facade`](https://github.com/segmentio/facade) into a page view for the integration. See the [`page` method docs](https://segment.io/docs/tracking-api/page-and-screen/) for more information. |
| 113 | + |
| 114 | +[Identify a user.](https://segment.io/docs/tracking-api/identify) |
| 115 | + |
| 116 | +### #track(facade) |
| 117 | + |
| 118 | +Track an event with the integration, given a `Track` [`facade`](https://github.com/segmentio/facade). See the [`track` method docs](https://segment.io/docs/tracking-api/track/) for more information. |
| 119 | + |
| 120 | +### #alias(facade) |
| 121 | + |
| 122 | +Alias two user identities given an `Alias` `facade`. See the [`alias` method docs](https://segment.io/docs/tracking-api/alias/) for more information. |
0 commit comments