rorygarand / react-amplitude

React Amplitude Analytics
11 stars 13 forks source link

react-amplitude

React Amplitude Analytics

npm version npm downloads

This is a JavaScript module that can be used to include Amplitude Analytics tracking code in a website or app that uses React for its front-end codebase.

Feel free to file issues, ideas and pull requests against this repo.

Installation

With yarn:

yarn add react-amplitude

With npm:

npm install react-amplitude --save

Usage

Initializing:

import React from 'react';
import ReactDOM from 'react-dom';

...
import Amplitude from 'react-amplitude';
Amplitude.init('YOUR_UNIQUE_TRACKING_CODE');
...

document.addEventListener('DOMContentLoaded', function() {
  ReactDOM.render(<App />, document.getElementById('app'));
});

API

Amplitude.init(apiKey, userId, config, callback)

Must be initialized using this function before any of the other tracking functions will record any data.

Example
Amplitude.init(apiKey, userId, config, cb);
Value Notes
apiKey String. Required.
userId String. Optional.
config Object. Optional.
callback Function. Optional.

Amplitude.logEvent(eventName, eventProperties, callback)

Log an event to Amplitude.

Example
Amplitude.logEvent(eventName, eventProperties, cb);
Value Notes
eventName String. Required.
eventProperties Object. Optional.
callback Function. Optional.

Amplitude.logEventWithTimestamp(eventName, eventProperties, timestamp, callback)

Log an event to Amplitude.

Example
Amplitude.logEventWithTimestamp(eventName, eventProperties, timestamp, cb);
Value Notes
eventName String. Required.
eventProperties Object. Optional.
timestamp Number. Optional.
callback Function. Optional.

Amplitude.resetUserId()

Remove user tracking (e.g. on logging out).

Example
Amplitude.resetUserId();

Amplitude.setUserId(userId)

Track users through a unique user id.

Example
Amplitude.setUserId(userId);
Value Notes
userId String. Required.

Amplitude.setUserProperties(userProps)

Track user properties

Example
Amplitude.setUserProperties(userProps);
Value Notes
userProps object. Required.

Amplitude.clearUserProperties()

Clear user properties (careful, this is irreversible!)

Example
Amplitude.clearUserProperties();

Amplitude.getSessionId()

Returns current session id

Example
Amplitude.getSessionId();

Amplitude.identify(idObj, callback)

Send an identify call containing user property operations to Amplitude servers

Example
Amplitude.identify(idObj, cb);
Value Notes
idObj object. Required.
callback Function. Optional.

Amplitude.isNewSession()

Returns if a new session was created at init

Example
Amplitude.isNewSession();

Development

git clone https://github.com/rorygarand/react-amplitude.git
yarn install
npm run build

Acknowledgements

Contributors