Skip to content

Instantly share code, notes, and snippets.

@benarent
Last active February 14, 2017 22:29
Show Gist options
  • Save benarent/c8e0b14bca976d1d1917a28084fe8cac to your computer and use it in GitHub Desktop.
Save benarent/c8e0b14bca976d1d1917a28084fe8cac to your computer and use it in GitHub Desktop.
Librato Conversion ADC Script.js
<script type="text/javascript">
// code via https://github.com/librato/unbounce
var AdStorage, MarketingTracking, Tracking, initTracking, initValidations,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
AdStorage = (function() {
function AdStorage() {
var params;
if (this.getUriParams() != null) {
this.data = {};
params = this.getUriParams();
if ($.cookie('adsnarf') == null) {
this.data = {
last: params,
clicked: 1
};
} else {
this.data = JSON.parse($.cookie('adsnarf'));
this.data['last'] = params;
this.data['clicked'] += 1;
}
this.createCookie();
}
}
AdStorage.prototype.createCookie = function() {
var cookie_opts;
cookie_opts = {
expires: 365 * 2,
path: '/',
domain: 'librato.com'
};
$.cookie('adsnarf', JSON.stringify(this.data), cookie_opts);
return null;
};
AdStorage.prototype.getUriParams = function() {
return $.url().param('adc');
};
return AdStorage;
})();
Tracking = (function() {
function Tracking() {}
Tracking.prototype.track = function(event, properties) {
analytics.track("" + event, properties);
return null;
};
Tracking.prototype.trackLink = function($link, event, properties) {
analytics.trackLink($link, "" + event, properties);
return null;
};
Tracking.prototype.trackForm = function($form, event, properties) {
analytics.trackForm($form, "" + event, properties);
return null;
};
Tracking.prototype.pageview = function(url) {
if (url == null) {
url = null;
}
if (url != null) {
analytics.pageview(url);
} else {
analytics.pageview();
}
return null;
};
Tracking.prototype.deviceLookup = function(device) {
switch (device) {
case 'c':
return 'Computer';
case 't':
return 'Tablet';
case 'm':
return 'Mobile';
default:
return null;
}
};
return Tracking;
})();
MarketingTracking = (function(superClass) {
extend(MarketingTracking, superClass);
function MarketingTracking() {
if (typeof analytics !== "undefined" && analytics !== null) {
this.adClicks();
this.pageViews();
}
}
MarketingTracking.prototype.adClicks = function() {
var k, meta, props, ref, v;
meta = $.url().param('adc');
if (meta != null) {
props = {
'Ad Provider': meta.apr,
'Ad Group': meta.ag,
'Acquisition Cost': meta.co,
'Ad Campaign': meta.camp,
'Ad Results Page': (ref = meta.ap) != null ? ref.charAt(0) : void 0,
'Device Type': this.deviceLookup(meta.dv),
'Keywords': meta.kw
};
for (k in props) {
v = props[k];
if (!v) {
delete props[k];
}
}
this.track("Clicked an Ad", props);
}
return null;
};
MarketingTracking.prototype.pageViews = function() {
var params;
params = {
url: $.url().attr('source'),
'Page Name': $('title').html(),
'Context': this._hostContext($.url().attr('host'))
};
this.track('Viewed a Marketing Page', params);
return null;
};
MarketingTracking.prototype._hostContext = function(host) {
var context;
context = (function() {
switch ("" + host) {
case "blog.librato.com":
return "Blog";
case "promo.metrics.librato.com":
return "Landing Page";
case "info.librato.com":
return "Landing Page";
case "get.librato.com":
return "Landing Page";
case "www.librato.com":
return "Marketing Site";
default:
return "" + host;
}
})();
return context;
};
return MarketingTracking;
})(Tracking);
initValidations = function() {
var config, feedback;
feedback = function(type, _this) {
jQuery(_this.element).removeClass("LV_neutral_field");
_this.addFieldClass();
if (type === "neutral") {
return jQuery(_this.element).addClass("LV_neutral_field");
}
};
if (jQuery(".signup_form").length > 0) {
config = {
onValid: function() {
return feedback("valid", this);
},
onInvalid: function() {
if (jQuery(this.element).val().length > 0) {
return feedback("invalid", this);
} else {
return feedback("neutral", this);
}
}
};
jQuery(".signup_form").each(function(i, elem) {
var email, pw;
email = new LiveValidation(jQuery("input[type=\"email\"]", elem)[0], config);
email.add(Validate.Presence);
email.add(Validate.Email);
pw = new LiveValidation(jQuery("input[type=\"password\"]", elem)[0], config);
pw.add(Validate.Presence);
return pw.add(Validate.Length, {
minimum: 6
});
});
}
return null;
};
initTracking = function() {
new AdStorage();
new MarketingTracking();
return null;
};
jQuery(document).ready(function() {
initValidations();
initTracking();
return null;
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment