Clean-up how Mixpanel Javascript is added to the page (maybe using Libraries module?)

Created on 19 October 2013, over 11 years ago
Updated 17 January 2025, 3 months ago

From kscheirer:

  • Both the script blocks should live in a js file, and I think your comment about using drupal_add_js() is correct. Is the second block intentionally obfuscated? It seems like the inclusion of the actually library file should be as simple as drupal_add_js('//cdn.mxpnl.com/libs/mixpanel-2.1.min.js', 'external'). Finally, it would be nice to have an option to store the file locally - then it can be cached by Drupal and aggregated, much better for performance. You can use the Libraries API to make sure it's installed correctly.
  • You refer to the library being version 1.0 or 2.0 a lot, but the actual linked version is 2.1. From https://mixpanel.com/docs/integration-libraries/javascript it looks like they're at 2.2 now. Do they have a link that stays current? With libraries you can make sure its >= 2.0 or something like that, instead of testing for 2.0 directly.
  • You also have a mix of settings, some are being written to Drupal.settings, but others embedded into the js directly. I don't think you need to use those js scripts verbatim from the mixpanel site, a little modification to always use Drupal.settings would make it a lot cleaner.

My response:

These are actually really complicated issues. Mixpanel's library must be available before any other JavaScript, and mixpanel.init() has to be called with it's token right away (and Drupal.settings is added to the page after the script includes). The "obfuscated" Javascript (I think it's just minified for space) actually creates a stub 'mixpanel' object, which just buffers commands in an Array until the full library is loaded and then it'll send them to the server. This way the 'mixpanel' Javascript object is available immediately and to all other scripts, even before it's library loads.

Anyway, while the current code works, I'm not totally happy with it either and I think you're right - it could be cleaned up!

That said, I don't think we really do much worse than Google Analytics. ;-)

πŸ“Œ Task
Status

Closed: outdated

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States dsnopek USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024