eealeivan / mixpanel-csharp

Mixpanel C# integration library
MIT License
63 stars 15 forks source link

Support for EU Data Residency #36

Closed strizzwald closed 1 year ago

strizzwald commented 1 year ago

Mixpanel APIs support two domains, api.mixpanel.com and api-eu.mixpanel.com. The EU domain is used to store event data in Mixpanel's EU data center.

https://help.mixpanel.com/hc/en-us/articles/360039135652-Data-Residency-in-EU

I'm unable to configure the mixpanel-csharp library to send events using the EU domain

eealeivan commented 1 year ago

You can achieve it by providing the corresponding value with MixpanelConfig:

var mc = new MixpanelClient("xxx", new MixpanelConfig
{
     DataResidencyHandling = MixpanelDataResidencyHandling.EU
});

Or alternatively by setting it globally:

MixpanelConfig.Global.DataResidencyHandling = MixpanelDataResidencyHandling.EU;

strizzwald commented 1 year ago

Thanks

strizzwald commented 1 year ago

I'm experiencing another issue where tracking events while a user is still anonymous does not send the distinct_id, even though the mixpanel cookie is set.

Project: .NET Core MVC with Razor Pages Framework: .NET 6.0 mixpanel-csharp: 5.0.0

public async Task OnGetAsync(string returnUrl = null)
        {
            await base.OnGetAsync();

            var mixPanelCookie = HttpContext.Request.Cookies
                                .Where(x => x.Key.EndsWith("_mixpanel", StringComparison.OrdinalIgnoreCase))
                                .Select(x => x.Value)
                                .FirstOrDefault();

            var mixPanelDistinctId = JsonConvert.DeserializeObject<dynamic>(mixPanelCookie).distinct_id;

            var success = await _mixpanelClient.TrackAsync(KnownMixpanelEvent.ViewLoginPage, new Dictionary<string, object>
            {
                { MixpanelProperty.DistinctId, mixPanelDistinctId },
                { MixpanelProperty.Token, _mixpanelProjectToken },
                { MixpanelProperty.Ip, Request.GetIpAddress() }
            });
}

Initially, I tracked the event without setting distinct_id. I thought the library would read the _mixpanel cookie before sending the event to Mixpanel.

This is what that's logged to Mixpanel looks like

image

Both the IP Address and distinct_id are not being logged. Please advise on where I could be going wrong.

Thanks

eealeivan commented 1 year ago

My guess would be that the types of DistinctId and Ip are different from what Mixpanel C# supports. You can see the parsing logic in DistinctIdParser and IpParser.

Most likely just calling ToString method on you variables before passing them to TrackAsync should solve your problems.

You should also consider configuring the error logging

Another approach is to call TrackTest method which will return all the details f building a message including the possible errors. It is very handy to use while debugging.

strizzwald commented 1 year ago

Thanks again @eealeivan calling ToString helped. The distinct_id I was attempting to use was of type dynamic

eealeivan commented 1 year ago

👍