Code Monkey home page Code Monkey logo

meteor-segment.io's Introduction

Segment.io integration for Meteor

  • Note: We've switched to the official versioning scheme for tracking upstream packages (2.0.0_1). Unfortunately, meteor will pick the highest version (in this case 3.0.0) which is old and should not be used. Please choose the latest version explicitly like so meteor add percolatestudio:segment.io@=2.0.0_1). *

Works on client and server.

Client

The segment.io snippet is copied almost verbatim from the docs, however, analytics is exposed via the Meteor package rather than window.

Snippet version is 3.1.0.

Server

Using official analytics-node npm module.

Usage

Almost all the apis are available on both client and server

The un-inited analytics object is available for use as per normal.

First, initialize analytics:

analytics.load(YOUR_KEY);

Then, you could send a pageview from IronRouter:

Router.onRun(function() {
  // Defer calling page() till the next tick so it correctly records the
  // current url as opposed to the previous one
  Meteor.setTimeout(function() {
    analytics.page();
  });
  this.next(); // If using versions of IronRouter later than 0.9.4
});

To identify the user, you'd setup an autorun:

Meteor.startup(function() {
  Tracker.autorun(function(c) {
    // waiting for user subscription to load
    if (! Router.current() || ! Router.current().ready())
      return;

    var user = Meteor.user();
    if (! user)
      return;

    analytics.identify(user._id, {
      name: user.profile.name,
      email: user.emails[0].address
    });

    c.stop();
  });
});

Aliasing anonymous users to a registered user when their account is created:

Accounts.createUser({
  email: email,
  password: password,
  profile: {
    name: name
  }
}, function(error) {
  if (! error) {
    analytics.alias(Meteor.userId());
  } else {
    alert('Error creating account!\n' + EJSON.stringify(error));
  }
});

Tracking an event is as simple as:

analytics.track('Purchased T-Shirt', {
  name: 'The Cake is a Liar',
  revenue: 14.99
});

License

MIT. (c) Percolate Studio, maintained by Zoltan Olah (@zol).

meteor-segment.io's People

Contributors

zol avatar againer avatar arunoda avatar jshimko avatar andylash avatar chrisbutler avatar fpoirier1 avatar sanjo avatar lorensr avatar tadruj avatar sdybskiy avatar

Watchers

0x44 0x46 avatar Samuel Chow avatar James Cloos avatar Ben Hencke avatar  avatar wayne tsuchitani avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.