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)
-
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 :)
- 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).
- 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
- 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.
If it is a 0, this means your request has failed.
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.
- 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.
- 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!
-
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 :)
- 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).
- 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==
- 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.
If it is a 0, this means your request has failed.
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.
- 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.
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!