user3605780
user3605780

Reputation: 7072

get query parameters from google with referrer javascript

Is it possible to get the query parameters from google searches?

I.e. if someone googled bicycles the url becomes:

https://www.google.es/search?q=bicycles......

If you then come in the search results and someone clicks to your page you cannot see the query parameters with document.referrer it will only show

 https://www.google.es/

Is there a way to know what a visitor searched before coming to your site?

Upvotes: 2

Views: 3835

Answers (1)

Clinton
Clinton

Reputation: 1196

Late response, but I have recently been doing some research and thought that this information may be interesting to other stackoverflow visitors who end up on this page.

Prior to 2016 you could get the url parameters using PHP from the referral data when visitors came to your site via organic search by using server variables:

function get_search_query(){
    $query = $_SERVER['QUERY_STRING'];
    return (strlen($query)? $query: 'none');
}

or

function get_search_query() {
    $query = false;
    $referrer = $_SERVER['HTTP_REFERER'];
    if (!empty($referrer)) {
        //Parse the referrer URL
        $parts_url = parse_url($referrer);
        // Check if a query string exists
        $query = isset($parts_url['query']) ? $parts_url['query'] : '';
        return (strlen($query)? $query: false);
    }
    return $query;
}

However, Google and other search engines have since made it impossible to view the query parameters from organic search and any script like that above returns a blank query string. This is true whether you are logged in to Google or not.

This is unfortunate because this removes valuable information like the the keywords used to find your site. The information still can be found in Google Search Console, but this provides Googles interpretation of what you should see and it is not as direct as getting the information immediately when a visitor hits a page after an organic search.

I am not aware of there being any way to still get organic query parameters.

Upvotes: 5

Related Questions