Need help with creating a link with an "onclick=" event

Discussion in 'HTML & Website Design' started by mike33pt, Sep 10, 2008.

  1. #1
    Hi, i need a little advice please about creating a link to another site.

    I have a link as below with an onclick event for my google tracking. This event logs the outgoing click in google analytics to a value "outgoing/affiliatetextlink1.php" This is all working fine. In Google analyitics I see the outgoing clicks as "/outgoing/affiliatetextlink1.php"

    Here's the link:

    <a href="http://www.mydomain.com" rel="nofollow" onClick="javascript: pageTracker._trackPageview('/outgoing/affiliatetextlink1.php');">test</a>


    I also want to log this click as a conversion in google adwords.
    This would be a link for that

    <a href="http://www.mydomain.com" onclick="return ConversionCount();">test</a>

    My question is:

    How do I write the link so the onclick event logs both the page view and the conversion?

    Is this possible? Will it work?

    Thanks

    Mike
     
    mike33pt, Sep 10, 2008 IP
  2. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm thinking you'll need to take the onclicks out of the anchors, and set the js in an external file or in the <head> of the HTML page, and then give classes or id's to the links you consider Conversions. The two different onclick events would be two different functions in the same object, with the second one (conversion) only coming into play if the link has the class or id you added (I'm not sure if you'd need id's or classes cause I dunno if conversions each get something different or if they're all just "conversions") is present. Every link gets the pagetrack, so prolly you can have getElementByTagName("a") for that one.

    so one function is
    pageTracker._trackPageview()
    and the other is
    ConversionCount()
    which would have if/else or case or something that checks if the link also belongs in the group you call "conversions".


    I'm not far enough in Javascript to go any further : ( I'm moving too slow in it. But generally we try to keep javascript out of the HTML anyway... might be a bit harder with add-on stuff like Google Analytics but it can be done.

    if you don't have an eventloader you may need to stick the reference to the new javascript at the bottom of the HTML page instead of the <head> so that it doesn't even load until the whole page has loaded (so that it doesn't go trying to find elements who haven't yet loaded), which is why those Urchin Tracker scripts are usually at the bottom of HTML pages.
     
    Stomme poes, Sep 11, 2008 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    My advice - don't mess with any of that scripted rubbish. Just send the anchor value direct to that tracking php file (adjusting said file to handle that) with said file redirecting the user to the link.

    <a href="/tracking.php?target=http%3A%2F%2Fwww.mydomain.com" rel="nofollow" />

    Then just handle the rest server-side since you are likely passing that "affiliatetextlink1.php" as Ajax, right? Save yourself the headache and rubbish unneeded scripting.
     
    deathshadow, Sep 11, 2008 IP