@segment/analytics.js-integration

Base factory for analytics.js integrations

analytics.js-integration

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 pagefor 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/facadewhich is a helper that makes working with the input to Analytics.jseasier, by handling lots of common cases in one place.

API

integration(name)

Create a new Integrationconstructor with the given integration name. nameis the key with which users can initializethe integration.

.option(key, default)

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

.mapping(key)

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

For example if your integration only supports a handful of events like Signed Upand Completed Order, you might create an mapping option called eventsthat 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 eventsto 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 keythat the Integration uses. If this key already exists on windowwhen initializeis called, it will return early, thus ensuring that setup logic and libraries aren't loaded twice.

.assumesPageview()

Mark the Integrationas 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 pageactually initializes the integration, ensuring that the pageviews aren't accidentally duplicated.

.readyOnInitialize()

Mark the Integrationas being ready to accept data after initializeis 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 Integrationas being ready to accept data after loadis 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, initializewill be called with the pagemethod'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 initializeand placed here.

#identify(facade)

Identify the current user for the integration given an Identifyfacade. See the identifymethod docsfor more information.

#group(facade)

Group the current account/organization/group/etc for the integration given an Groupfacade. See the groupmethod docsfor more information.

#page(facade)

Transform a Pagefacadeinto a page view for the integration. See the pagemethod docsfor more information.

Identify a user.

#track(facade)

Track an event with the integration, given a Trackfacade. See the trackmethod docsfor more information.

#alias(facade)

Alias two user identities given an Aliasfacade. See the aliasmethod docsfor more information.

HomePage

https://github.com/segmentio/analytics.js-integration#readme

Repository

https://github.com/segmentio/analytics.js-integration


上一篇:@segment/localstorage-retry
下一篇:@segment/ad-params

相关推荐

  • 🙋Hanjst汉吉斯特改进+enSafeExpression安全表达式等

    Hanjst汉吉斯特模版语言及模版引擎,近期持续改进升级。 这次改进主要是增加了对安全输出表达式兼容,由于涉及到对软件开发过程中的效率和软件运行效率的平衡和取舍,所以多写了几句,以描述这个权衡利弊对...

    15 天前
  • 🙋Hanjst汉吉斯特升级:+showImageAsync及性能改进等

    自2019年元旦🙋Hanjst汉吉斯特 模板语言及其编译引擎发布,已经过去一年多了。 这期间随着 🙋Hanjst汉吉斯特 的推广应用,我们也陆续发布了如下一些更新内容: 🛠️Hanjst/汉吉...

    2 个月前
  • 😉我用 Nuxt.js 仿了个掘金

    前言 首先肯定是要夸夸掘金啦,最开始从 CSDN 到 博客园 再到 掘金,个人感觉掘金的技术氛围非常的nice,真是个宝藏社区👏。技术文章大多以前端为主,对前端开发者非常友好,质量也是歪瑞古的。

    25 天前
  • 😀一个原生js弹幕库

    danmujs 😀一个原生js弹幕库,基于 CSS3 Animation 地址、核心代码 本项目基于 rcbullets,项目约70%的代码基于rcbullets,首先要感谢这个项目的作者,如...

    4 个月前
  • 🕵️‍♀️由原型到JS中的“模拟类”

    讲述了有关 JavaScript 中原型相关知识,又引出了 JavaScript 中的“类“究竟是什么?,以及一系列相关问题。 一、前置知识 1、JavaScript 的面向对象(OOP) ​ 面向...

    2 个月前
  • 🔥《吊打面试官》系列 Node.js 必知必会必问!

    (/public/upload/f204a3b224d986128f1b4d9b8d06cd17) 前言 codeing 应当是一生的事业,而不仅仅是 30 岁的青春🍚 本文已收录 Git...

    3 个月前
  • 💖CSS + JS 送学妹满屏幕小爱心

    故事开始 午饭时间,暗恋已久的学妹拉着我的衣袖:“学长学长,你能不能让这些爱心变成五颜六色的吗~”。 我在旁边笑开了花~~~ image.png(/public/upload/04aaa24e...

    1 个月前
  • (vuejs学习)2、使用ElementUI(*)

    1.element安装 开发环境是win10,一到node官网下载node的.msi包(https://npm.taobao.org/mirrors/node/v10.16.0/nodev10.16....

    10 个月前
  • (vuejs学习)1、Vue初上手(*)

    参考《官方(https://cli.vuejs.org/zh/guide/installation.html)》官方: Node 版本要求: Vue CLI 需要 Node.js 8.9 或更高...

    10 个月前
  • 黄金搭档 -- JS 装饰器(Decorator)与Node.js路由

    很多面对象语言中都有装饰器(Decorator)函数的概念,Javascript语言的ES7标准中也提及了Decorator,个人认为装饰器是和一样让人兴奋的的变化。

    1 年前

官方社区

扫码加入 JavaScript 社区