Jump to content

Pretty New To Javascript. Trying to pass Google Analytics Code to an ID. Help!


Recommended Posts

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  :confused:

 

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

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?

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.