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.
Hello! I’m AK and I’m an engineer at Mixpanel. Today I’m going drop some knowledge on pixel tracking with Mixpanel.
Pixel Tracking has been around for a long time - it is used by ESPs, facebook ads, google adsense, and many other advertising/email providers to keep track of what users click on.
The essential idea of pixel tracking is simple:
create a 1x1 transparent image that the user cannot see...
host the images on a server you do control
Embed that image on a webpage/email/ad (usually a domain you do not own or control)
If that image is requested, the server which hosts that ‘image’ can be sure that a user has ‘viewed’ the page which contains that pixel
The neat thing about images is that they can be shared across domains… for example, my page on:
can contain HTML that requests images from a totally different domain; for example this is perfectly valid HTML, that can be served on any domain:
That means, if todayifoundout.com wanted to see information on the users I was rickrolling on aktunes.com, they could!
This unique feature of the
<img> tag makes it possible to track events and behaviors on web-properties that you don’t own using pixel tracking. And this is actually a pretty big deal!
However, emails can contain images, even invisible ones, so if your email contained one of these ‘pixel tracker’ images, when the user opens the email, the invisible image would be requested from our server, alerting the server that the user did open the email.
While email is certainly the intended target for pixel tracking, there are numerous other use-cases for pixel tracking, like ads, affiliate links, direct messages, social media posts, and bot-driven interactions. Pixel tracking is usually a second (or third) resort; pixel tracking is primarily used when we wish to track interactions (views, clicks) on resources we do not completely control.
With most pixel-tracking implementations, you don’t have control over the parameters or values that are passed into the pixel trackers; most pixel tracking providers will hardcode this information and give you an ‘embed script’ which you cannont easily customize.
Mixpanel, by contrast, fully supports custom pixel tracking, with any number of parameters and it’s easy to get started!
Let’s walk through the spec together…
Recall that an event in Mixpanel has a very specific anatomy:
"event": "Something Happened",
To send this event to Mixpanel, this event data first base64 encoded
And finally, that data is appended to our base url for the events API:
(under the hood, this is what’s happening when you call
mixpanel.track() in your code!)
Note that we can append additional params to this URL, to make it a ‘pixel’
That means this URL can now be used inside an image tag
This HTML will not produce any visible output, but when it’s loaded, but it will send an event to Mixpanel ‘something happened’ when any browser loads that tag.
It’s also worth noting that Mixpanel’s API supports redirects as well, which allows us to more granularly track any link that is clicked (such as a social media post, or direct message):
This works similarly to the image option, except we use the ‘redirect’ parameter in the URL instead of the ‘img’
And can be used in anywhere that supports anchor tags (<a>):
<a href="https://api.mixpanel.com/track/?data=ewogICAgImV2ZW50IjogIlNvbWV0aGluZyBIYXBwZW5lZCIsCiAgICAicHJvcGVydGllcyI6IHsKICAgICAgICAiZGlzdGluY3RfaWQiOiAiMTM3OTMiLAogICAgICAgICJ0b2tlbiI6ICJlM2JjNDEwMDMzMGMzNTcyMjc0MGZiOGM2ZjVhYmRkYyIsCiAgICAgIAkiZm9vIjogImJhciIKCiAgICB9Cn0=&redirect=https://google.com">click me</a>
The above HTML will create a link; when it’s clicked, it takes the user to google, but first, it logs an event to Mixpanel.
In order to help you experiment with pixel tracking, I made this (very simple) web form that will help you generate these URLs, so that you can create a few of your own and test them before rolling out a larger implementation:
That’s all you need to know about pixel tracking! Have fun out there!