Possible to target banners to people coming from certain keywords?

Discussion in 'PHP' started by BrendanGall, Jul 7, 2008.

  1. #1
    Is it possible to create a script in php (or other) that could target a banner or other advertising to people coming from google through a certain keyword?

    I want to show the banner for one product when people people visit my site through "Keyword A" to see one banner, and the people coming from "keyword b" to see a completely different banner ad. Like targeting the ads by what the visitor is searching for.

    Can this be done?
     
    BrendanGall, Jul 7, 2008 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    Yes, you can utilize $_SERVER['HTTP_REFERER'] in this way.

    For example a query 'video converter download' was searched, then the url will be something like:
    "http://www.google.com/search?sourceid=navclient&aq=t&ie=UTF-8&rlz=1T4GGLJ_enPK279PK279&q=video+converter+download"

    Now when it comes to your script:

    $info = parse_url($_SERVER['HTTP_REFERER']);
    
    if($info['host'] == 'www.google.com') {
        #good, utilize the keywords
        $pos = strpos($info['query'],"q=");
        if($pos === FALSE) {
              #handle error, no keywords
        }
             
        $keywords = substr($info['query'],$pos+2,1024);
        print $keywords;
    }
    Code (markup):
    It will probably print something like

    I hope it helps.

    regards
     
    Vooler, Jul 7, 2008 IP
  3. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Here you go, I just wrote this quickly.

    Put this at the top of your file:
    
    <?php
    // Change this to your default banner if there are no keywords
    $sBanner = '<img src="default.gif">';
    
    // Change this to suit, the format is: 'keyword' => 'output'
    $aBanners = array('test'  => '<img src="test.gif">',
                      'test2' => '<img src="test2.gif">'
                     );
    
    if ( isset($_SERVER['HTTP_REFERER']) )
    {
        $aUrl = parse_url($_SERVER['HTTP_REFERER']);
        switch($aUrl['host'])
        {
            case 'search.yahoo.com':
                $sVar = 'p';
            case 'search.live.com':
            case 'www.google.com':
            case 'google.com':
                $sVar = 'q';
                break; 
        } 
        
        if ( isset($sVar) )
        {
            $aQuery = explode('&', $aUrl['query']); 
            foreach($aQuery as $sQuery)
            {
                list($sName, $sValue) = explode('=', $sQuery);
                    
                if ( $sName == $sVar )
                {
                    $aKeywords = explode('+', $sValue);   
                }
            }
                         
            foreach($aKeywords as $sKeyword)
            {
                if ( array_key_exists($sKeyword, $aBanners) )
                {
                    $sBanner = $aBanners[$sKeyword];
                    break;
                }
            }
        }
    }
    ?>
    
    PHP:
    Then where you want your banner displayed place this:
    
    <?php echo $sBanner; ?>
    
    PHP:
    I hope that helps :).
     
    clarky_y2k3, Jul 7, 2008 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    Very nice work. I will benifit many users surely.

    regards
     
    Vooler, Jul 7, 2008 IP
  5. BrendanGall

    BrendanGall Peon

    Messages:
    225
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That looks awesome! cant wait to try it.

    But a question. Now if a user visits from google initially through a keyword, but then clicks on another page inside my site. Will the visitor still see the correct ad if the referer is technically different?

    Thanks!
     
    BrendanGall, Jul 8, 2008 IP
  6. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #6
    No, this is only for the landing page but I could do that for you too, if you like?
     
    clarky_y2k3, Jul 8, 2008 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #7
    clarky: Nice script, but perhaps instead of exploding by & and =, use the parse_str function designed for this, I would definitely recommend you use the second parameter.

    Here's a link: http://php.net/manual/en/function.parse-str.php
     
    Danltn, Jul 8, 2008 IP