Skip to content

Instantly share code, notes, and snippets.

@raineorshine
Last active May 3, 2019 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raineorshine/3912440 to your computer and use it in GitHub Desktop.
Save raineorshine/3912440 to your computer and use it in GitHub Desktop.
Google Analytics example
// 1. basic tracking code
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-35559116-1']);
_gaq.push(['_setDomainName', 'collegecoding.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
// 2. how to track a custom event
_gaq.push(['_trackEvent', 'Buy Now', 'Click', selectedOption]);
// 3. how to track a custom event on an outbound link
// you need to introduce a small delay for the Analytics request to go through
$("#paypal-form").on("submit", function() {
var selectedOption = $("[name=os0]").val();
_gaq.push(['_trackEvent', 'Buy Now', 'Click', selectedOption]);
// submit the form after a small delay
setTimeout(function() {
$("#paypal-form").get(0).submit();
}, 100);
return false;
});
// 4. how to trigger an AdWords conversion
// https://adwords.google.com/o/ConversionTracking
var image = new Image(1,1);
image.src = "http://www.googleadservices.com/pagead/conversion/GOOGLE_CONV_ID/?label=GOOGLE_CONV_LABEL&script=0";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment