Mixpanel Unity Csharp

MIT
by waltdestler

Mixpanel Analytics integration for Unity3D, C# version.

( Crawled 33 minutes ago )
+

This is the project for the C# version of the Mixpanel plugin for Unity 3D. If you prefer to work in Javascript, then use this project.

Setup

If you want to track user behavior on your Unity3D application, first download the mixpanel-cs.unitypackage file.

Next, import mixpanel-cs.unitypackage into your Unity project by selecting the "Assets" menu, hovering over "Import Package", and clicking "Custom Package".

Import Package

Locate the mixpanel-cs.unitypackage file you just downloaded and click "Open". You will see this window:

Import Package

From this window you can select the individual files you want to import into your Unity project. The only two required files for the API are Mixpanel.cs and LitJson.dll. The example.unity and MixpanelExample.cs files are for tutorial purposes only and not required to use the API. If you want you may uncheck these two optional files or delete them later from your project at any time.

  1. Mixpanel.cs - This is a required file which contains the core Mixpanel API for Unity3D.
  2. LitJson.dll - This is a required file which is used by Mixpanel.js to translate Unity data types into JSON, the language understood by Mixpanel.
  3. MixpanelExample.cs - This is an optional file containing example source code demonstrating how to use the Mixpanel API. This tutorial is similar to the contents of this file, but doesn't follow it exactly.
  4. example.unity - This is an optional scene file containing an object with the MixpanelExample.cs script attached. You can open the scene in the Unity editor and test out the API from this scene.

Click the "Import" button to finish importing the Mixpanel API into your project.

As an alternative to simply downloading mixpanel-cs.unitypackage, you may also clone the git repository, in which case you can simply copy the API files from the repository:

git clone https://github.com/waltdestler/Mixpanel-Unity-CSharp.git

This repository contains an entire Unity3D project, which is set in a mode to work with version control systems such as Git. This mode automatically creates additional Mixpanel.cs.meta, LitJson.dll.meta, MixpanelExample.cs.meta, and example.unity.meta files. You do not need to copy these additional ".meta" files into your own project.

Initializing Mixpanel

The first thing you need to do in order to use Mixpanel is to initialize the Mixpanel class. At the very least you must set your unique Mixpanel.Token string. We recommend doing this in the Start method of a script in the very first scene of your application.

public void Start()
{
    Mixpanel.Token = "YOUR MIXPANEL TOKEN HERE";
}

Tracking Events

After initializing the Mixpanel class, you are ready to track events. Sending an event with no properties is very easy, and you can do this from anywhere in any of your scripts:

Mixpanel.SendEvent("name of event");

If you want to add properties to the event you can do the following:

Mixpanel.SendEvent("name of event", new Dictionary<string, object> {
    {"name of property 1", value_of_property1}, // Most common types such as int, float, double, and string are supported.
    {"name of property 2", value_of_property2},
});

Super Properties

Super properties are an easy way to store data about your users. They are global properties that get attached to everything you are tracking. More information can be found here.

If you want to register a super property for your current user, you can do it as follows:

Mixpanel.SuperProperties.Add("name of super_property", value_of_super_property); // Most common types such as int, float, double, and string are supported.

It's often useful to log data about your application with all events. For example, we can add some super properties to the Start method we created earlier:

public void Start()
{
    Mixpanel.Token = "YOUR MIXPANEL TOKEN HERE";

    // Set some "super properties" to be sent with every event.
    Mixpanel.SuperProperties.Add("platform", Application.platform.ToString());
    Mixpanel.SuperProperties.Add("quality", QualitySettings.names[QualitySettings.GetQualityLevel()]);
    Mixpanel.SuperProperties.Add("fullscreen", Screen.fullScreen);
    Mixpanel.SuperProperties.Add("resolution", Screen.width + "x" + Screen.height);
}

Identifying a User

By default, your users are identified by a randomly generated "globally unique identifier" that is stored on their computer or device. You can easily change the identifier with the following call:

Mixpanel.DistinctID = "the distinct ID string of your user";

Typically this would be done in that Start method, so our Start method from earlier could be further expanded like this:

public void Start()
{
    Mixpanel.Token = "YOUR MIXPANEL TOKEN HERE";
    Mixpanel.DistinctID = "the distinct ID string of your user";

    // Set some "super properties" to be sent with every event.
    Mixpanel.SuperProperties.Add("platform", Application.platform.ToString());
    Mixpanel.SuperProperties.Add("quality", QualitySettings.names[QualitySettings.GetQualityLevel()]);
    Mixpanel.SuperProperties.Add("fullscreen", Screen.fullScreen);
    Mixpanel.SuperProperties.Add("resolution", Screen.width + "x" + Screen.height);
}