Skip to content
berarma edited this page Nov 2, 2014 · 1 revision

Use Examples

Using it with Google Analytics

Classical Analytics

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);

$.freeTheCookies(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);
});

Universal Analytics

(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');

$.freeTheCookies(function() {
  ga('create', 'UA-XXXX-Y', 'auto');  // Creates a tracker.
  ga('send', 'pageview');             // Sends a pageview.
});

You may see a warning on your Google Analytics Dashboard that you haven't added the tracking code even when everything works well.

Using it with Google AdSense

When using the asynchronous code just load the external JS script from inside the onConsent function. Everything else is left as is.

$.freeTheCookies(function() {
  $( "<script async src='//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'></script>" ).appendTo('body');
});