Getting Started

mParticle’s .NET MAUI SDK is compatible with iOS and Android apps. The SDK consists of two .Net 8.0 libraries for mParticle’s native iOS and Android SDKs.

Starting in version 2.1.0, this SDK requires using .NET 8+ to support MAUI development. See Microsoft’s policy around the end of support for Xamarin.

Installation

This SDK is distributed via the public NuGet gallery: https://www.nuget.org/packages/mParticle.Xamarin/.

If you have both an iOS and an Android project, you should add the NuGet package to each following the Visual Studio Add Package dialog. See here for a detailed walkthrough on using NuGet with Visual Studio.

Initialize the SDK

The SDK must be initialized with your mParticle workspace key and secret prior to use. This call should be placed in your Xamarin or MAUI application initialization, as early as possible:

 //use the correct workspace API key and secret for iOS and Android
 string key = "REPLACE WITH iOS APP KEY";
 string secret = "REPLACE WITH iOS APP SECRET";

MParticle.Instance.Initialize(new MParticleOptions()
{
    InstallType = InstallType.KnownUpgrade,
    Environment = Environment.Development,
    ApiKey = key,
    ApiSecret = secret,
    IdentifyRequest = new IdentityApiRequest()
    {
        UserIdentities = new Dictionary<UserIdentity, string>() {
        //{ UserIdentity.Yahoo, "tom@yahoo.com" },
        { UserIdentity.IOSAdvertiserId, "C56A4180-65AA-42EC-A945-5FD21DEC0538" },
        { UserIdentity.CustomerId, "Other Identity" }
    },
        UserAliasHandler = ((previousUser, newUser) => newUser.SetUserAttributes(previousUser.GetUserAttributes()))
    },
    DevicePerformanceMetricsDisabled = false,
    IdDisabled = false,
    UploadInterval = 650,
    SessionTimeout = 50,
    LogLevel = LogLevel.INFO,
    ConfigMaxAgeSeconds = 60,
    AttributionListener = new AttributionListener()
    {
        OnAttributionError = error => Console.WriteLine("AttributionError\n" + "Error Message = " + error.Message + "\nService Provider = " + error.ServiceProviderId),
        OnAttributionResult = result => Console.WriteLine("AttributionResult\n" + "LinkUrl = " + result.LinkUrl + "\nParameters" + result.Parameters + "\nService Provider" + result.ServiceProviderId)
    },
    LocationTracking = new LocationTracking("GPS", 100, 350, 22),
    PushRegistration = new PushRegistration()
    {
        AndroidSenderId = "12345-abcdefg",
        AndroidInstanceId = "andriod-secret-instance-id",
        IOSToken = "09876654321qwerty"
    }
});

Usage

Events

The Xamarin and MAUI SDK simply provides bindings for mParticle’s iOS and Android SDKs. Refer to the underlying SDK docs for full details on Events. See below for basic implementation examples for MAUI, which all new implementations should be using.

App Events

mparticle.LogEvent("AppEvent of type navigation", EventType.Navigation, new Dictionary<string, string>() { { "Cool", "Beans" } });

Commerce Events

var product = new Product("Chicken", "SomeSku", 123.43, 22.14) { Brand = "mybrand", Category = "mycategory", CouponCode = "mycoupon", Variant = "myvariant", Position = 12, customAttributes = new Dictionary<string, string>() { { "Tk1", "Tv1" }, { "Tk2", "Tv2" } } };
var product2 = new Product("Noodles", "CoolSku", 12.4, 52.4) { Brand = "mybrand2", Category = "mycategory2", CouponCode = "mycoupon2", Variant = "myvariant2", Position = 3, customAttributes = new Dictionary<string, string>() { { "NTk1", "NTv1" }, { "NTk2", "NTv2" } } };
var products = new Product[] { product, product2 };
var transactionAttributes = new TransactionAttributes("transactionId123") { Affiliation = "Some Affiliation", CouponCode = "Winner", Revenue = 400.21, Tax = 434.98, Shipping = 5.13 };

mparticle.LogCommerceEvent(new CommerceEvent(ProductAction.AddToCart, products) { Currency = "AUD" });
mparticle.LogCommerceEvent(new CommerceEvent(ProductAction.Purchase, products, transactionAttributes) { CustomAttributes = new Dictionary<string, string> { { "TestCommerceAttr", "TestCommerceVal" } } });      

Screen events

mparticle.LogScreen("Home Screen", new Dictionary<string, string>() { { "EventAttributeKey", "EventAttributeValue" } });

Kit Integrations

While most of mParticle’s integration are server side, several require additional client side libraries called kits. An mParticle Kit is composed of a class that typically wraps a 3rd-party SDK, and maps the mParticle API onto a that SDK’s API.

If you’d like to use one of these client side kits simply follow Microsofts guide for creating .Net Android and iOS Binding Libraries.

Was this page helpful?