Link to home
Start Free TrialLog in
Avatar of evibesmusic
evibesmusicFlag for United States of America

asked on

How can I apply two (2) 'onclick' events to a single link?

Experts this is a two part question:

1.  I need to figure out how to use two onclick events in the same link code.
2.  I need to insert php coding inside the second onclick event which gives each link a unique name that can be tracked using Google Analytics.

I use php to dynamically create file links on my page.  Here is an example link:

<a href="'.$getinfo['FilePath'].'" title="'.$getinfo['File'].'" onclick="return confirm(&quot;Notice:  You are about to view privileged information.)">

The code above creates a link using the 'FilePath' stored in the database which leads to an Excell file.  It also gives each link a unique title by using the value of 'File' stored in the database.  On clicking this link it also creates a popup window which states "Note: You are about to view privileged information."

First Question:
How can I add a second onclick event which adds the ability for Google Analytics to be able to track the clicking of this file link.  Google Analytics says to use the following onclick event example to do so:

onClick="javascript: pageTracker._trackPageview('/downloads/map');"

Second Question:
Given the provided Google Analytics code, how can I insert php code into this javascript so that each link is tracked with a unique name?  This is what I was thinking:

onClick="javascript: pageTracker._trackPageview('<?php .$getinfo['File']. ?>');"

I think this may be an easy question but, I just wanted to make sure I provide the information necessary to answer it.

Cheers!
Avatar of TedInAK
TedInAK
Flag of United States of America image

This should work:
<a href="'.$getinfo['FilePath'].'" title="'.$getinfo['File'].'" onclick="pageTracker._trackPageview('<?php .$getinfo['File']. ?>'); return confirm(&quot;Notice:  You are about to view privileged information.)">

Open in new window

SOLUTION
Avatar of TedInAK
TedInAK
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, wait...just realized that you only want to do the analytics if the user chooses "Yes" (or "Ok" or whatever...my JS is a little rusty).  Nevermind.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, long day...the link should be:

<a href="'.$getinfo['FilePath'].'" title="'.$getinfo['File'].'" onclick="return ConfirmClick();">
Better thing use event handlers to attach multiple events of onclick on same object
Avatar of evibesmusic

ASKER

@TedInAK:

Thanks for your suggestions.  I will implement them and let you know if I have any issues.  Cheers!

@manjunathub:

Can you give an example of an event handler that you would use to accomplish this?  As a learning experience, can you give insight into why you feel this way is better to use?  Cheers!
You're welcome.  I believe manjunathub was intending to say "event listeners".   I'd suggest googling "javascript event listeners" if you want more info, because there's much stuff written on it and is much more eloquent (and correct) than I could hope to say.
@TedInAK:

Thanks TedInAK I've implemented your code and am now waiting for the Analytics system to verify that it is working correctly.  I'll get back to you over the weekend I'm sure.

Cheers!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to the both of you for your assistance.  Learned about event handlers and how to include mutliple onclick events into one link.

Cheers and Happy Holidays from the US.

EVibesMusic
You're most welcome. :-)  Merry Christmas to you and all.