mParticle.Sdk 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package mParticle.Sdk --version 1.0.0                
NuGet\Install-Package mParticle.Sdk -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="mParticle.Sdk" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add mParticle.Sdk --version 1.0.0                
#r "nuget: mParticle.Sdk, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install mParticle.Sdk as a Cake Addin
#addin nuget:?package=mParticle.Sdk&version=1.0.0

// Install mParticle.Sdk as a Cake Tool
#tool nuget:?package=mParticle.Sdk&version=1.0.0                

<img src="https://static.mparticle.com/sdk/mp_logo_black.svg" width="280">

Dotnet Server Events SDK

This is the mParticle Dotnet SDK for the server-based Events API - use it to send your data to the mParticle platform and off to 250+ integrations! The SDK is designed to be use in a variety of environments. It provides a Retrofit interface as well as serializable models if you prefer to handle HTTP transport on your own.

Requirements.

Dotnet 2.0 and later

Getting Started

Add the Dependency

The SDK is available via NuGet.

Install-Package mParticle.Sdk [-Version <VERSION>]

Create a Batch

All data that passes through mParticle does so in the form of a "batch." A batch describes identities, attributes, events, and other information related to a single user. This SDK lets you upload either single batches or multiple batches at a time.

The full schema of a batch is documented in the mParticle Events API overview. The models in this SDK directly match the JSON referenced in the overview.

Batch batch = new Batch();
batch.Environment = BaseBatch.EnvironmentEnum.Development;

// Set user identities
batch.UserIdentities = new BatchUserIdentities() 
{
        CustomerId = "1234",
        Email = "example@foo.com"
};
                


// Set device identities
batch.DeviceInfo = new DeviceInformation() 
{
        IosAdvertisingId = "5864e6b0-0d46-4667-a463-21d9493b6c10"
};            

// Set user attributes
var userAttributes = new Dictionary<string, object>();
userAttributes["foo"] = "bar";
userAttributes["foo-array"] = new String[]{"bar1", "bar2"};
userAttributes["foo-array-2"] = new List<string>() {{"bar3"},{"bar4"}};
batch.UserAttributes = userAttributes;

It's critical to include either user or device identities with your server-side data

Create Events

All mParticle events have a similar structure:

  • event_type: this is the type of event, such as custom_event and commerce_event
  • data: this contains common properties of all events, as well as properties specific to each event_type

The following are common properties that all events share, as represented by the CommonEventData class:

{
	"data" :
	{
		"event_id" : 6004677780660780000,
		"source_message_id" : "e8335d31-2031-4bff-afec-17ffc1784697",
		"session_id" : 4957918240501247982,
		"session_uuid" : "91b86d0c-86cb-4124-a8b2-edee107de454",
		"timestamp_unixtime_ms" : 1402521613976,
		"location" : {},
		"device_current_state" : {},
		"custom_attributes": {},
		"custom_flags": {}
	},
	"event_type" : "custom_event"
}

The Dotnet SDK represents this structure via an event and an event-data class for each unique event type. For example, CustomEvent which can be populated by a CustomEventData instance.

Custom Events

var customEvent = new CustomEvent("My Custom Event Name", CustomEvent.CustomEventTypeEnum.Location) 
{
    CustomAttributes = new Dictionary<string, string>()
    {
        { "foo", "bar" }
    } 
}  

Screen Events

var screenViewEvent = new ScreenViewEvent("foo screen");            

Commerce Events

var product = new Product("product-id", "product-name") 
{
        TotalProductAmount = new Decimal(123.12),
};
var productAction = new ProductAction(ProductAction.ActionEnum.Purchase) 
{       
        TotalAmount = new Decimal(123.12),
        TransactionId = "foo-transaction-id"
};
var commerceEvent = new CommerceEvent(productAction);

Full Upload Example

Create an EventsApi instance

EventsApi is a Retrofit-compatible interface, allowing you to use the rich feature-set of the Retrofit and OkHttp libraries, such as queueing and asynchronous requests.

Create an API instance with your mParticle workspace credentials. These credentials may be either "platform" (iOS, Android, etc) or "custom feed" keys:

MParticle.Start(new Configuration("API KEY", "API-SECRET"));

The mParticle Events API leverages HTTP basic authentication over TLS.

LogEvents individually

The SDK supports event logging. In this case, we will handle the creation of a Batch, and upload the events on an interval defined by the defined Configuration#UploadInterval (default 60 seconds). You may register a BaseBatch to specify Batch properties that will be included in these uploads.

// Assemble and register your BaseBatch
var batch = new BaseBatch();
batch.Environment = Batch.EnvironmentEnum.Development;
batch.UserIdentities = new BatchUserIdentities() 
{
        CustomerId = "1234",
        Email = "example@foo.com"
};
MParticle.Instance.BaseBatch = batch;

// Create your events
var customAttributes = new Dictionary<string, string>();
customAttributes["foo"] = "bar";
var customEvent = new CustomEvent("foo event", CustomEvent.CustomEventTypeEnum.Location);

var screenEvent = new ScreenViewEvent("foo screen");

// Log events
MParticle.Instance.LogEvent(customEvent);
MParticle.Instance.LogEvent(screenEvent);

//(optional) Force an immediate upload (before the UploadInterval is finished, this will restart the UploadInterval)
MParticle.Instance.Upload();

Perform a Synchronous Upload

The SDK supports both multi-batch ("bulk") or single-batch uploads:

// Assemble your Batch
var batch = new Batch();
batch.Environment = Batch.EnvironmentEnum.Development;
batch.UserIdentities = new BatchUserIdentities() 
{
        CustomerId = "1234",
        Email = "example@foo.com"
};

// Create your events and add to Batch
var customAttributes = new Dictionary<string, string>();
customAttributes["foo"] = "bar";
var customEvent = new CustomEvent("foo event", CustomEvent.CustomEventTypeEnum.Location);
batch.Events.Add(customEvent);

var screenEvent = new ScreenViewEvent("foo screen");

batch.Events.Add(screenEvent);

// Either
// 1) Perform a single upload
var batchResult = MParticle.Instance.UploadBatch(batch);

// 2) Perform a bulk upload
var bulk = new Collection<Batch>();
bulk.Add(batch);
var bulkBatchResult = MParticle.Instance.BulkUploadBatches(bulk);

Perform an Asynchronous Upload

The SDK also supports both async multi-batch ("bulk) or async single-batch uploads

// Assemble your Batch
var batch = new Batch();
batch.Environment = Batch.EnvironmentEnum.Development;
batch.UserIdentities = new BatchUserIdentities() 
{
        CustomerId = "1234",
        Email = "example@foo.com"
};

// Create your events and add to Batch
var customAttributes = new Dictionary<string, string>();
customAttributes["foo"] = "bar";
var customEvent = new CustomEvent("foo event", CustomEvent.CustomEventTypeEnum.Location);

batch.Events.Add(customEvent);

var screenEvent = new ScreenViewEvent("foo screen");

batch.Events.Add(screenEvent);

// Either
// 1) Perform a single upload
var batchResult = MParticle.Instance.UploadBatchAsync(batch)
.ContinueWith(apiResonse =>
        {
                //do something
        }
);

// 2) Perform a bulk upload
var bulk = new Collection<Batch>();
bulk.Add(batch);
MParticle.Instance.BulkUploadBatchesAsync(bulk)
.ContinueWith(apiResponse =>
        {
                //do something
        }
);

License

Apache License 2.0

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.0 26,520 6/28/2022
1.3.0 2,138 1/6/2022
1.2.0 391 10/26/2021
1.1.0 2,068 12/17/2020
1.0.1 422 10/22/2020
1.0.0 632 7/7/2020