flagsmith icon indicating copy to clipboard operation
flagsmith copied to clipboard

mixpanel integration does not work

Open timhbt opened this issue 4 years ago • 11 comments

Hi guys,

I would like to activate the mixpanel integration. After looking into the code a bit and testing, I may have discovered the error.

The header in the mixpanel request (here) is wrong.

"Content-Type": "application/x-www-form-urlencoded"

should be

"Content-Type": "application/json"

After this change I can fire the curl and it works.

The curl that works:

curl --request POST \
     --url 'https://api.mixpanel.com/engage#profile-set' \
     --header 'accept: text/plain' \
     --header 'content-type: application/json' \
     --data '
[
     {
          "$token": "abcde",
          "$distinct_id": "development_user_123456",
          "$set": {
            "test": "ok"
          }
     }
]
'

timhbt avatar Oct 04 '22 10:10 timhbt

@timhubert thanks for the detailed description, I will have a look

gagantrivedi avatar Oct 04 '22 10:10 gagantrivedi

@timhubert what error are you getting with "Content-Type": "application/x-www-form-urlencoded" header?

curl --request POST \
     --url 'https://api.mixpanel.com/engage#profile-set' \
     --header 'accept: text/plain' \
     --header 'content-type: application/x-www-form-urlencoded' \
     --data '
[
     {
          "$token": "key,
          "$distinct_id": "development_user_123456",
          "$set": {
            "test": "ok"
          }
     }
]
'

Works fine for me (returns 200)?

gagantrivedi avatar Oct 04 '22 11:10 gagantrivedi

@gagantrivedi

'content-type: application/x-www-form-urlencoded' gives me zero ("0") as output and property in mixpanel is not set.

'content-type: application/json' gives 1 as output and property in mixpanel is set.

But both returns 200.

mixpanel-integration

timhbt avatar Oct 04 '22 11:10 timhbt

@gagantrivedi

'content-type: application/x-www-form-urlencoded' gives me zero ("0") as output and property in mixpanel is not set.

'content-type: application/json' gives 1 as output and property in mixpanel is set.

But both returns 200.

mixpanel-integration

Ah, yes

gagantrivedi avatar Oct 04 '22 12:10 gagantrivedi

@gagantrivedi Sorry to have reopened this issue. I saw that you deleted the content-type header in the mixpanel request payload here. So I just tested it with the new Flagsmith version (2.32.1), without success. Also the curl without the content-type header does not work (returns "0" to me).

Adding 'content-type: application/json' should fix this. https://developer.mixpanel.com/reference/profile-set

timhbt avatar Oct 06 '22 13:10 timhbt

@gagantrivedi Sorry to have reopened this issue. I saw that you deleted the content-type header in the mixpanel request payload here. So I just tested it with the new Flagsmith version (2.32.1), without success. Also the curl without the content-type header does not work (returns "0" to me).

Adding 'content-type: application/json' should fix this. https://developer.mixpanel.com/reference/profile-set

Nope, that is not correct. Content-type header is added by request implicitly since I am passing the data using the json kwarg

If you need that header set and you don’t want to encode the dict yourself, you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically:

url = 'https://api.github.com/some/endpoint'

payload = {'some': 'data'}

r = requests.post(url, json=payload)


ref: https://requests.readthedocs.io/en/latest/user/quickstart/#make-a-request

gagantrivedi avatar Oct 06 '22 13:10 gagantrivedi

@timhubert Can you clarify exactly how you are running Flagsmith and how you are testing the Integration?

dabeeeenster avatar Oct 06 '22 13:10 dabeeeenster

I use your SaaS platform with the Edge API. The Flagsmith and Mixpanel users have the same identities / distinct id.

To integrate, I followed this https://docs.flagsmith.com/integrations/mixpanel#integration-setup The Flagsmith environment in which the user exists and the Mixpanel Project Token are entered correctly.

Then I run curl 'https://edge.api.flagsmith.com/api/v1/identities/?identifier=development_user_123' -H 'x-environment-key: XXX' (user's response is correct) and check if the user profile properties (Flagsmith's feature flags) are set in Mixpanel for this user development_user_123. According to the docs, the POST to https://api.mixpanel.com/engage#profile-set should happen on every Flagsmith Identity API call.

timhbt avatar Oct 06 '22 14:10 timhbt

OK we need to push a fix to our Edge API - this will get done tomorrow!

dabeeeenster avatar Oct 06 '22 14:10 dabeeeenster

Thank you guys 👍

timhbt avatar Oct 06 '22 14:10 timhbt

Hi @timhubert , the fix is now deployed on our Edge API (and I just tested it as well). I am going to close this issue now, but feel free to re-open or leave a comment if you face any challenges with it.

gagantrivedi avatar Oct 12 '22 07:10 gagantrivedi