Last active
February 28, 2017 19:27
-
-
Save alexgb/7456209 to your computer and use it in GitHub Desktop.
Segment.io debugger
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
var _asAnalyticsMethodColors = { | |
identify: "#c66", | |
alias: "#c66", | |
track: "#66c", | |
page: "#c6c", | |
_default: "#888" | |
}; | |
function _asAnalyticsLog(method, objects) { | |
var color = _asAnalyticsMethodColors[method] || _asAnalyticsMethodColors._default; | |
var args = ['%c Segment.io %c '+method+' ','background: #6c6; color: white;', 'background: '+color+'; color: white;']; | |
objects = objects || []; | |
console.log.apply(console, args.concat(objects)); | |
}; | |
var segmentIsLoaded = $('script[src]').toArray().filter(function(script) { return script.src.indexOf('analytics.js') !== -1 }).length | |
Object.keys(_asAnalyticsMethodColors) | |
.filter(function(key) { return key.indexOf('_') !== 0; }) //ignore private keys | |
.forEach(function(method) { | |
var logAnalytics = function() { | |
return _asAnalyticsLog(method, Array.prototype.slice.call(arguments, 0)); | |
}; | |
if(segmentIsLoaded) { | |
window.analytics.on(method, logAnalytics); | |
} else { | |
window.analytics[method] = logAnalytics; | |
} | |
}); | |
console.log('Segment debugger loaded'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment