Flagsmith / flagsmith

Open Source Feature Flagging and Remote Config Service. Host on-prem or use our hosted version at https://flagsmith.com/
https://flagsmith.com/
BSD 3-Clause "New" or "Revised" License
4.9k stars 374 forks source link

mixpanel integration does not work #1500

Closed timhbt closed 2 years ago

timhbt commented 2 years ago

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"
          }
     }
]
'
gagantrivedi commented 2 years ago

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

gagantrivedi commented 2 years ago

@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)?

timhbt commented 2 years ago

@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

gagantrivedi commented 2 years ago

@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

timhbt commented 2 years ago

@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

gagantrivedi commented 2 years ago

@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

dabeeeenster commented 2 years ago

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

timhbt commented 2 years ago

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_user123. According to the docs, the POST to https://api.mixpanel.com/engage#profile-set should happen on every Flagsmith Identity API call.

dabeeeenster commented 2 years ago

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

timhbt commented 2 years ago

Thank you guys 👍

gagantrivedi commented 2 years ago

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.