Reputation: 2269
My code does not allow me to opt-in back to Google Analytics. What would be the way to set again the Google Analytics cookie to opt-in or how to remove the state of opt-out to be again opt-in?
The Google Analytics debugger
Initializing Google Analytics.
Running command: ga("create", "UA-XXXXXXX-2", "electronics.semaf.at")
Creating new tracker: t0
Aborting cookie write: User has opted out of tracking.
Aborting cookie write: User has opted out of tracking.
Running command: ga("set", "anonymizeIp", true)
Running command: ga("set", "forceSSL", true)
Running command: ga("send", "pageview")
User has opted out of tracking. Aborting hit.
Registered new plugin: ga(provide, "render", Function)
My Javascript Code to opt-in and opt-out:
<script>
var gaProperty = 'UA-XXXXXXX-2';
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
// Opt-out function
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
location.reload(true);
}
// Opt-in function
function gaOptin() {
document.cookie = disableStr + '=false; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = false;
location.reload(true);
}
</script>
<script>
(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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXX-2', 'auto');
ga('set', 'anonymizeIp', true);
ga('set', 'forceSSL', true);
ga('send', 'pageview');
</script>
This it the part to opt-in or opt-out
<script type="text/javascript">
if(window[disableStr] == true) {
document.getElementById("google_analytics").innerHTML = 'You are opt-out from Google Analytics. <a href="javascript:gaOptin()">Opt-in</a>';}
else {
document.getElementById("google_analytics").innerHTML = 'This page is using <a href="http://analytics.google.com/">Google Analytics</a>. <a href="javascript:gaOptout()">Opt-out</a>';
}
</script>
Upvotes: 0
Views: 2408
Reputation: 145
Try this:
// Opt-in function
function gaOptin() {
document.cookie = disableStr + '=false; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
window[disableStr] = false;
location.reload(true);
}
If you need more Information about deleting cookies read this: w3schools Cookies
Upvotes: 2