Search code examples
pythonmixpanel

base64 encode a JSON for Mixpanel API pixel tracker


According to Mixpanel doc, I need to base64 my json array before sending it to their API endpoint

json = {"event": "e-mail opened", 
        "properties": {
        "distinct_id": "28224", 
        "token": "494f5d201963457e632d463d1d4745e4", 
        "time": int(time.time()), 
        "campaign": "gameweek 27"
            }
    }

Per the doc, I need to send it like that

http://api.mixpanel.com/track/?data=[BASE_64_JSON_EVENT]&ip=1&img=1

Here's my python code to convert the dict to base64 and send it. I get

data = base64.b64encode(json.dumps(json)) url = 'http://api.mixpanel.com/track/?data=%d&ip=1&img=1'%data requests.get(url)

My problem is that it doesn't track anything. If I convert the same dict using this online converter and construct the url manually it work.

So there's probably something who's not working in my encoding but what?

Thanks!


Solution

  • Not sure if it is a typo in the question, but instead of:

    url = 'http://api.mixpanel.com/track/?data=%d&ip=1&img=1'%data
    

    it should be

    url = 'http://api.mixpanel.com/track/?data=%s&ip=1&img=1'%data
    

    as the %d expects a number, not a string, like %s does