Sticky MPKnowledgeDrop

Use HTTP Spec to send data to Mixpanel

  • 19 March 2020
  • 6 replies
  • 744 views
Use HTTP Spec to send data to Mixpanel
Userlevel 6
Badge +4

Every week, we will release tips to help you get the most out of Mixpanel. Want to see more? Click here to see other #mpknowledgedrop articles.

 

Sometimes using new tools can be a bit intimidating. I know they are for me, especially if I have no idea how they work! Today we will be walking through how to send an event and user profile through an HTTP request. 

 

Before we go through this tutorial, here are some prerequisites and systems you will need:  

Prerequisite:  JSON(JavaScript Object Notation), Browser (ie Safari, Chrome, Mozilla), Base64 encoded, Mixpanel test project

Systems I am using: MacOS Mojave, Chrome Browser

Reference Guide: HTTP Spec

 

Now let’s get started sending in user profiles and events!

 

Create or Update User Profiles

When using the HTTP spec for user profiles, you can either create or update a user’s profile properties.

In this example we will create a new user profile. (if you want to know how update an existing user profile, skip to step 6)

  1. First we need a JSON of the User profile we will be creating. 
    Example:
    Fill out the values or add other user property values you want to send.

{

   "$token": “PROJECT_TOKEN",

   "$distinct_id": "123",

   "$set": {

       "$email": "testtesttest@gmail.com"

   }

}

Note: $token and $distinct_id are required!

Here is a screenshot of the JSON I am using - I blocked out my project token :)
fvo2RT_We0PVIId2sxBFeCVcDbXSRiSEkBx7r730uzbk99LU5qFSMwYLafk1W6kaT2WuKJdPSgX2Hpv5B0HWMQ5kEFOK3f4XnHMRpzKsRm7ejKFeB7RLabvYEv_l-FDY2Rogn61K

  1. Next we need to copy and paste the JSON to the following base64encode website (https://www.base64encode.org/). This will give us the encoded value we will need to paste in into the engage endpoint(URL).JX0KHGBNr4jH8tazqvO8mJgO10-Vhv3WHuub26loafVGGmhHQqYsuyzswMXyH3b33K7h-0TdvFsIB4y1DWQcW6F4b0MoiWiekQVK3ajLpRygIGATx_DrDZFuJipJt9aikTKMnAdW

 

  1. Now we need to attach the encoded characters to the engage endpoint(URL)

    Engage endpoint: https://api.mixpanel.com/engage/?data=

    Encoded Characters: ewogICAiJHRva2VuIjogIjJmNzIyMDYwMmUzZGE0MzE5ZjY0MzZjM2E0MmYzY2UxIiwKICAgIiRkaXN0aW5jdF9pZCI6ICIxMjMiLAogICAiJHNldCI6IHsKICAgICAgICIkZW1haWwiOiAidGVzdHRlc3R0ZXN0QGdtYWlsLmNvbSIKICAgfQp9

    Put together:  https://api.mixpanel.com/engage/?data=ewogICAiJHRva2VuIjogIjJmNzIyMDYwMmUzZGE0MzE5ZjY0MzZjM2E0MmYzY2UxIiwKICAgIiRkaXN0aW5jdF9pZCI6ICIxMjMiLAogICAiJHNldCI6IHsKICAgICAgICIkZW1haWwiOiAidGVzdHRlc3R0ZXN0QGdtYWlsLmNvbSIKICAgfQp9

 

  1. Now we can go to our Web browser, paste the engage endpoint(Put together URL)in the url box, and press enter. The screen will then return a value of 1 or 0.

    If it shows 1, this means your data points were sent to Mixpanel successfully. B5AB6qjFaBRUVT2An7kRstc7gzooFzhZMlhOosODwle0BOLgDWmpM_ES5VHx3owtwbwh2IEFsVV8DLcINFnTVtMZpb6OUAD_NgduR0083354MydfH0Kscju3bmeN_tqDu68w6Wlc
    If it is a 0, this means your request has failed.
    syeV-RfLbna9ZnbYXQnuwDMUS_W9et8ZjqD5x6ZRRXxELFYYG5o-SZ4lWuQekEpFOkI4t71u1ytJxJH4NIwAASwoRsLF9OxgYWshRxKtyJoPyr3s6ysWHT00pmHva0cHV263dY9i

    If your post has failed, you want to repeat steps 1-4. Something to check is that the JSON is in the correct format. You can use this online checker to look at your JSON format.  Also, something I noticed is that when I copy a JSON from a google doc, the double quotes format changes. So I usually go back and redo the double quote marks (“_”) and re-check the format and values. Lastly, make sure your project token is correct and other property values. 

 

  1. If your profile was sent successfully, you can now go into your Explore Report and see the user profile there. If you don’t see it right away, wait a few minutes to refresh! There might be a slight ingestion delay. 

6btEKL9LzRCWH85YpoR0zhcfPfgdFEo30J8QajC0Q_vp4m_-YRuy1AHOYz7HZ7RfJbpURosp5VEX-h6lSJyh89fGRiH93Eyj0AsRy35LZbZzxEGAn89eY9Kx3bRA8ltpnmbXgZ9o

F0NV_TICCBWdWu9X5BKYyjlh2hgqNeUXZWcDgbzMqEj2YavnOM43O6Fg_5SsWsx5qNe5pB4iahcZj2COq9Cat1CVy8t29el283rG3NzFZR6K1lUlYtNUHlXVAGz5RsC7xod0YHii

  1. If you want to update user profiles, steps 1-4 are the same, the only thing you will need to be mindful of is to  call "$ignore_alias": true with the JSON along with the updated values you want to send to the profile. An example on how this will look like is below 
{
"$token": "PROJECT_TOKEN",

   "$distinct_id": “123",

   "$set": {

       "$email": "testtesttest@gmail.com"

   },

   "$ignore_alias": true

}

 

Something to note: You need to make sure that the $distinct_id value matches the user you want to update. Here is a help article on how to find an existing users distinct ID in the explore report.



Send Events

Before we dive into sending events, it is important to emphasize that all events are immutable once they are sent to Mixpanel. This means that, you cannot update or alter an event that has already been sent in. That being said, you can send new events, up to 5 days old, via HTTP spec.
 

If you want to send in events that are more than five days old, you will need to use the import endpoint at http://api.mixpanel.com/import/ to process your request. Full details on using the import endpoint can be found here.
 

Now let’s get started!

  1. First, we need a JSON object for the event we want to send to Mixpanel.
     

Example: 

{

    "event": "Signed Up",

    "properties": {

        "distinct_id": "123",

        "token": "PROJECT_TOKEN",

        "Referred By": "Friend",

  "$email": "testtesttest@gmail.com",

  "$name": "John Doe"

    }

}

Note: Event and properties are required!

 

Here is a screenshot of the JSON I am using - I blocked out my project token again :)

ECK9Qefz_744IKCTUfn0bnIuynvmn0cy4J4egI5SBwsXdA1lveLattl3XpQKdbtnrwHFq6kAgkc4ujl2RsRNITsObvsCUTWccP2V-MR-vO8Hy4sY01zWN-Y5qiYKxoY29XFaxdKY
 

  1. Next,  copy and paste the JSON to the following base64 encode website (https://www.base64encode.org/). This will give us the encoded value we will need to paste into the track endpoint(URL).-Y0CojtV_BFneI2Ywl1-pXlorjrCiosHlPa9t9ETXOA6THaAPWVXowgQASh0k3OEv40AXXHOUzpJDpYEcaMiYmIRuv-IrDZcOsxZE09cHbndxFC4BJQdaRvp08PyzM76xjyeI_7w

 

  1. Now we need to attach the encoded characters to the track endpoint(URL)

    Track endpoint: https://api.mixpanel.com/track/?data=

    Encoded Characters: ewogICAgImV2ZW50IjogIlNpZ25lZCBVcCIsCiAgICAicHJvcGVydGllcyI6IHsKICAgICAgICAiZGlzdGluY3RfaWQiOiAiMTIzIiwKICAgICAgICAidG9rZW4iOiAiMmY3MjIwNjAyZTNkYTQzMTlmNjQzNmMzYTQyZjNjZTEiLAogICAgICAgICJSZWZlcnJlZCBCeSI6ICJGcmllbmQiLAogICAgICAgICIkZW1haWwiOiAidGVzdHRlc3R0ZXN0QGdtYWlsLmNvbSIsCiAgICAgICAgIiRuYW1lIjogIlN0ZXBoYW5pZSIKICAgIH0KfQ==

    Put together:  https://api.mixpanel.com/track/?data=ewogICAgImV2ZW50IjogIlNpZ25lZCBVcCIsCiAgICAicHJvcGVydGllcyI6IHsKICAgICAgICAiZGlzdGluY3RfaWQiOiAiMTIzIiwKICAgICAgICAidG9rZW4iOiAiMmY3MjIwNjAyZTNkYTQzMTlmNjQzNmMzYTQyZjNjZTEiLAogICAgICAgICJSZWZlcnJlZCBCeSI6ICJGcmllbmQiLAogICAgICAgICIkZW1haWwiOiAidGVzdHRlc3R0ZXN0QGdtYWlsLmNvbSIsCiAgICAgICAgIiRuYW1lIjogIlN0ZXBoYW5pZSIKICAgIH0KfQ==

 

  1. Now we can go to our Web browser, paste the engage endpoint(Put together URL)in the url box, and press enter. The screen will then return a value of 1 or 0.

    If it shows 1, this means your data points were sent to Mixpanel successfully.
    OeALjIBgCsmCxfMoUwq0M_E7Vb2pwTZKvbnmN4EDnR-sE00b6dkjvSBesHDvrqGjxrH11Oy-pubK3z6adQTN1zyCpsQW2zcn2bPOBF45Qd45a8LP9UbxQukbmtQGF2rc7LqiSY_g
    If it is a 0, this means your request has failed.
    syeV-RfLbna9ZnbYXQnuwDMUS_W9et8ZjqD5x6ZRRXxELFYYG5o-SZ4lWuQekEpFOkI4t71u1ytJxJH4NIwAASwoRsLF9OxgYWshRxKtyJoPyr3s6ysWHT00pmHva0cHV263dY9i
    If your post has failed, you want to repeat steps 1-4. Something to check is that the JSON is in the correct format. You can use this online checker to look at your JSON format. 
    Also, something I noticed is that when I copy a JSON from a google doc, the double quotes format changes. So I usually go back and redo the double quote marks (“_”) and re-check the format and values. Lastly, make sure your project token is correct and other property values as well. 

 

  1. If your Event was sent successfully, you can now go into your Live View and see the event there. Below I shared a screenshot of my Live View — you can see that the event was successfully sent to Mixpanel. 

7Kv3CvDJ71GG-JXNDRz9P1tXLBqq0I5o-ghvJX9SWl1nL2mF2NsyyFWOaRQ8du73ce-ZiImMmFeLFWHIGxh87QiATqJ-7ATeeJM94Qjx_51vJgCJJNFATjNSx2oAOLRs22yLLDCp

Congrats, you made to the end!

 

I hope this helped you learn how to send in user profiles and events via HTTP Spec! My walkthrough is designed to give you a simple example so you can  learn and test out the process. I would highly recommend going through our HTTP Spec Dev docs, which goes into much higher detail on other things you can do with HTTP Spec! 

 

Comment below if you have other tips on the HTTP Spec!


6 replies

Following the tutorial on how to update the user profile, i noticed it also updates the location of that user to wherever I called the Mixpanel API from. Is there a way to prevent that?

 

I want to add properties to my user from my backend-server, but I don’t want to lose the location information Mixpanel has tracked.

 

It also updates the user’s Last Seen timestamp which i dont want changed.

 

Any suggestions on how I can achieve this?

Userlevel 6
Badge +4

Hi @zmasta94,

 

In your JSON, you can include ip=0 and $ignore_time=true so it will not update the IP address and “Last seen” property when you send in an update to your user profiles.

Using my example shared on User profiles, the JSON would look like this,

 

{
"$token": "36ada5b10da39a1347559321baf13063",

"$distinct_id": "123",
"$ip": "0",
"$ignore_time": true,

"$set": {

"$email": "testtesttest@gmail.com"

},

"$ignore_alias": true

}

 

More information on attributes here, under storing user profiles. 

Hope this helps!

Badge +3

Hi @stephanie 

 

I am trying to do the same using the updated HTTP API but I am unable to log this in to mixpanel.

 

Could you help me identify what I am doing wrong.

 

It is just not logging in to Mixpanel although I am getting 200 status.

 

I have rechecked Project token as well.

 

Here is a part of my code:

    var event = {
      "data":{
        "event": "Test", 
        "properties":           {
          "token": devToken,
          "time": nonce,          
          "distinct_id": userId,
          "$first_name": FirstNameValue,
          "$last_name": LastNameValue,
          "$phone": PhoneValue,
          "$email": EmailValue,
          "$region": StateValue,
          "Import Property Name": PropertyNameValue,
          "Import Initial Source": InitialSourceValue,
          "Import Listing URL": ListingURLValue,
          "Import Website": WebsiteValue,
          "Import Social Media": SocialMediaValue,
          "Import onX Waypoint URL": onXWaypointURLValue,
          "Import Acres": AcresValue,
          "Import Tags": TagsValue,
          "Import Notes": NotesValue,
          "Import Address": AddressValue,
          "Import Date": dateNow,
          "Import Sheet Name": SheetName,
          "Import File Name": FileName,
        }
      }
    }

    var EncodeCharset = Utilities.Charset.UTF_8;
    var eventDecoded = JSON.stringify(event);
    
    var eventEncoded = Utilities.base64Encode(eventDecoded,EncodeCharset);

    
    var eventOptions = {
      method: 'POST',
      headers: headers,
      body: eventEncoded
    }

 

    var eventResponse = UrlFetchApp.fetch('https://api.mixpanel.com/track#live-event',eventOptions);

Userlevel 3
Badge +3

@Affan ,

public async track(event: ICDPEvents, properties: Partial<ICDPEventsProperties>) {

const data = Buffer.from(

JSON.stringify({

event,

properties: {

token: this.token,

...properties,

},

})

).toString("base64");

 

return (await fetch("https://api.mixpanel.com/track/?data=" + data)).text();

}

Badge +3

@robin Are are using an old API documentation since the current documentation has different end points:

https://developer.mixpanel.com/reference/events#track-event

 

Could anyone share a working code based on the updated documentation?

Hi there,

 

I’m using insomnia with Mixpanel API, and I not being able to track events.

I’m using endpoint: https://api-eu.mixpanel.com/track#live-event (I also tested with https://api.mixpanel.com/track#live-event)

My Json Body is: 

{

"event": "Testing",
"properties": {
"distinct_id": "123",
"token": "My_PROJECT_TOKEN"
}

}

 

And i’m using in my headers the Authorization with my  username and secret combined to base64

https://prnt.sc/vp5b4u

 

the response is always 0, meaning that something is invalid in the data.

 

Can someone please help me?

Thanks

Reply