Sticky MPKnowledgeDrop

Use HTTP Spec to send data to Mixpanel

  • 19 March 2020
  • 2 replies
  • 154 views
Use HTTP Spec to send data to Mixpanel
Userlevel 5
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 :)
kapKihrF5s1V_N-aqBfBcg-mQolA7GiOTKwjDJtnKE63JF_sZakR99lcn_SeWQN1HI1WBcBFi6J5fBZjv3kH7gyLkI-jTPx6X3EeGj4kbNLewJJ8rpqauhPS0KyWXWr94t1RA4b9
 

  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).
    La0qLyrapYCXtR29aJlDlPNEtBl_gGy5fUe5tCX77NltBLN6Avw4KOQoXRcSnhhKLycj7lPfb6XeS2WAyUV-ndqXibB03H7cqPYY8IYDbStHrS_P786KpYnoTlASA2KdSSZLkDbc

 

  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. zKnEcS739qrKFoiS7Ep5tGrEBv8GnT_WdkykvabZzU1fENOpx-45467SZFcKBW-MY8oE2s5_J2zOQ-Ecs63-Q4feMKaCw4IAqJinyRLFa89hOXGbMKcOUEuZOGiAi-VJmw9BS6x5
    If it is a 0, this means your request has failed.
    Z1IL1y3r66pgVHcqnWQ7yWCuG-BTav3KdzlRxK2alRAdaSoT-ZkslYimR9aZuVH7ZyZii48aQl5ABRcmqGH-VcaOK3Bexn3xzdzjoLUOtsncQhSeAlxlsgcbhy_wj4Z5ZIektd7U

    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. 

UsC03wyWnVr5PGXWGBvgayYZS1CYHX3wGeZTiR9DlEFhqOd3kSW7nRrLMm7sRPVF_w6XmapaqHM8FTukTpnW4gRm13w8_jit00WNYda-lAfedOKFA25yoKWPnBg2wFY7Vkvw_OwJ

QKozqn1HVRocj4iUgZ-mFoYFUAD0UAdC_c4aY_w-fVfg1gz5jKrd_YEIHREh5DLjggKPlak8Rojn9mRbbcXZGN3sTYbNV_z-kvi-X7OCdxMOS1USRK5saxRS18h_6IsevmCy6-Pj

  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 :)

8n5vdH-RDsNY7LTaWEYd5e-loAn-4L7IqTOOZql00Bymlysnx1-0f1B82tZ_qZgabOfFgBZqiDHb68LtPH34UDq6vWFBGU6LgXVNyAV4F9PkRcTTIjqDflxEZSkInZI6skdGIecz

 

  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).
    2rmhCwT4ocZG25WmoUeFquM168kKwW0RPJrqFBy-Q06o8JIO1x54fTajnjnWBBfSoCP6V_wg2FkSH70rH2-DU9NQcMRgWMV3v-Sg5g2Iz-oc7UMfV3Ujflo8HSai6iOv8mTxJKbN

 

  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.
    yKUTJdZTtFrnlm-ZSpd6ghT8xLPTq67veKcjOcvbexRXq3j0zDcMVxT4QL4TD7SSdBC02bUrPsTBFsmY-8KgPnWBaWcldL8cyPu4JvN_Qcm2gjj1AobKAeba76lJNDJoAzRAswTe
    If it is a 0, this means your request has failed.
    Z1IL1y3r66pgVHcqnWQ7yWCuG-BTav3KdzlRxK2alRAdaSoT-ZkslYimR9aZuVH7ZyZii48aQl5ABRcmqGH-VcaOK3Bexn3xzdzjoLUOtsncQhSeAlxlsgcbhy_wj4Z5ZIektd7U
    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. 

GHdnYjVxGWjFGo-pS5inanPOxMfV2qLWjrM71p7gYYw1ZFPEAy-KtPqHJlMQt9PMEgwbWPn3cwJzDTToQAbwvdOc3zZLDZNuIKe_7j2Fwd_bQWPMvguKjPAe7Lz4DQK60zosppFq

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!


2 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 5
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!

Reply