neiker / analytics-react-native

The hassle-free way to integrate analytics into any React Native application. https://segment.com/libraries/node
MIT License
109 stars 11 forks source link

Build Status Code Climate codecov.io

dependencies Greenkeeper badge

A React Native client for Segment. The hassle-free way to integrate analytics into any application.

This library is based on its node counterpart, analytics-node. Despite being designed to run on react-native, it will also work in browsers (via webpack or browserify) and even in nodejs. You only need to include fetch for browsers or node-fetch for nodejs or io.js.

Installation

npm install analytics-react-native

Usage

import Analytics from 'analytics-react-native';

const analytics = new Analytics(YOUR_WRITE_KEY);

analytics.identify({
  userId: user.id,
  traits: {
    name: 'John',
    lastname: 'Doe',
    email: 'user@domain.com',
    plan: 'Enterprise',
  }
});

analytics.track({
  userId: user.id,
  event: 'Item Purchased',
  properties: {
    revenue: 39.95,
    shippingMethod: '2-day'
  }
});

Configuration

The second argument to the Analytics constructor is an optional object to configure the module.

const analytics = new Analytics(YOUR_WRITE_KEY, {
  host: 'http://localhost/', // Host where reports will be send. Useful for debug.
  flushAt: 20, // The number of messages to enqueue before flushing.
  flushAfter: 10000 // The number of milliseconds to wait before flushing the queue automatically.
});

Documentation

Documentation is available at https://segment.com/libraries/node.

Differences from analytics-node

Screen method

In addition to methods available in analytics-node, documented above, screen method is available. The screen method lets you you record whenever a user sees a screen of your mobile app, along with optional extra information about the page being viewed.

You’ll want to record a screen event an event whenever the user opens a screen in your app. This could be a view, fragment, dialog or activity depending on your app.

You can call it using exactly the same params as page method.

analytics.screen({
  userId: 'john_doe',
  name: 'products_list',
  properties: {
    order: 'ASC',
    page: 2,
    // And any other data about this screen
  }
});