PHP script - tracking keywords and inserting in aff link

Discussion in 'PHP' started by Carnation25, Jul 1, 2009.

  1. #1
    I need a PHP tracking script for my website, that is available to track keywords which the visitors came from, and insert the keyword at the subID variable of the affiliate link. At the same time the affiliate links must be hidden.

    Here is an example:

    Visitor comes from: http://www.google.com/search?q=mone...&rls=org.mozilla:bg:official&client=firefox-a

    Keyword is "money"

    Then visitors leaves through my affiliate link, which is like this one:

    http://track.affiliatesite.com/hit.php?w=xxxxxx&s=yy&ck=site1&kw={$kw}

    The {$kw} is a variable. I want the script to be able to take the word money, and put it on the place of the variable {$kw}.

    At the same time I want my affiliate not to be visible. Currently they are hidden by the PHP redirect method. It is not a problem if the script needs other redirecting method.

    Does somebody knows of something like this?
     
    Carnation25, Jul 1, 2009 IP
  2. tguillea

    tguillea Active Member

    Messages:
    229
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #2
    This shouldn't be too hard but i need a few more details...
    can you edit hit.php? Are you a programmer yourself? What is the redirecting setup?

    the general idea of it is to use getenv("HTTP_REFER") and then split the keyword out of there. Once you got it use str_replace and your good to go. I'll show you a quick demo if you answer those questions
     
    tguillea, Jul 1, 2009 IP
  3. www.amagit.com

    www.amagit.com Peon

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This could work:

    <?php
    $url = $_SERVER['HTTP_REFERER'];
    $start = strpos($url, "q=") + 2;
    $end = strlen($url);
    if(strpos($url, "+", $start) !== false)
    	$end = strpos($url, "+", $start);
    else if(strpos($url, "&", $start) !== false)
    	$end = strpos($url, "&", $start);
    
    $kw = substr($url, $start, $end - $start);
    print "<a href=\"http://track.affiliatesite.com/hit.php?w=xxxxxx&s=yy&ck=site1&kw={$kw}\">link</a>";
    ?>
    Code (markup):
    Replace the link text with your ad. This will insert the first search term from google into the link url at {$kw}
     
    www.amagit.com, Jul 1, 2009 IP
  4. Carnation25

    Carnation25 Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm not a programmer myself, the only thing I can do is look at the codes and have some idea what they are meant to do ;)

    I tested amagit's code:
    1. When I clicked the link a blank page opened with a link to the merchant site, which I needed to click to go there.
    2. In stats page the keyword was the url of the script, not the keyword I reached that page. I believe this is caused by 1.

    However, this script is very close to be perfect, it needs some modifications to clear the issues I pointed.


    Btw, is it possible this script to extract the keyword from the __utmz cookie (that g analytics sets) and insert it in the link?
     
    Carnation25, Jul 4, 2009 IP
  5. Carnation25

    Carnation25 Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm not a programer myself.

    I can't edit hit.php - it is part of the affiliate link, so hit.php is not located on my webserver.

    Redirecting is like this:

    <?php
    header( 'Location: http://track.affiliatesite.com/hit.php?w=xxxxxx&s=yy&ck=site1&kw={$kw}' ) ;
    ?>
    Code (markup):
     
    Carnation25, Jul 4, 2009 IP
  6. www.amagit.com

    www.amagit.com Peon

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I don't completely understand what you want. The page won't work unless someone comes from Google because $_SERVER['HTTP_REFERER']; would not contain keywords if one just loads the page. It prints a link because that's the affiliate link that links to your ad redirection page which requires a keyword. Someone would click the ad, leave your site, end up on the ad site, and you would presumably get paid by your affiliate.

    If you want visitors to be immediately redirected then yes you would use header. Yet, I don't understand why you would force your visitors to travel to your affiliate's site. To do that you would use the header command just as you did above instead of the print command.

    To test the script without coming from Google just set $url equal to your sample url after or instead of
    $url = $_SERVER['HTTP_REFERER'];
    Code (markup):
     
    www.amagit.com, Jul 4, 2009 IP