Skip to content
Aleksandr Ivanov edited this page Nov 11, 2022 · 4 revisions

There are separate methods for all Mixpanel message types. For each message type there is in turn set of methods that allows you to do different stuff.

Method types

Send message immediately

You pass message data and it will immediately send it to Mixpanel. The return type is bool which indicates if everything went well or not. If return value is false that can mean different things: For example, that there was a validation error, or that something failed in mixpanel-scharp, or that Mixpanel returned 0 instead of 1 in response. Add error logging or use Test methods for better control.

Retrieve a message instance

You pass message data and receive a generated message object that is ready to be serialized and sent to Mixpanel.

Test a message

You pass a message data and receive an object that contains all steps of building a message: message data presented as IDictionary<string, object>, data serialized as JSON, JSON encoded as Base64 and exception details if there was some error.

Methods

Track

Task<bool> TrackAsync(string @event, object properties, CancellationToken cancellationToken)
Task<bool> TrackAsync(string @event, object distinctId, object properties, CancellationToken cancellationToken)
MixpanelMessage GetTrackMessage(string @event, object properties)
MixpanelMessage GetTrackMessage(string @event, object distinctId, object properties)
MixpanelMessageTest TrackTest(string @event, object properties)
MixpanelMessageTest TrackTest(string @event, object distinctId, object properties)

Example:

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
await mc.TrackAsync("Level Complete", new {
    DistinctId = "12345",
    Ip = "111.111.111.111",
    LevelNumber = 5,
    LevelName = "Super hard level"
});

JSON that will be sent to https://api.mixpanel.com/track/:

{
  "event": "Level Complete",
  "properties": {
    "token": "e3bc4100330c35722740fb8c6f5abddc",
    "distinct_id": "12345",
    "ip": "111.111.111.111",
    "LevelNumber": 5,
    "LevelName": "Super hard level"
  }
}

Alias

Task<bool> AliasAsync(object alias, CancellationToken cancellationToken)
Task<bool> AliasAsync(object distinctId, object alias, CancellationToken cancellationToken)
MixpanelMessage GetAliasMessage(object alias)
MixpanelMessage GetAliasMessage(object distinctId, object alias)
MixpanelMessageTest AliasTest(object distinctId, object alias)
MixpanelMessageTest AliasTest(object alias)

Example:

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
await mc.AliasAsync("12345", "67890");

JSON that will be sent to https://api.mixpanel.com/track/:

{
    "event": "$create_alias",
    "properties": {
        "token": "e3bc4100330c35722740fb8c6f5abddc",
        "distinct_id": "12345",
        "alias": "67890"
    }
}

People Set

Task<bool> PeopleSetAsync(object properties, CancellationToken cancellationToken)
Task<bool> PeopleSetAsync(object distinctId, object properties, CancellationToken cancellationToken)
MixpanelMessage GetPeopleSetMessage(object properties)
MixpanelMessage GetPeopleSetMessage(object distinctId, object properties)
MixpanelMessageTest PeopleSetTest(object properties)
MixpanelMessageTest PeopleSetTest(object distinctId, object properties)

Example:

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
await mc.PeopleSetAsync(new {
    DistinctId = "12345",
    Ip = "111.111.111.111",
    FirstName = "Darth",
    LastName = "Vader",
    Name = "Darth Vader",
    Email = "[email protected]",
    Phone = "123456",
    Sex = "M",
    Kills = 215
});

JSON that will be sent to https://api.mixpanel.com/engage/:

{
    "$token": "e3bc4100330c35722740fb8c6f5abddc",
    "$distinct_id": "12345",
    "$ip": "111.111.111.111",
    "$set": {
        "$first_name": "Darth",
        "$last_name": "Vader",
        "$name": "Darth Vader",
        "$email": "[email protected]",
        "$phone": "123456",
        "Sex": "M",
        "Kills": 215
    }
}

People Set Once

Task<bool> PeopleSetOnceAsync(object properties, CancellationToken cancellationToken);
Task<bool> PeopleSetOnceAsync(object distinctId, object properties, CancellationToken cancellationToken)
MixpanelMessage GetPeopleSetOnceMessage(object properties)
MixpanelMessage GetPeopleSetOnceMessage(object distinctId, object properties)
MixpanelMessageTest PeopleSetOnceTest(object properties)
MixpanelMessageTest PeopleSetOnceTest(object distinctId, object properties)

Example:

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
mc.PeopleSetOnce(new {
    DistinctId = "12345",
    FirstLogin = new DateTime(2018, 10, 22, 0, 0, 0, DateTimeKind.Utc)
});

JSON that will be sent to https://api.mixpanel.com/engage/:

{
    "$token": "e3bc4100330c35722740fb8c6f5abddc",
    "$distinct_id": "12345",
    "$set_once": {
        "FirstLogin": "2018-10-22T00:00:00"
    }
}

People Add

Task<bool> PeopleAddAsync(object properties, CancellationToken cancellationToken)
Task<bool> PeopleAddAsync(object distinctId, object properties, CancellationToken cancellationToken)
MixpanelMessage GetPeopleAddMessage(object properties)
MixpanelMessage GetPeopleAddMessage(object distinctId, object properties)
MixpanelMessageTest PeopleAddTest(object properties)
MixpanelMessageTest PeopleAddTest(object distinctId, object properties)

When parsing properties object only numeric properties will be processed, all other types will be ignored (except DistinctId).

Example:

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
mc.PeopleAdd(new {
    DistinctId = "12345",
    TransactionsCount = 1,
    MoneySpent = 24.99M
});

JSON that will be sent to http://api.mixpanel.com/engage/:

{
    "$token": "e3bc4100330c35722740fb8c6f5abddc",
    "$distinct_id": "12345",
    "$add": {
        "TransactionsCount": 1,
        "MoneySpent": 24.99
    }
}

People Append

Task<bool> PeopleAppendAsync(object properties, CancellationToken cancellationToken)
Task<bool> PeopleAppendAsync(object distinctId, object properties, CancellationToken cancellationToken)
MixpanelMessage GetPeopleAppendMessage(object properties)
MixpanelMessage GetPeopleAppendMessage(object distinctId, object properties)
MixpanelMessageTest PeopleAppendTest(object properties)
MixpanelMessageTest PeopleAppendTest(object distinctId, object properties)

Example:

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
mc.PeopleAppend(new {
    DistinctId = "12345",
    PowerUps = "Bubble Lead",
    ItemIds = 1568
});

JSON that will be sent to http://api.mixpanel.com/engage/:

{
    "$token": "e3bc4100330c35722740fb8c6f5abddc",
    "$distinct_id": "12345",
    "$append": {
        "PowerUps": "Bubble Lead",
        "ItemIds": 1568
    }
}

People Union

Task<bool> PeopleUnionAsync(object properties, CancellationToken cancellationToken)
Task<bool> PeopleUnionAsync(object distinctId, object properties, CancellationToken cancellationToken)
MixpanelMessage GetPeopleUnionMessage(object properties)
MixpanelMessage GetPeopleUnionMessage(object distinctId, object properties)
MixpanelMessageTest PeopleUnionTest(object properties)
MixpanelMessageTest PeopleUnionTest(object distinctId, object properties)

When parsing properties object only collection type properties will be processed, all other types will be ignored (except DistinctId).

Example:

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
mc.PeopleUnion(new {
    DistinctId = "12345",
    PowerUps = new [] { "Bubble Lead", "Super Mushroom" },
    ItemIds = new []  { 1568, 7653 }
});

JSON that will be sent to http://api.mixpanel.com/engage/:

{
    "$token": "e3bc4100330c35722740fb8c6f5abddc",
    "$distinct_id": "12345",
    "$union": {
        "PowerUps": ["Bubble Lead", "Super Mushroom"],
        "ItemIds": [1568, 7653]
    }
}

People Unset

Task<bool> PeopleUnsetAsync(IEnumerable<string> propertyNames, CancellationToken cancellationToken)
Task<bool> PeopleUnsetAsync(object distinctId, IEnumerable<string> propertyNames, CancellationToken cancellationToken)
MixpanelMessage GetPeopleUnsetMessage(IEnumerable<string> propertyNames)
MixpanelMessage GetPeopleUnsetMessage(object distinctId, IEnumerable<string> propertyNames)
MixpanelMessageTest PeopleUnsetTest(IEnumerable<string> propertyNames)
MixpanelMessageTest PeopleUnsetTest(object distinctId, IEnumerable<string> propertyNames)

Example:

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
mc.PeopleUnset("12345", new [] { "Days Overdue" });

JSON that will be sent to http://api.mixpanel.com/engage/:

{
    "$token": "e3bc4100330c35722740fb8c6f5abddc",
    "$distinct_id": "12345",
    "$unset": ["Days Overdue"]
}

People Delete

Task<bool> PeopleDeleteAsync(bool ignoreAlias = false, CancellationToken cancellationToken)
Task<bool> PeopleDeleteAsync(object distinctId, bool ignoreAlias = false, CancellationToken cancellationToken)
MixpanelMessage GetPeopleDeleteMessage(bool ignoreAlias = false)
MixpanelMessage GetPeopleDeleteMessage(object distinctId, bool ignoreAlias = false)
MixpanelMessageTest PeopleDeleteTest(bool ignoreAlias = false)
MixpanelMessageTest PeopleDeleteTest(object distinctId, bool ignoreAlias = false)

Example:

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
mc.PeopleDelete("12345");

JSON that will be sent to http://api.mixpanel.com/engage/:

{
    "$token": "e3bc4100330c35722740fb8c6f5abddc",
    "$distinct_id": "12345",
    "$delete": ""
}

People Track Charge

Task<bool> PeopleTrackChargeAsync(decimal amount, CancellationToken cancellationToken)
Task<bool> PeopleTrackChargeAsync(object distinctId, decimal amount, CancellationToken cancellationToken)
Task<bool> PeopleTrackChargeAsync(decimal amount, DateTime time, CancellationToken cancellationToken)
Task<bool> PeopleTrackChargeAsync(object distinctId, decimal amount, DateTime time, CancellationToken cancellationToken)
MixpanelMessage GetPeopleTrackChargeMessage(decimal amount)
MixpanelMessage GetPeopleTrackChargeMessage(object distinctId, decimal amount)
MixpanelMessage GetPeopleTrackChargeMessage(decimal amount, DateTime time)
MixpanelMessage GetPeopleTrackChargeMessage(object distinctId, decimal amount, DateTime time)
MixpanelMessageTest PeopleTrackChargeTest(decimal amount)
MixpanelMessageTest PeopleTrackChargeTest(object distinctId, decimal amount)
MixpanelMessageTest PeopleTrackChargeTest(decimal amount, DateTime time)
MixpanelMessageTest PeopleTrackChargeTest(object distinctId, decimal amount, DateTime time)

Example:

var mc = new MixpanelClient("e3bc4100330c35722740fb8c6f5abddc");
mc.PeopleTrackCharge("12345", 24.99M, new DateTime(2014, 10, 22, 0, 0, 0, DateTimeKind.Utc));

JSON that will be sent to http://api.mixpanel.com/engage/:

{
    "$token": "e3bc4100330c35722740fb8c6f5abddc",
    "$distinct_id": "12345",
    "$append": {
        "$transactions": {
            "$amount": 24.99,
            "$time": "2014-10-22T00:00:00"
        }
    }
}

Send

Using this method you can send one or more messages that you retrieved from Get*Message methods. If messages contains both track (Track and Alias) and engage (People*) message types, then they will be divided in 2 batches and will be sent separately. If amount of messages of one type exceeds 50, then messages will be divided in batches and also will be sent separately.

Task<SendResult> SendAsync(IEnumerable<MixpanelMessage> messages, CancellationToken cancellationToken)
ReadOnlyCollection<MixpanelBatchMessageTest> SendTest(IEnumerable<MixpanelMessage> messages)