WillUK Posted July 2, 2012 Share Posted July 2, 2012 Hi I have recently taken up Javascript after a couple of years away from coding, so am finding this pretty tough to get my head around. What I am trying to do is to set up the Google Analytics tracking code in an external Javascript file. I do not want the analytics code to be outputted/set on the page unless the user chooses to accept it (This is relating to recent changes to the cookie legislation.). If the user does not choose to accept Analytics, the script runs without setting the analytics code. I decided to use an event handler to deal with this, and I have stored the anlaytics code itself in a function, which is then passed to another function - disp_confirm() - in order for it to have an id applied to it. The plan then is to call this id (and therefore call the event handler) from the just above the closing body tag in the html page (I decided against putting the analytics code in the header because I was under the impression that the event handlers wouldn't process successfully if the code was implemented here). The problem is, I don't know whether this will work, or if I am trying to do something that cannot be done with Javascript. The idea of passing a function to another function doesn't sit well with me - so I'm sure I must be way off the mark with this....I'm not sure if I'm using the correct syntax to pass one function to another either Anyway, here's my code. I'd really appreciate some advice on this, especially if I am way off mark with my approach!! Thanks window.onload = disp_confirm; function disp_confirm() { var r=confirm("This website uses Google Analytics to monitor website use which helps us offer a better service to you. However, No personal data is stored. If you wish to proceed with cookies, click ok. Alternatively click cancel.") if (r==true) { var gas = passString.analytics(); document.getElementById('gas'); } else { alert("Google cookies have been disabled") } } function analytics() { var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXX-1']); _gaq.push(['_trackPageview']); (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); }); } Any help advice would be really appreciated.... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/265110-pretty-new-to-javascript-trying-to-pass-google-analytics-code-to-an-id-help/ Share on other sites More sharing options...
haku Posted July 2, 2012 Share Posted July 2, 2012 At a quick glance, I think you'd want this: if (r) { analytics(); } Quote Link to comment https://forums.phpfreaks.com/topic/265110-pretty-new-to-javascript-trying-to-pass-google-analytics-code-to-an-id-help/#findComment-1358556 Share on other sites More sharing options...
WillUK Posted July 2, 2012 Author Share Posted July 2, 2012 Thanks Haku - I considered your approach, but wouldn't I need to somehow apply an ID to it in order to be able to call it from inside the html? At the moment I've got in my html file: (This is in the head section) <script type="text/javascript" language="Javascript" src="javascript/cookie.js"> </script> Then just above the closing body tag: <div id="gas"> </div> Are you saying that I don't need to assign an id? Quote Link to comment https://forums.phpfreaks.com/topic/265110-pretty-new-to-javascript-trying-to-pass-google-analytics-code-to-an-id-help/#findComment-1358566 Share on other sites More sharing options...
haku Posted July 2, 2012 Share Posted July 2, 2012 No, google analytics is purely javascript, it doesn't require any markup (html). Quote Link to comment https://forums.phpfreaks.com/topic/265110-pretty-new-to-javascript-trying-to-pass-google-analytics-code-to-an-id-help/#findComment-1358681 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.