Skip to content

API Client

Sam Dozor edited this page Jan 13, 2017 · 5 revisions

API Client

The SDK provides an interface to the mParticle HTTP API by way of the EventsApi class.

At minimum, the EventsApi must be initialized with a Configuration object, containing an mParticle key and secret. You can find your mParticle key and secret by navigating to the Apps section of the mParticle platform UI.

You must associate your data with the correct key and secret. If your app is multi-platform, for example, be sure to send your Android data to your Android key/secret, and your iOS data to your iOS key/secret.

var api = new mParticle.EventsApi(new mParticle.Configuration(
    'REPLACE WITH API KEY', 
    'REPLACE WITH API SECRET'));

Uploading Data

The EventsAPI class exposes two interfaces:

  • bulkUploadEvents - Accepts up to 100 Batch objects for up to 100 users.
  • uploadEvents - Accepts a single Batch object for a single user
var batch = new mParticle.Batch(mParticle.Batch.Environment.development);

batch.user_identities = new mParticle.UserIdentities();
batch.user_identities.customerid = '123456' // identify the user (required)

batch.user_attributes = {'hair color': 'brown'}

var event = new mParticle.AppEvent(mParticle.AppEvent.CustomEventType.navigation, 
  'Hello World');

batch.addEvent(event);

var body = [batch]; // {[Batch]} Up to 100 Batch objects

var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully.');
  }
};

api.bulkUploadEvents(body, callback);

// or upload a single batch
//api.uploadEvents(body, batch) 
Clone this wiki locally