Hello,

can someone help me with an issue I am having with javascript cookies. I do not want the google analytics code to run until someone clicks on a button to accept cookies. Now I can get a cookie to be created when someone clicks on the button on my page, however this is not triggering the code for google analytics, the google analytics code is wrapped inside a javascript function which checks if the cookie exists and if it does then I want the google analytics code to run. Can someone please advise as I have been struggling with this for a few days now.

my cookie name is - websitecookie

Code:
<script type="text/javascript">
function cookieExists(websitecookie){

    if(!websitecookie || websitecookie=''){
        return false;
    }
    var cookies = document.cookie.split(";");
    var cookieNames = new Array();
    for( x in cookies){
        cookieNames.push(cookies[x].substr(0,cookies[x].indexOf("=")));
    }
    for(x in cookieNames){
        if(cookieNames[x] == websitecookie){
            return true;
        }
    }
return false;
}
if(cookieExists('websitecookie')){

   //my analytics code here

 
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

		var pageTracker = _gat._getTracker("**-******-1");
		pageTracker._setClientInfo(false);
		pageTracker._setAllowHash(false);
		pageTracker._setDetectFlash(false);
		pageTracker._setDetectTitle(false);
		pageTracker._trackPageview();
 


}
</script>
The above code is not working for me please advise.

My HTML button has two onClick functions, one to create the cookie and the second to cookieExists, have I done this correctly?

Code:
<input type="button" name="testcookieaccepted" value="Cookie" onClick="(Set_Cookie(), cookieExists() )" />