Skip to content

Instantly share code, notes, and snippets.

@yahelc
Created October 3, 2011 18:48
Show Gist options
  • Save yahelc/1259897 to your computer and use it in GitHub Desktop.
Save yahelc/1259897 to your computer and use it in GitHub Desktop.
ga integration stub
var _gaq = _gaq || [];
var optimizely = optimizely || [];
(function($) {
var oldonerror = window.onerror;
window.onerror = function(msg, url, line) {
if(oldonerror) {oldonerror(msg, url, line);}
_gaq.push(['_trackEvent', 'Error Log', msg, url + '_' + line]);
};
function getElem(href)
{
var target = document.createElement("a");
target.href = href;
return target;
}
function proper(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
function getPathname(href){
return getElem(href).pathname;
}
function readcookie(key) {
var result;
return (result = RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? (result[1]) : null;
}
var get = (function() {
var map = {};
location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, k, v) {
map[k] = v;
});
return map;
}());
if (get.source) {
_gaq.push(['_setCustomVar', 1, 'Source', get.source, 2]);
}
if (get.subsource) {
_gaq.push(['_setCustomVar', 5, 'Subsource', get.subsource, 2]);
}
if (readcookie("msid")) {
_gaq.push(['_setCustomVar', 2, 'msid', readcookie("msid"), 2]);
}
_gaq.push(['_setCustomVar', 3, 'Has GUID', "" + !!readcookie("guid"), 2]);
_gaq.push(['_setCustomVar', 4, 'Has Spud', "" + !!readcookie("spud"), 2]);
$.fn.analytics = function(param, custom, live) {
if (!param) {
return this.trigger(custom || "mousedown");
}
var event_string = custom ? custom + ".analytics" : "keydown.analytics mousedown.analytics";
var func = live || "one";
return this[func](event_string, function(e) {
if (custom || (e.type === "mousedown" || e.which === 13)) {
if (typeof param === "function") {
param.apply(this);
}
else{
if ($.isArray(param) && param[1] && typeof param[1] !== "number") {
_gaq.push(["_trackEvent"].concat(param));
} else if(typeof param==="string" || $.isArray(param)) {
optimizely.push(["trackEvent"].concat(param));
}
}
}
});
};
function bsd_contrib_amt(m) {
if (m.match(/contribution/i) && $("form#contribution").length) {
var val = Math.round((!$('#amt_other').attr('checked')) ? $('input[name="amount"]:checked').val() : $('input[name="amount_other"]').val());
return (val != val || Math.abs(val)>1E9) ? 0 : val;
}
return 0;
}
$.fn.registerBSDError = function(module) {
if (this.length) {
_gaq.push(['_trackEvent', 'Error', module, location.pathname, bsd_contrib_amt(module)]);
}
};
$(document).ready(function() {
$("input").filter('[type="image"],[type="submit"]').analytics(function(e) {
var $form = $(this).closest('form');
var form_id = $form.attr('id') || $form.attr('name') || $form.attr('action') || "(none)";
var amt = 0;
_gaq.push(['_trackEvent', 'Conversion', form_id, location.pathname, bsd_contrib_amt(form_id)]);
});
$('span.signuperror').registerBSDError("Signup");
$('div.contriberrorbanner').registerBSDError("Contribution");
$('#invitationpage .error').registerBSDError("Share");
$("[href^='http']").analytics(function(e) {
if (this.hostname !== location.hostname) {
var dest = $(this).attr("href");
var net = (/(facebook|twitter|addthis|youtube)(\.com)/i).exec(dest);
if(net)
{
_gaq.push(["_trackSocial", proper(net[1]), action, dest, getPathname(dest)]);
}
var action = net ? "social" : "external";
_gaq.push(['_trackEvent', 'Exits', action, dest]);
}
},null, "live");
if (window.BSDTracker) {
var type = (BSDTracker.signup) ? "signup" : (BSDTracker.contribution) ? "contribution" : false;
if (type) {
var tracker = BSDTracker[type];
var form = tracker.safeGet('formname', "");
var amount = tracker.safeGet('transaction_amt', 0);
_gaq.push(['_trackEvent', 'Completions', tracker.getModuleName(), form, Math.ceil(amount)]);
if (amount) {
var order_id = tracker.safeGet("contribution_key", "");
var sku_end = $.inArray(amount, tracker.safeGet("pageamounts", []))!==-1 ? Math.round(amount) : "other";
var sku = tracker.safeGet("contribution_page_id", "") + "_" + sku_end;
var pagetype = +tracker.safeGet("pagetype", 1);
var category = (+tracker.safeGet("is_recurring", false)) ? "BSD Recurring" : "BSD";
category += pagetype === 4 ? " Memberships" : pagetype === 2 ? " Tickets" : pagetype === 3 ? " Custom Contributions" : " Contributions";
_gaq.push(['_addTrans', order_id, "", amount, '0', '0', "", "", ""], ['_addItem', order_id, sku, form, category, amount, '1'], ['_trackTrans']);
optimizely.push(["trackEvent", "ga_integration", amount*100 ]);
}
}
}
$(window).load(function(){
$.ajax({
cache: true,
url: '//www.bluestatedigital.com/page/-/js/analytics/GA-to-bsdsource-min.js',
dataType: "script"
});
function social_event(net, action, url, path, val)
{
_gaq.push(["_trackSocial", net, action, url, path]);
_gaq.push(["_trackEvent", net, action, path, val ]);
}
if (window.FB && FB.Event) {
FB.Event.subscribe('edge.create', function(response) {
social_event("Facebook", "Like", response, getPathname(response), 1);
});
FB.Event.subscribe('edge.remove', function(response) {
social_event("Facebook", "Unlike", response, getPathname(response),-1);
});
FB.Event.subscribe('comment.create', function(response){
social_event("Facebook", "Comment", response.href, getPathname(response.href),1);
});
FB.Event.subscribe('comment.remove', function(response){
social_event("Facebook", "Uncomment", response.href, getPathname(response.href),-1);
});
}
if (window.twttr) {
'click tweet retweet follow favorite'.replace(/\w+/g,function(n){
twttr.events.bind(n, function(intent) {
if(intent)
{
var target = document.createElement("a");
target.href = decodeURIComponent(intent.target.src.match(/[&#?]url=([^&]*)/)[1]);
social_event("Twitter", intent.type, target.href.replace(target.hash,""), target.pathname);
}
});
});
}
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment