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.
With yarn:
yarn add react-amplitude
With npm:
npm install react-amplitude --save
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'));
});
Must be initialized using this function before any of the other tracking functions will record any data.
Amplitude.init(apiKey, userId, config, cb);
Value | Notes |
---|---|
apiKey | String . Required. |
userId | String . Optional. |
config | Object . Optional. |
callback | Function . Optional. |
Log an event to Amplitude.
Amplitude.logEvent(eventName, eventProperties, cb);
Value | Notes |
---|---|
eventName | String . Required. |
eventProperties | Object . Optional. |
callback | Function . Optional. |
Log an event to Amplitude.
Amplitude.logEventWithTimestamp(eventName, eventProperties, timestamp, cb);
Value | Notes |
---|---|
eventName | String . Required. |
eventProperties | Object . Optional. |
timestamp | Number . Optional. |
callback | Function . Optional. |
Remove user tracking (e.g. on logging out).
Amplitude.resetUserId();
Track users through a unique user id.
Amplitude.setUserId(userId);
Value | Notes |
---|---|
userId | String . Required. |
Track user properties
Amplitude.setUserProperties(userProps);
Value | Notes |
---|---|
userProps | object . Required. |
Clear user properties (careful, this is irreversible!)
Amplitude.clearUserProperties();
Returns current session id
Amplitude.getSessionId();
Send an identify call containing user property operations to Amplitude servers
Amplitude.identify(idObj, cb);
Value | Notes |
---|---|
idObj | object . Required. |
callback | Function . Optional. |
Returns if a new session was created at init
Amplitude.isNewSession();
git clone https://github.com/rorygarand/react-amplitude.git
yarn install
npm run build