Google Analytics Javascript code throwing Permission Denied error


I have a javascript code to use google analytics in an ASP.Net application. Following are the first two lines of it

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

If I embed this in an aspx file it works fine but if I put it in a function and call that function on a client side button click, it gives me a javascript error of "Permission Denied". Can someone help me resolve this?


Who is Participating?
Amandeep Singh BhullarConnect With a Mentor Commented:
jimmysaundersAuthor Commented:
Thanks AmanBhullar but that discussion is entirely about something else.
The tracking should be placed at the end of your page, just before the closing body tag (</body>) See here:

I am not sure why you would want to add it to a button click. This link shows how you would add tracking to an Anchor tag :
jimmysaundersAuthor Commented:
Was looking for a wrong solution.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.