Skip to content

Instantly share code, notes, and snippets.

@walkergv
Created April 27, 2016 22:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save walkergv/f07d55b3573d510de1c6a037676502f9 to your computer and use it in GitHub Desktop.
Save walkergv/f07d55b3573d510de1c6a037676502f9 to your computer and use it in GitHub Desktop.
Unbounce Event Tracker Script
<script type='text/javascript'>
(function() {
var EventTracker, lpScriptVersion;
lpScriptVersion = "1.2.0";
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}
,
i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
;
ga('create', 'UA-19997641-1', 'auto');
if (window.ub.page.variantId != null ) {
ga('send', 'pageview', document.location.pathname + window.ub.page.variantId + window.location.search);
} else {
ga('send', 'pageview');
}
EventTracker = (function() {
function EventTracker() {
lp.jQuery('.lp-pom-form .lp-pom-button').unbind('click').click((function(_this) {
return function(e) {
return _this.formHandler(e);
}
;
})(this));
lp.jQuery('.lp-pom-form form').unbind('keypress').keypress((function(_this) {
return function(e) {
if (e.which === 13 && e.target.nodeName.toLowerCase() !== 'textarea') {
return _this.formHandler(e);
}
}
;
})(this));
lp.jQuery('.lp-pom-button').not('.lp-pom-form .lp-pom-button').click((function(_this) {
return function(e) {
var id;
id = lp.jQuery(e.currentTarget).attr('id');
return _this.linkHandler(e, id);
}
;
})(this));
lp.jQuery('.lp-pom-image a').click((function(_this) {
return function(e) {
var id;
id = lp.jQuery(e.currentTarget).closest('.lp-pom-image').attr('id');
return _this.linkHandler(e, id);
}
;
})(this));
lp.jQuery('.lp-pom-text a').click((function(_this) {
return function(e) {
var id;
id = lp.jQuery(e.currentTarget).closest('.lp-pom-text').attr('id');
return _this.linkHandler(e, id);
}
;
})(this));
}
EventTracker.prototype.formHandler = function(e) {
var $form, id;
e.preventDefault();
e.stopPropagation();
$form = lp.jQuery('.lp-pom-form form');
id = '#' + $form.parent().attr('id');
if ($form.valid()) {
if (typeof console !== "undefined" && console !== null ) {
console.log('Sending event:\nCategory: Form\nAction: Submit\nID:', id);
}
return ga('send', 'event', 'Form', 'Submit', id, { hitCallback: function() {return $form.submit();}});
}
}
;
EventTracker.prototype.linkHandler = function(event, parentID) {
var $element, browser, hasHandler, id, params, preventingDefault, target, uri;
$element = lp.jQuery(event.currentTarget);
uri = $element.attr('href');
id = $element.attr('id');
target = $element.attr('target');
params = this.getGAParams(uri);
browser = lp.jQuery.browser;
hasHandler = this.hasHandler(id !== "" ? id : parentID);
preventingDefault = !hasHandler && target !== '_blank';
if (browser.msie === true && browser.version <= 8.0) {
preventingDefault = false;
}
if (preventingDefault) {
event.preventDefault();
}
if (typeof console !== "undefined" && console !== null ) {
console.log("Sending event:\nCategory: " + params.category + "\nAction: " + params.action + "\nLabel:" + parentID);
}
return ga('send', 'event', params.category, params.action, parentID, {
hitCallback: function() {
var _ref;
if (preventingDefault) {
if (uri.indexOf('#') === 0) {
return lp.jQuery('html, body').scrollTop((_ref = lp.jQuery(uri).offset()) != null ? _ref.top : void 0);
} else {
switch (target) {
case '_top':
return window.top.location.href = uri;
case '_parent':
return window.parent.location.href = uri;
default:
return window.location.href = uri;
}
}
}
}
});
}
;
EventTracker.prototype.hasHandler = function(id) {
var handler, handlers, name, _i, _len, _ref, _ref1, _ref2, _ref3;
handlers = [].concat(typeof $ === "function" ? (_ref = $('#' + id).data('events')) != null ? _ref.click : void 0 : void 0, typeof $ === "function" ? (_ref1 = $('#' + id + ' a').data('events')) != null ? _ref1.click : void 0 : void 0, (_ref2 = lp.jQuery('#' + id).data('events')) != null ? _ref2.click : void 0, (_ref3 = lp.jQuery('#' + id + ' a').data('events')) != null ? _ref3.click : void 0);
if (handlers.length > 0) {
for (_i = 0,
_len = handlers.length; _i < _len; _i++) {
handler = handlers[_i];
if (handler != null ) {
name = handler.namespace;
if (name === 'fb' || name === 'smoothScroll') {
return true;
}
}
}
}
return false;
}
;
EventTracker.prototype.getGAParams = function(uri) {
var host;
host = uri.split('/')[2] != null ? uri.split('/')[2] : "";
if (uri.indexOf('#') === 0) {
return {
category: 'In-Page',
action: uri
};
} else if (host.indexOf('facebook') > -1) {
return {
category: 'Social',
action: 'Facebook'
};
} else if (host.indexOf('twitter') > -1) {
return {
category: 'Social',
action: 'Twitter'
};
} else if (host.indexOf('linkedin') > -1) {
return {
category: 'Social',
action: 'Linkedin'
};
} else if (host.indexOf('plus.google.com') > -1) {
return {
category: 'Social',
action: 'Google+'
};
} else if (uri.indexOf('mailto:') > -1) {
return {
category: 'Email',
action: uri.replace('mailto:', '')
};
} else if (uri.indexOf('/tel/') > -1) {
return {
category: 'Phone',
action: uri.split('/tel/')[1]
};
} else if (/\.(pdf|doc|docx|csv)/.test(uri)) {
return {
category: 'Download',
action: this.cleanUri(uri)
};
} else {
return {
category: 'Outbound',
action: this.cleanUri(uri)
};
}
}
;
EventTracker.prototype.cleanUri = function(uri) {
return uri.replace(/clk[n,g]\//, "").replace("/", "://");
}
;
return EventTracker;
})();
lp.jQuery(function() {
return new EventTracker;
});
}
).call(this);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment