Step 1: Collect

Welcome to mParticle! Let’s send your first event.


iOS


1. Generate your API key

Open your workspace and generate iOS API credentials on the Setup page.

2. Add the SDK to your project

You can add the SDK via CocoaPods or Carthage.

CocoaPods

Specify our SDK in your Podfile:

use_frameworks!

target '<Your Target>' do
    pod 'mParticle-Apple-SDK', '~> 7.0'
end

Carthage

Specify our SDK in your Cartfile:

github "mparticle/mparticle-apple-sdk" ~> 7.0

3. Initialize the SDK

import mParticle_Apple_SDK

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    //initialize mParticle
    let options = MParticleOptions(key: "YOUR_API_KEY",
                                         secret: "YOUR_API_SECRET")
    options.environment = .development   
    MParticle.sharedInstance().start(with: options)
        
    return true
}
// Assumes the SDK has been included as a dynamic library
// Requires "Enable Modules (C and Objective-C)" in pbxproj
@import mParticle_Apple_SDK; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //initialize mParticle
    MParticleOptions *options = [MParticleOptions optionsWithKey:@"YOUR_API_KEY"
                                                          secret:@"YOUR_API_SECRET"];
    options.environment = MPEnvironmentDevelopment;
    [[MParticle sharedInstance] startWithOptions:options];
    return YES;
}

4. Verify your installation

Go to your Live Stream and watch new Session events come in as you launch your app in the emulator.

Next Steps πŸŽ‰

Congrats on sending your first event to mParticle!

Some ideas on what to do next:


Android


1. Generate your API key

Open your workspace and generate Android API credentials on the Setup page.

2. Add the SDK to your project

Add our SDK to your build.gradle file:

dependencies {
    // Alternatively, you can target a specific version
    // https://github.com/mParticle/mparticle-android-sdk/releases
    implementation 'com.mparticle:android-core:5+'
}

3. Initialize the SDK

Initialize the SDK in the onCreate() method of your app’s Application or launcher Activity class.

//import mParticle
import com.mparticle.MParticle
import com.mparticle.MParticleOptions

class ExampleApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        var options = MParticleOptions.builder(this)
            .credentials("YOUR_API_KEY", "YOUR_API_SECRET")
            .environment(MParticle.Environment.Development)
            .build()
        MParticle.start(options)
    }
}
//import mParticle
import com.mparticle.MParticle;
import com.mparticle.MParticleOptions;

public class ExampleApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        MParticleOptions options = MParticleOptions.builder(this)
                .credentials("YOUR_API_KEY", "YOUR_API_SECRET")
                .environment(MParticle.Environment.Development)
                .build();
        MParticle.start(options);
    }
}

4. Verify installation

Go to your Live Stream and watch new Session events come in as you load your app in the emulator.

Next Steps πŸŽ‰

Congrats on sending your first event to mParticle!

Some ideas on what to do next:


Web


1. Generate your API key

Open your workspace and generate web API credentials on the Setup page.

2. Install our client library

Copy this code snippet and place it in the <head> tag of each page of your web app.

<script type="text/javascript">
  window.mParticle = {
    config: {
      isDevelopmentMode: true //switch to false (or remove) for production
    }
  };

  (
    function(t){window.mParticle=window.mParticle||{};window.mParticle.EventType={Unknown:0,Navigation:1,Location:2,Search:3,Transaction:4,UserContent:5,UserPreference:6,Social:7,Other:8};window.mParticle.eCommerce={Cart:{}};window.mParticle.Identity={};window.mParticle.config=window.mParticle.config||{};window.mParticle.config.rq=[];window.mParticle.config.snippetVersion=2.2;window.mParticle.ready=function(t){window.mParticle.config.rq.push(t)};var e=["endSession","logError","logBaseEvent","logEvent","logForm","logLink","logPageView","setSessionAttribute","setAppName","setAppVersion","setOptOut","setPosition","startNewSession","startTrackingLocation","stopTrackingLocation"];var o=["setCurrencyCode","logCheckout"];var i=["identify","login","logout","modify"];e.forEach(function(t){window.mParticle[t]=n(t)});o.forEach(function(t){window.mParticle.eCommerce[t]=n(t,"eCommerce")});i.forEach(function(t){window.mParticle.Identity[t]=n(t,"Identity")});function n(e,o){return function(){if(o){e=o+"."+e}var t=Array.prototype.slice.call(arguments);t.unshift(e);window.mParticle.config.rq.push(t)}}var mp=document.createElement("script");mp.type="text/javascript";mp.async=true;mp.src=("https:"==document.location.protocol?"https://jssdkcdns":"http://jssdkcdn")+".mparticle.com/js/v2/"+t+"/mparticle.js";var c=document.getElementsByTagName("script")[0];c.parentNode.insertBefore(mp,c)}
  )("YOUR_API_KEY");
</script>

3. Verify installation

Go to your Live Stream and watch new Session events come in as you reload the page.

Next Steps πŸŽ‰

Congrats on sending your first event to mParticle!

Some ideas on what to do next:


HTTP


1. Generate your API key

Create a Custom Feed on the Setup page to generate server-to-server API credentials.

2. Send an HTTP request

Use curl or Postman to send an HTTP request with your access credentials to our server-to-server endpoint https://s2s.mparticle.com/v2/events.

curl

Create data.json with the contents of your request.

{
  "user_identities": {
    "customer_id": "1234",
    "email": "hello@mparticle.com"
  },
  "events": [
    {
      "data": { "event_name": "click" },
      "event_type": "custom_event",
      "custom_attributes": {
        "button_name": "home",
        "other_attribute": "xyz"
      }
    }
  ],
  "environment": "development"
}

Run curl from the same directory.

curl -u YOUR_API_KEY:YOUR_API_SECRET -vX POST -H "Content-Type: application/json" -d @data.json https://s2s.mparticle.com/v2/events

Postman

Run in Postman

Use our Postman collection to make an HTTP request to the Events API.

3. Verify

Go to the Live Stream and watch new events come in as you send requests.

Next Steps πŸŽ‰

Congrats on sending your first event to mParticle!

Some ideas on what to do next:


Python


1. Generate your API key

Create a Custom Feed on the Setup page to generate server-to-server API credentials.

2. Install the Python SDK

Install the Python SDK via pip:

pip install git+https://github.com/mparticle/mparticle-python-sdk.git

3. Call the Events API

Integrate mParticle in your Python application.

import mparticle
from mparticle import AppEvent, SessionStartEvent, SessionEndEvent, Batch

batch = Batch()
batch.environment = 'development'

app_event = AppEvent('Hello World', 'navigation')
batch.events = [SessionStartEvent(), app_event, SessionEndEvent()]

configuration = mparticle.Configuration()
configuration.api_key = 'YOUR_API_KEY'
configuration.api_secret = 'YOUR_API_SECRET'

api_instance = mparticle.EventsApi(configuration)
api_instance.upload_events(batch)

4. Verify installation

Go to the Live Stream and watch new events come in as you run your script.

Next Steps πŸŽ‰

Congrats on sending your first event to mParticle!

Some ideas on what to do next:


Java


1. Generate your API key

Create a Custom Feed on the Setup page to generate server-to-server API credentials.

2. Add the mParticle SDK dependency

Our Java SDK is available via Gradle or Maven.

Gradle

Add the SDK dependency to your build.gradle file.

dependencies {
  implementation 'com:mparticle:server-events-sdk:2+'
}

Maven

Add the SDK dependency to your pom.xml file.

<dependency>
  <groupId>com.mparticle</groupId>
  <artifactId>server-events-sdk</artifactId>
  <version>2.0.0</version>
</dependency>

3. Call the Events API

Now you can integrate mParticle in your Java application.

// configure API
EventsApi api = new ApiClient(
        "YOUR_API_KEY",
        "YOUR_API_SECRET")
        .createService(EventsApi.class);

// assemble an event batch
Batch batch = new Batch();
batch.environment(Batch.Environment.DEVELOPMENT);
batch.userIdentities(new UserIdentities()
        .customerId("1234")
        .email("example@foo.com")
);

// create an event
CustomEvent customEvent = new CustomEvent().data(
        new CustomEventData()
                .eventName("bid")
);

// create attributes
Map customAttributes = new HashMap<>();
customAttributes.put("price", 33);

// add them to an event
customEvent.getData().customAttributes(customAttributes);
batch.addEventsItem(customEvent);

// upload
Call<Void> singleResult = api.uploadEvents(batch);
Response<Void> singleResponse = singleResult.execute();
System.out.println("Returned code: " + singleResponse.code());

4. Verify installation

Go to the Live Stream and watch new events come in as you run your application.

Next Steps πŸŽ‰

Congrats on sending your first event to mParticle!

Some suggestions on what to do next:


Node


1. Generate your API key

Create a Custom Feed on the Setup page to generate server-to-server API credentials.

2. Install the Node SDK

Install the Node SDK via npm:

npm install mparticle

3. Call the Events API

Integrate mparticle in your Node app.

var mParticle = require('mparticle');

var api = new mParticle.EventsApi(new mParticle.Configuration(
    'YOUR_API_KEY', 
    'YOUR_API_SECRET'));

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);

4. Verify installation

Go to the Live Stream and watch new events come in as you run your script.

Next Steps πŸŽ‰

Congrats on sending your first event to mParticle!

Some suggestions on what to do next:

Was this page helpful?