Skip to content

Instantly share code, notes, and snippets.

@Macagare
Last active April 22, 2019 09:59
Show Gist options
  • Save Macagare/4134400 to your computer and use it in GitHub Desktop.
Save Macagare/4134400 to your computer and use it in GitHub Desktop.
Javascript: Google Analytics Snipper + Google Analytics JS Logging
var _gaq = _gaq || [['_setAccount', 'UA-xxxxxxx-1'], ['_trackPageview']];
; (function ($, n, d, t, w, s, undefined) {
//Google Analytics Snipper
var g = d.createElement(t); g.type = 'text/javascript'; g.async = true;
g.src = ('https:' == d.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var b = d.getElementsByTagName(t)[0]; b.parentNode.insertBefore(g, b);
//Javascript Error Logging on Google Analytics
var i = ($) ? $.browser : {}, v = n.appVersion, u = n.userAgent;
if (v.match(/Win/i)) i.os = 'Windows';
else if (v.match(/X11/i)) i.os = 'UNIX';
else if (v.match(/Linux/i)) i.os = 'Linux';
else if (v.match(/Android/i)) i.os = 'Android';
else if (v.match(/Mac/i)) {
i.os = 'MacOS';
if (u.match(/iPhone/i)) i.os = 'iPhone';
else if (u.match(/iPod/i)) i.os = 'iPod';
}
else i.os = 'Unknown OS';
i.screen = s.width + 'x' + s.height;
i.window = w.innerWidth + 'x' + w.innerHeight;
i.appVersion = v;
window.onerror = function (m, f, l) {
var a = f + ':' + l + '\n' + m;
for (var p in i)
a += '\n' + p + ': ' + i[p];
_gaq.push(['_trackEvent', 'error', m, a]);
};
})(jQuery, navigator, document, 'script', window, screen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment