Results 1 to 4 of 4

 

Thread: setting cookies based on web url - translating to tracking ids?

  1. #1
    Registered User

    Status
    Offline
    Join Date
    Jan 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts


    I wonder if anyone can help with the following:

    I want to set a cookie when a user comes to my site based on the url they use to enter my site, e.g. if the come via http://www.mysite.co.uk/?ref=adwords, I want the cookie to be "adwords"

    I then want to replace all the tracking-id parts of all the affiliate links, i.e. http://www.awin1.com/tclick.php?id=U...ef=TRACKING-ID with http://www.awin1.com/tclick.php?id=U...ickref=adwords

    etc.

    How can I achieve this with minimum server load. Is there a javascript way of achieving this, i.e. first setting the cookie based on the url and then replacing the tracking-id parts of the affiliate links.

    Thanks.

  2. #2
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Stafford, UK
    Posts
    322
    Thanks
    1
    Thanked 4 Times in 2 Posts
    Hi,

    This is possible with JavaScript / DHTML....

    I want to set a cookie when a user comes to my site based on the url they use to enter my site, e.g. if the come via http://www.mysite.co.uk/?ref=adwords, I want the cookie to be "adwords"
    Here's a handy JS function to read GET parameters and make them available to the rest of your script as normal variables:

    Code:
    function parsequerystring()
    {
       var qs=location.search.substring(1);
       var s1=qs.split("&");
       for(var i=0;i < s1.length;i++)
       {
        var s2=s1[i].split("=");
        window[s2[0]]=unescape(s2[1]);
       }
    }
    parsequerystring();
    Having called parsequerystring(), you can then set the cookie based on the value of the "ref" parameter:

    Code:
    document.cookie = ref;
    I then want to replace all the tracking-id parts of all the affiliate links
    To start with, I would suggest creating all your links containing a generic tracking ID that basically means "no javascript" - for example, "unknown". You can then change all tracking IDs based on the cookie set using the script above like this:

    Code:
    <script>
    function SetTrackingID()
    {
    document.body.innerHTML.replace(/unknown/gi,document.cookie);
    }
    </script>
    Don't forget to call the above function using the onload event of the HTML body:

    Code:
    <body onload='javascript:SetTrackingID();'>
    Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!

  3. #3
    Registered User

    Status
    Offline
    Join Date
    Jan 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks dmorison - I didn't have a chance to test this out until now, but can't get it to work. Here's what I've done:

    PHP Code:
    <html>
    <
    head>
    <
    script language="JavaScript">
    function 
    parsequerystring()
    {
       var 
    qs=location.search.substring(1);
       var 
    s1=qs.split("&");
       for(var 
    i=0;s1.length;i++)
       {
        var 
    s2=s1[i].split("=");
        
    window[s2[0]]=unescape(s2[1]);
       }
    }
    parsequerystring();
    </script>
    <script language="JavaScript">
    document.cookie = ref;
    function SetTrackingID()
    {
    document.body.innerHTML.replace(/unknown/gi,document.cookie);
    }
    </script>
    </head>
    <body onload='java script:SetTrackingID();'>
    <a href="http://www.dgm2.com/m/anymerchant/t.asp?A=0000&I=0000&nwk=unknown">A Link</a>
    </body>
    </html> 
    Any ideas what I've done wrong? I uploaded this page as test.htm, and called http://www.mydomain.com/test.htm?adwords, but the link dind't change as I expected.

    Thanks.

  4. #4
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Stafford, UK
    Posts
    322
    Thanks
    1
    Thanked 4 Times in 2 Posts
    Hi,

    Sorry - silly oversight on my part.... the result of the replace operation must be reassigned back to document.body.innerHTML...

    Code:
    document.body.innerHTML = document.body.innerHTML.replace(/unknown/gi,document.cookie);
    ...should do the trick!

    Also, the parsequerystring() function expects name=value pairs; so you will need to use ?ref=adwords instead of just ?adwords

    Hope this helps!
    Last edited by dmorison; 25-02-05 at 07:30 PM.
    Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2