Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Is there a way we can add the ability to track what keywords the user has used to end up on our website? i.e. If they type in Software into google and our site pops up in the SEO list, when they click on it I want to log an entry into our db from logic in the home page to say the user used keyword 'Software' and also maybe offer some different features on the home page depending on the keyword used.

Thanks

share|improve this question

1 Answer 1

You can extract the keywords that the user searched from the HTTP_REFERER header.

Google uses the GET parameter "q" to send the terms that the user searched. Note that the terms are not always send, and the parameter q will be empty. This is based on each visitor's privacy settings, browser settings etc

You can use a function like this:

function extract_keywords($referral)
{
    $parsed = parse_url($referral);
    parse_str($parsed['query'], $query);

    //Check if referral is Google
    if (stripos($parsed['host'], 'www.google.') !== false)
    {
        if (!isset($query['q']{0}))
            return false;

        return trim($query['q']);
    }
}

$a = 'https://www.google.gr/url?sa=t&rct=j&q=test';

echo extract_keywords($a);

Or in real project, use the $_SERVER['HTTP_REFERER'];

echo extract_keywords($_SERVER['HTTP_REFERER'])
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.