Last active
August 29, 2015 14:22
-
-
Save awavering/70e1aad49f2b167764f1 to your computer and use it in GitHub Desktop.
A skeleton for building Google Analytics plugins with correct tracker names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Provides a plugin name and constructor function to analytics.js. This | |
// function works even if the site has customized the ga global identifier. | |
function providePlugin(pluginName, pluginConstructor) { | |
var ga = window[window['GoogleAnalyticsObject'] || 'ga']; | |
if (ga) ga('provide', pluginName, pluginConstructor); | |
} | |
// Plugin constructor. | |
// All plugin functionality takes place inside the function . | |
function Demo(tracker, config) { | |
// Sanity check to write tracker name to console. | |
console.log("Loaded trackomatic on tracker " + tracker.get('name') + " with the config object " + JSON.stringify(config)); | |
// Sample event call below. Uncomment to run. | |
//tracker.send('event', 'category', 'action', 'label', 0, { 'nonInteraction': 1 }); | |
} | |
// Register the plugin. | |
providePlugin('demo', Demo); | |
// run the above code in console, then run ga('require', 'demo'); | |
// to run this for a specific tracker, run ga('[trackername].require', 'demo'); where [trackername] is the name of your tracker | |
// (default is "t0") | |
// Normally, plugin js should be loaded after GA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment