Drupal Mixpanel Integration Methods

Drupal Mixpanel Integration

Drupal Mixpanel integration grants access to a product analytics stack with powerful features centered around deep user segmentation and A/B testing. This is much more complex than most popular web analytics tools like Matomo. Those are primarily focused on reporting user activity, not modifying a website in accordance with someone’s user agent, geolocation, etc. From Mixpanel.com you can develop complex integrations and API calls to modify web sites and applications automatically based on reported user analysis.

As great as it is, Mixpanel integration with a content management system (CMS) can be difficult. Many Mixpanel CMS integrations only provide basic integration using the Mixpanel token ID. For anything more you’ll need to develop API calls within code that may not be accessible in an admin dashboard by default.

Depending on your goals with Mixpanel, and your comfort level with editing code, there are three ways to integrate Mixpanel with Drupal:

Drupal Mixpanel Integration Module

As of Spring 2021, Drupal 7 users can use the Mixpanel Drupal module (version 7.x-1.3). The Drupal 8 port is still in progress, according to the module description. If you choose this method, remember that it is covered by the security advisory policy but was last updated in 2016. Ensure you’re using the latest PHP version and a proactive server malware scanner to handle cyber threats.

  1. Install the Drupal module using the Mixpanel download link for your Drupal version.
  2. Click Enable newly added modules.
  3. There will be a dedicated section for Mixpanel. Check the boxes for Mixpanel and Mixpanel defaults. You’ll need the separate Rules module to enable Mixpanel rules which can assist you with configuring what data is reported.
  4. At the bottom, select Save Configuration.
  5. To the right of Mixpanel, select Configure.
  6. Uncheck Queue and send events during cron for immediate data reporting.
  7. Uncheck the option to track anonymous users to focus on Drupal users instead of all visitors.
  8. Log into your Mixpanel.com account.
  9. In the upper-right corner, select the gear icon and the project name from the menu.
  10. Copy the alphanumeric project token string and paste it in the Drupal Mixpanel Token text field.
  11. Select Save Configuration.
  12. Clear your Drupal cache to ensure the tracking code is public immediately.
  13. Log into your Drupal site in a private browsing session. The Mixpanel defaults module will send data for joining/leaving a group, modifying users, user login/logout, modifying a node, and creating a comment. Page views stats aren’t sent by default.
  14. In Mixpanel, select the gear icon in the upper-right corner and Set up Mixpanel.
  15. At the bottom, beside Check for Incoming Data, select Check. The notification should change to “We successfully received your data!” 
  16. Select Start Using Mixpanel.
  17. You’ll need to configure your insights report to see the logged event. Under Events & Cohorts, select + Add and Event.
  18. In the Select event drop-down menu, you’ll see options from recorded events. To see everything, choose All Events.
  19. The line graph at the bottom will update accordingly. You may choose to learn more about sending custom events to Mixpanel in a custom module or the Rules module.
  20. You can use the Header and Footer Scripts module covered below to add tracking code for additional events including page activity.
Drupal Mixpanel module settings

Manual Mixpanel Drupal Integration

For Drupal 8 users, and those who prefer not to use an old module, there are other methods. The first one is to use the Header and Footer Scripts Drupal module. Like the Mixpanel module, this scripts module is covered by the security advisory policy and hasn’t been updated in 2-3 years (as of Spring 2021). 

Unlike the Mixpanel Drupal 7 module, it is much more popular with over four thousand installations and available for Drupal 8. Still, ensure you have a solid cybersecurity posture beforehand.

  1. Install and enable the Header and Footer Scripts module for Drupal 7 or 8.
  2. Select Header and Footer Scripts from the Configuration page.
  3. Go to the Set up Mixpanel page.
  4. Under Install Library (SDK), copy the HTML snippet and paste it into the Header Scripts, not “Styles,” text box.
  5. Select Save Header Settings.
  6. Select the Body Scripts tab.
  7. Add this tracking code in the Body Scripts field. This will only gather page views but it is a starting point to ensure your setup works:
    <script>mixpanel.track("Viewed Page", {'Page Name': document.title, 'Page URL': window.location.pathname});</script>
  8. At the bottom, select Save Body Settings.
  9. Clear your Drupal cache to ensure the tracking code is public.
  10. View a few pages of your Drupal site in a private browsing session. 
  11. At the bottom of the Mixpanel setup page, select the Check button. The notification to the left should change to “We successfully received your data!” 
  12. Select Start Using Mixpanel.
  13. At the top, ensure Insights is selected.
  14. Under Events & Cohorts, select + Add.
  15. Select Event, then Viewed Page. The graph at the bottom will update accordingly.
  16. Under Breakdown, select + Add.
  17. Select Page Name or Page URL.
  18. Change the visualization to a pie graph for a different representation.
Mixpanel viewed page and page name insights

Manually Editing Drupal Core Files

If you’d prefer to not use any modules at all, you can edit Drupal core files. There are multiple methods for this including: 

  • Editing core and theme files
  • Structure blocks
  • Native Drupal APIs

Unless you’re comfortable editing Drupal core files, you should first create a backup and seek assistance from Drupal developers via Freenode internet relay chat (IRC) channels and other Drupal communities. You could also contact Mixpanel technical support directly. This allows you to best determine which method would work best for your needs and Drupal hosting plan. Let us know if you have any questions regarding Drupal Mixpanel integration.

For more step-by-step guides and practical tips, visit our Drupal Education Channel.

Ignite your online presence with cutting-edge Drupal Website Hosting. Elevate speed, security, and reliability for a seamless website experience.

InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

Was this article helpful? Join the conversation!