2
votes

I use insites cookie consent https://cookieconsent.insites.com/

Now I will change to the cookie opt-in option but I can’t find how to use this with google analytics. I found this code https://cookieconsent.insites.com/documentation/disabling-cookies/

onInitialise: function (status) {
  var type = this.options.type;
  var didConsent = this.hasConsented();
  if (type == 'opt-in' && didConsent) {
    // enable cookies
  }
  if (type == 'opt-out' && !didConsent) {
    // disable cookies
  }
},

onStatusChange: function(status, chosenBefore) {
  var type = this.options.type;
  var didConsent = this.hasConsented();
  if (type == 'opt-in' && didConsent) {
    // enable cookies
  }
  if (type == 'opt-out' && !didConsent) {
    // disable cookies
  }
},

onRevokeChoice: function() {
  var type = this.options.type;
  if (type == 'opt-in') {
    // disable cookies
  }
  if (type == 'opt-out') {
    // enable cookies
  }
},

and I have google analytics code:

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXXXX-1']);
  _gaq.push(['_trackPageview']);
  _gaq.push(['_gat._anonymizeIp']); 

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

How can I bring these codes together? Analytics should run when a user accept - click on the button cookies (opt-in) without a page reload. And of course should run when cookies were accepted - user has clicks before.

2

2 Answers

2
votes

Use the separate function and past your GA scripts there. For example myScripts()

To get full code example see cookie consent script

0
votes

Copy and paste the Google Analytics code below the //enable cookies lines.

Of course you can create a function, eg. enableCookies, and call it from these events.