views:

78

answers:

4

This is an update, since Gaby was able to help me with my issue. This now works

DEMO TIME! http://media.apus.edu/it/ref/page1.htm

In a nutshell - this code will send the referring URL from page 1 to page 3 dynamically when you click the link. Why is this important?

We're not able to place analytical code (google analytics) on page 3 since it is a third party website. We should now be able to track leads from page 1, which is also a third party website, when they click to page 2 (our website that has google analytics) and then click a link to page 3 (third party site that can't by customized) as this site uses its own analytic code to track campaigns.

Page 1: Click link and it will take you to page 2.

Page 2: Page 2 knows you came from page 1 and will pass the referring URL from page 1 to page 3 when you click it. Also, the link is assigned a class that will call the function, because we are not able to assign an onclick to links in our CMS. Also, Gaby helped resolve the issue of having me use the same ID more than once and now I only need to use a class assigned to the link. No more ID is needed... Thank you! This will enable me to assign any link on the page to pass the referring URL if it's assigned the css class.

I'm sure someone out there will benefit from this code... please pass it along!

PS. I'm aware this code won't work if page 1 comes from a secure site.

Evan

+1  A: 

You could give your links a distinct class, like "specialTrick":

$('a.specialTrick').each(function() {
  // do the special trick
});

Note that relying on "REFERER" is pretty fragile and probably not a good idea.

Pointy
+1. `Note that relying on "REFERER" is pretty fragile and probably not a good idea.` - more specifically, most proxies remove the REFERER header and some browsers/browser plugins allow you to change it to whatever value you like. So it shouldn't be relied upon at all.
Andy E
if you look at his source code, they already do ..
Gaby
@Gaby you're right - I didn't go past the first page when I looked!
Pointy
+1  A: 

No need to access the element by the ID since you want the same logic applied to multiple items.. (and you already have access through the click event ..)

your code should be

$(document).ready(function()
  {
    $(".referringClass").click(function(event)
      {
        if (document.referrer != '')
          {
            var testing=document.referrer;
            var href = $(this).attr('href');
            $(this).attr('href', href + "?campaign-id=" + testing );
          }
      }
     );
 });
Gaby
I tested your code and it worked great! Thank you Gaby!Everyone can test it here to see the change working. I'm now able use this trick on any link as long as it has the CSS class assigned to it.http://media.apus.edu/it/ref/page1.htmNote: I know this trick won't work if the referring URL comes from a secure website. We're not able to place analytic code on the third page, but we are able to pass a campaign ID to the third page by passing it as a referring URL, so we should now know where the user originated from when they were taken to page 3. This was amazing amazing help Thank you! :)
Evan
@Evan, no need to add to the question title the [SOLVED] .. You can accept the answer that fits your needs by clicking on the checkbox on the left of it, and the question gets automatically marked as answered by the StackOverflow system...
Gaby
forgive me. i've updated the title to not have it.
Evan
A: 

you don't need the id for anything - you already have everything you need when you assign the click by getting the elements by className. By assigning the clicks you can access 'this' which gives access to all the properties of the element being clicked on.

So myTextField is actually 'this' e.g.

oldHref = $(this).attr('href');
$(this).attr('href', oldHref + "?campaign-id=" + testing);

or something like that anyway

matpol
A: 

Sorry to say I can't test and confirm this at the moment, but you should be good using event.target

http://docs.jquery.com/Events/jQuery.Event#event.target

It'd look something like:

$(document).ready(
    function()
    {
        $(".referringClass").click(
            function(event)
            {
                if (document.referrer != '')
                var testing=document.referrer

                var myTextField = event.target;
                myTextField.href=myTextField.href + "?campaign-id=" + testing;
            }
        );
    }
)
Paul