Google Analytics Javascript link tracking code

Discussion in 'Web Design and Development' started by Brendon Bauer, Mar 3, 2008.

  1. Brendon Bauer macrumors 6502

    Joined:
    May 14, 2007
    Location:
    Good 'ol USofA
    #1
    Ok, so here's the deal:

    For every video posted on my website I have three links: One that uses a script to pop it open in a new window, one to download it, and one to view it directly at Google Video. In each link I have included this code to track the usage of the videos:

    Code:
    <a href="http://www.example.com/examplevideo" onClick="javascript: pageTracker._trackPageview('/video/02-23-08-download');">Download</a>
    My problem comes in on the link that pops the video into a new window. I'm not sure how to incorporate this code
    Code:
    javascript: pageTracker._trackPageview('/video/02-23-08-download');
    into this code
    Code:
    onClick="poptastic(this.href); return false;">
    .

    My try at it was to do it like this:
    Code:
    onClick="poptastic(this.href); return false; javascript: pageTracker._trackPageview('/video/02-23-08-view');">
    The problem is that google analytics is tracking the download and direct links but as far as I can tell not the links that open a new window for the user to view the video in (which is the most important). Did I do the code right, or did I do something silly?
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Once the code hits return false; it stops, so the code after that doesn't get executed. Also get rid of the javascript: snippet, it's only getting in the way. That leaves you with
    Code:
    onClick="poptastic(this.href); pageTracker._trackPageview('/video/02-23-08-view');">
     
  3. Brendon Bauer thread starter macrumors 6502

    Joined:
    May 14, 2007
    Location:
    Good 'ol USofA
    #3
    Wirelessly posted (LG-CU500 Obigo/WAP2.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Link/6.3.1.20.06.3.1.20.06.3.1.20.0)

    Could you put the return false back in? I think it saves people without javascript so they are just redirected to the movie. Not sure though.
     
  4. Brendon Bauer thread starter macrumors 6502

    Joined:
    May 14, 2007
    Location:
    Good 'ol USofA
    #4
    What I meant by that was
    Code:
    onClick="poptastic(this.href); pageTracker._trackPageview('/video/02-23-08-view'); return false;">
    
    Would that still work fine, and if they don't have javascript enabled it would just go straight to the movie?

    Also, could I remove that "javascript:" portion from all links? Even the ones where the trackPageview is the only onClick part? As in, there is no poptastic script. Google Analytics just gave me the code with the "javascript:" part in there, so I wasn't sure if I'd need that.

    Would this be a normal tracking code? (without javascript:)
    Code:
    onClick="pageTracker._trackPageview('/video/02-23-08-view');">
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    Yes, you can remove the 'javascript:' portion from the onclick attributes.

    I think you can add the "return false;" to the end for the right effect. I'd have to try it out to be sure. I don't know exactly what each piece of JavaScript you have there does so don't want to say one way or another, but easy enough thing to try out.
     
  6. Brendon Bauer thread starter macrumors 6502

    Joined:
    May 14, 2007
    Location:
    Good 'ol USofA
    #6
    Ok, I've removed the javascript: portion and have moved the return false portion to the end of the onClick code. Everything works great! We'll see in the next 24-48 hours whether or not the tracking is working correctly, but I assume it should. Thanks again for your help :)
     
  7. Brendon Bauer thread starter macrumors 6502

    Joined:
    May 14, 2007
    Location:
    Good 'ol USofA
  8. djemmers macrumors newbie

    Joined:
    Jan 15, 2009
    #8
    goal tracking problem

    Hi

    I have a simular problem, did your get tracked in the end?
    my setup :
    the links to external sites I want to track:
    HTML:
    <a target="_blank" onclick="pageTracker._trackPageview('/doelsite=24');" href="http://www.example.com/ ">http://www.example.com/</a>
    where the number varies.
    my goal is set up as follows:
    regular expression
    /doelsite=

    so I want to track all exit links to other sites (all links are made up like the example only with different numbers after /doelsite=
    what am I doing wrong?
     

Share This Page