Developers
mParticle’s Xamarin SDK is compatible with iOS and Android apps. The SDK consists of a common C# API layer and two Xamarin binding libraries for mParticle’s native iOS and Android SDKs.
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 Xamarin Add Package dialog. See here for a detailed walkthrough on using NuGet with a Xamarin project.
The mParticle API is exposed via MParticle.Instance
.
The SDK must be initialized with your mParticle workspace key and secret prior to use. This call should be placed in your Xamarin application initialization, as early as possible:
using mParticle.Xamarin;
namespace MyProject
{
public class Example
{
void Example ()
{
//use the correct workspace API key and secret for iOS and Android
string apiKey = "";
string apiSecret = "";
#if __ANDROID__
apiKey = "REPLACE ME ANDROID KEY", ;
apiSecret = "REPLACE ME ANDROID SECRET"
#elif __IOS__
apiKey = "REPLACE ME IOS KEY", ;
apiSecret = "REPLACE ME IOS SECRET"
#endif
new MParticleSDK().Initialize(new MParticleOptions()
{
ApiKey = apiKey,
ApiSecret = apiSecret
});
}
}
The plugin must be included at the top of your file to access the singleton:
using mParticle.Xamarin;
The Xamarin 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 Xamarin examples.
MParticle.Instance.LogEvent (
"Hello world",
EventType.Navigation,
new Dictionary<string, string>{{ "foo", "bar" }}
);
Product[] products = new Product[2];
products[0] = new Product("foo name", "foo sku", 42, 2);
products[0].Brand = "foo brand";
products[0].Category = "foo category";
products[0].CouponCode = "foo coupon";
products[1] = new Product("foo name 2", "foo sku 2", 100, 3);
products[1].Brand = "foo brand 2";
products[1].Category = "foo category 2";
products[1].CouponCode = "foo coupon 2";
TransactionAttributes transactionAttributes = new TransactionAttributes("foo transaction id");
transactionAttributes.Revenue = 180;
transactionAttributes.Shipping = 10;
transactionAttributes.Tax = 15;
transactionAttributes.Affiliation = "foo affiliation";
transactionAttributes.CouponCode = "foo coupon code";
CommerceEvent eCommEvent = new CommerceEvent (
ProductAction.Purchase,
products,
transactionAttributes
);
MParticle.Instance.LogCommerceEvent(eCommEvent);
MParticle.Instance.LogScreenEvent
(
"Test screen",
new Dictionary<string, string>{{ "Test key 1", "Test value 1" }}
);
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.
Android
.aar
file to the Jars folder and set the build action to LibraryProjectZip.POM
of the kit Maven artifact to see if it specifies a transitive dependency. Most kits specify a transitive dependency on a 3rd-party SDK.Download the .jar
or .aar
file of the transitive dependency:
To verify that the kit was successfully detected, look for a string that matches ”[Service Provider Name] kit detected” so for instance “AppsFlyer Detected”
iOS
Additional information on troubleshooting Xamarin bindings can be found here for Android and iOS:
If you do not use NuGet in your Xamarin project, you can build this project manually:
.sln
file in Xamarin Studio, select Release and Build
. Alternatively, run msbuild mParticle.Xamarin.sln /p:Configuration=Release /t:Rebuild
from a terminal.nuget pack
. This will create the NuGet package.In order for attribution, deep linking, and many other integrations to work properly, add the mParticle ReferrerReceiver
to your manifest file within the <application>
tag. The mParticle SDK will collect any campaign referral information and automatically forward it to kits (such as AppsFlyer, Kochava, and Adjust) and server-side integrations.
<receiver android:name="com.mparticle.ReferrerReceiver" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER"/>
</intent-filter>
</receiver>
To test if the install referrer is working, run the following in the terminal:
adb shell
am broadcast -a com.android.vending.INSTALL_REFERRER -n com.companyname.SampleAndroid/com.mparticle.ReferrerReceiver --es "referrer" "utm_source=test_source\&utm_medium=test_medium\&utm_term=test_term\&utm_content=test_content\&utm_campaign=test_name"
Then go to the mParticle Live Stream, click on the Android - Batch, click View Event and you should see your value in the device info.
Was this page helpful?