Jump to content

JS strings in strings in event handlers (such as onClick)


Wuhtzu

Recommended Posts

Hi everyone

 

I am playing around with Google Analytics asynchronous tracking code and facing a "problem" where I have to have strings within strings in java script called by event handlers such as onClick.

 

This piece of java script needs to be called when the even handler onBeforeUnload fires:

 

onbeforeunload="timeTracker._recordEndTime(); _gaq.push(['java script code']);"

 

So _gaq.push(['java script code']) takes a string of java script code and enqueues it for execution.

 

The piece of codes needing to be executed is this:

 

function(){ eTr = _gat._getTrackerByName('eventTracker'); timeTracker._track(eTr, undefined, document.title);}

 

As you can see the code above function(){} has a string in it. How should those two pieces of code be merged so that everything gets interpreted correct?

 

onbeforeunload="timeTracker._recordEndTime();
     _gaq.push(['function(){ eTr = _gat._getTrackerByName('eventTracker'); timeTracker._track(eTr, undefined, document.title);}']);"

I cannot mix " and ' since the html tags use ". The above with 'code 'string' more code' does (obviously) not work and escaping the quotes \' does not work either.

 

Any suggestions?

 

Best regards

Wuhtzu

 

Link to comment
Share on other sites

What I needed I found documented here: http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html#PushingFunctions

 

The function just has to be passed to _gaq.push() like a function and not a string:

 

onbeforeunload="timeTracker._recordEndTime();
     _gaq.push([function(){ eTr = _gat._getTrackerByName('eventTracker'); timeTracker._track(eTr, undefined, document.title);}]);"

Link to comment
Share on other sites

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.