Can I implement Mixpanel using Google Tag Manager?

Google Tag Manager (GTM) lets you inject custom HTML and JavaScript into your web pages based on user-defined selection criteria. It is designed to help manage tracking applications like Mixpanel, Google Analytics, Optimizely, etc.

That said, GTM does limit the flexibility you have when implementing an analytics platform like Mixpanel. If possible, we generally recommend implementing Mixpanel directly using the JavaScript library or using Autotrack because:

  • Tracking detailed user activity (which is the advantage of using Mixpanel in the first place) is cumbersome with GTM and will still require implementing code on your site itself, negating the main advantage of GTM (being able to make changes to tracking without code changes on your site itself).
  • Debugging any issues that arise is more difficult if you’ve integrated using GTM.

If you do decide to implement Mixpanel using GTM, you can do so by:

  1. Creating a GTM Container: See Google’s documentation on setup and getting started for additional guidance.
  2. Create a Mixpanel project: While you’re logged into your Mixpanel account, click on the project dropdown in the top left corner of the page, fill in a name at the bottom of the list, and click “Create Project.”
  3. Initialize the Mixpanel library: While you can try to use GTM to load the Mixpanel library, we recommend placing the Mixpanel snippet on the page directly if possible to ensure it loads properly (i.e., before any of your track calls). If you’re using Autotrack, you can stop here - this is where you will switch to the Mixpanel Point & Click Editor to complete your Event tracking.
  4. Create Mixpanel tags: Select “Custom HTML” from the list of tag types to create a new Mixpanel tag. From there, insert mixpanel.track() calls to track the particular action that users perform with this tag.
  5. Define rules for when tags should send data: Next, select a rule that injects your tags on certain pages of your site (e.g., if you only want this tag to fire when the landing page loads, you’ll need to create a custom rule to target just the landing page).

Advanced tracking

If you want to track actions outside of pageviews, to get that level of detail with GTM, you’d have to implement a “data layer object” that allows you to pass variable information from the various parts of your site to the tags you inject with GTM. Read more about Data Layers.