Mixpanel’s client-side SDKs automatically send City, Region, and Country properties when a user triggers an event or a profile property update. But is still possible to track users’ location data when you send server-side updates.
Geolocation data properties (City, Region, and Country) can be set in two ways: through the IP ($ip) property, or through the Latitude ($latitude) and Longitude ($longitude) properties
The Geo Source ($geo_source) user profile property describes the method you used to establish the values for the location properties. If the value is null, the location properties were determined through the IP ($ip) property. If the value is “reverse_geocoding”, then the location properties were determined through the Latitude ($latitude) and Longitude ($longitude) properties.
Geolocation and Events
Here’s an example event call to our REST API that sends an event "Level Complete" with a single property "Level Number".
The REST API won’t perform any geolocation by default. It assumes you’re sending these calls server-side, although you can force it when you send in a property called “ip.”
The request uses the value sent with the “ip” parameter for geolocation, so this event appears to have come from New York.
{
"event": "Level Complete",
"properties": {
"Level Number": 9,
"distinct_id": "13793",
"token": "e3bc4100330c35722740fb8c6f5abddc",
"time": 1409259110,
"ip": "72.229.28.185"
}
}
Geolocation and User Profile Updates
User profile updates present more issues with geolocation because they are often batch-updated server-side or over the REST API. But calls to engage assume that you want to use the IP address of the request if no IP property is given.
As a result, each user appears to be located wherever the server that sends the requests is located. You can override this behavior by including an $ip key in each of the profile update objects.
{ "$token": "e3bc4100330c35722740fb8c6f5abddc", "$distinct_id": "13793", "$ip": "72.229.28.185", "$set": { "Address": "1313 Mockingbird Lane" } }
Notice that you need to set “$ip” outside of the “$set” dictionary, this applies to all profile action keys.
A key-value pair of "$ip": "0" flags that the IP on the request should be ignored. This would leave the geolocation information of that profile unchanged.
You can find an example of this in our developer documentation for user profile updates.
Latitude and Longitude Based Geolocation
If you have access to the information, you can specify the latitude and longitude of the originating event or profile update.
Events (SDKs/HTTP)
On events, these properties must be named $latitude and $longitude respectively and the values should be in floating point decimal degrees.
Example:
{ "event": "Signed Up", "properties": { "distinct_id": "13793", "token": "mytoken", “$latitude”: 37.77, “$longitude”: -122.42 } }
If both $latitude and $longitude are present on an event, Mixpanel will use these properties (instead of the IP address) to infer the closest city.
Users (HTTP)
On user profile updates, these properties must be named $latitude and $longitude respectively and the values should be in floating point decimal degrees. Additionally, Mixpanel will only set geolocation information on the profile if the accompanying operation is a $set or a $set_once. Currently, reverse geocoding for user profiles is not supported via client-side SDKs.
Example:
{ "$token": "mytoken", “$distinct_id”: “13793”, “$latitude”: 37.77, “$longitude”: -122.42, "$set": { “My_property”: “my_value” } }
If both $latitude and $longitude are present on the update, Mixpanel will use these properties (instead of the IP address) to infer the closest city.
Pass Your Own IP Address (Javascript/HTTP)
As all server-side calls originate from the same IP, such as the IP of your server, it can have the unintended effect of setting the location of all of your users to the location of your datacenter.
If you want to pass in your own IP address similar to the way you can with track, leave out the URL parameter ip= and add a property called $ip to the message payload.
{
"$token": "e3bc4100330c35722740fb8c6f5abddc",
"$distinct_id": "13793",
"$ip": "72.229.28.185",
"$set": {
"Address": "1313 Mockingbird Lane"
}
}
Notice that you need to set $ip outside of the $set dictionary. This action overwrites the geographic data on the profile with distinct_id = 13793 with New York, NY.
Pass Your Own IP Address (Java)
If you want to pass in your own IP address, you can add a property called $ip to the message payload.
new MessageBuilder(PROJECT_TOKEN);
JSONObject props = new JSONObject();
props.put("Plan", "Premium");
props.put("$ip","72.229.28.185");
props.put("$ignore_time”,”true”);
JSONObject update = messageBuilder.set("13793", props);
MixpanelAPI mixpanel = new MixpanelAPI();
mixpanel.sendMessage(update);
Notice that you set the $ip property to set the ip address of the user. This prevents all events being ingested with the IP address of your server. When you send people updates, $ip must be included as a modifier parameter outside the properties dictionary. Pass the $ip property in a people.set() call in a separate dictionary as the third argument to update City, Region, or Country properties.
Set the $ignore_time property to not update the $last_seen property with the time when Mixpanel ingested the data. Set the $ip property to use user data location data in Mixpanel because Java does not capture location by default.
Pass Your Own IP Address (Node.js)
If you want to pass your own IP address similar to the way you can with track, leave out the ip= URL parameter, and add a property called ip to the message payload:
mixpanel.people.set(req.session.user._id, { $email: req.session.user.email, $first_name: req.session.user.name, $created: req.session.user.date, ip: req.param('ip') });
Force the IP (Ruby)
All Mixpanel server-side libraries have parameters that allow you to force the IP behavior that you want.
The following are examples in a Ruby implementation:
The general format of set() could look like:
def set(distinct_id, properties, ip=nil, optional_params={})
Using the IP in the property for Geo-location:
tracker.people.set('Drew', {'$first_name' => 'Drew'}, '72.229.28.185')
Not using Geo-locate:
tracker.people.set('Drew', {'$first_name' => 'Drew'}, '0')
tracker.people.set('Drew', {'$first_name' => 'Drew'}, 0)
Geo-locates from the IP on the request:
tracker.people.set('Drew', {'$first_name' => 'Drew'}, nil)
tracker.people.set('Drew', {'$first_name' => 'Drew'})
Does not Geo-locate or update “$last_seen”
tracker.people.set('Drew', {'$first_name' => 'Drew'}, 0, {'$ignore_time' => 'true'}
Comments
Article is closed for comments.