Home » Data completeness via a server-side Mixpanel implementation

Data completeness via a server-side Mixpanel implementation

Tags:

The future is server-side. The rise of ad blockers and other privacy extensions hasn’t made product analytics implementation easier. On the other hand, good ol’ Google Analytics has gained quite a lot of competition in recent years from vendors like Heap and Mixpanel. A friend of mine asked my opinion on the latter — no better time to try a server-side implementation of Mixpanel.

Mixpanel in 2011.

Mixpanel, founded in 2009, was ahead of its time. Before web analytics” turned into “product analytics” and the full-stop embracement of event-based tracking, Mixpanel already made the bold statement that actions mattered, not page views. A mantra that competitors only picked up years later.

Mixpanel is surprisingly flexible. You can build your own insights and dashboards. It has an integrated A/B testing tool (beta), and yes, there’s even a data catalog. But what’s on the front is of lesser importance in this blog post. In the following paragraphs, I explain how you set up Mixpanel to not be blocked by privacy tools.

Mixpanel’s server-side tracking libraries

Contrary to client-side tracking, Mixpanel’s server-side tracking does not collect default tracking parameters by default. That makes perfect sense, given that these parameters live in the browser of the visitor. Our job is to manually collect, calculate, and manage all these parameters before sending a signal to the server.

I have written a JavaScript library that creates an object that collects and stores these parameters consistently. This object also has a method that we can call to send the events we’d like to track. It’s much like the original Mixpanel JavaScript tracker, but a lot more simplified — more on this below.

The most significant difference is that it does not communicate with the Mixpanel servers directly. Instead, it sends the information to a relay, a self-hosted file, where the IP address is appended to ensure that Mixpanel can geolocate the user. Finally, this file uses Mixpanel’s PHP library to send all the information to the Mixpanel servers.

Implementation

Implementing it is extremely easy. However, currently, only PHP is supported.

  • Upload both files to the same server as your website.
  • Add your mixpanel token in mp_relay.php
  • Add the snippet below somewhere in the header on all pages of your website
  • Point the script source to the library location on your server
  • Point the URL inside the creation of the Badgr object to the relay file location on your server
<script src="https://www.roelpeters.be/mp_front.js"></script>
<script>
  mp = new Badgr("https://www.roelpeters.be/mp_relay.php");
  mp.init();
</script>

You can now start tracking events as follows:

<script>
  mp.track("Page view"); // example without properties
  mp.track("Social Click", {"Social Medium": "Twitter"}); // example with properties
</script>

Currently, the library doesn’t support building user profiles or tracking revenue, but expect this feature to be available in the very near future.

Why go through all this hassle?

Browser extensions such as uBlock Origin prevent the Mixpanel tracker — like any other tracker — from functioning properly. By hosting your own tracking library and having it interface with a relay instead of a third-party server, there’s no straightforward way privacy tools can block this.

One could shift more responsibility from a developer to a marketer or analyst by implementing the JavaScript library via a client-side tag management solution. However, just like with trackers, some privacy tools also block tag management containers, affecting your data’s completeness.

From an ideological perspective, the solution I proposed has some overlap with the new server-side Google Tag Manager container. However, you don’t need a GCP deployment, nor do you have to pay for cloud resources. The downside is that most of the technical implementation responsibilities reside with a developer — there is no tag management (yet).

The way forward?

There’s clearly a gap in the (non-enterprise) analytics stack. On the one hand, product analytics vendors offer server-side libraries. On the other hand, there are no straightforward first-party tag management solutions, preferably integrated with a CMS like WordPress or Drupal, that allow marketers and analysts to implement tracking themselves.

In the meantime, feel free to use my library. Although it is written with Mixpanel in mind, you can make it interface with a server-side implementation of Adobe Analytics, Google Analytics, and the likes with minimal modifications. I’m not a developer, so feel free to propose adjustments with regards to ease of use, implementation and security.

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

2 thoughts on “Data completeness via a server-side Mixpanel implementation”

Leave a Reply

Your email address will not be published. Required fields are marked *