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.



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'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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