Sergiu Pirlici
Sergiu Pirlici

Reputation: 140

how to get search queries using google-api-php-client

I have read the instructions from the Hello Analytics API: PHP quickstart for service accounts and all works ok, but I need some other behavior of the function getResults

In the exampe it returns the number of sessions in the specified period

function getResults(&$analytics, $profileId) {
  // Calls the Core Reporting API and queries for the number of sessions
  // for the last seven days.
   return $analytics->data_ga->get(
       'ga:' . $profileId,
       '7daysAgo',
       'today',
       'ga:sessions');
}

Is it possible to modify this function to return the search keywords instead of sessions?

Upvotes: 0

Views: 213

Answers (1)

Eike Pierstorff
Eike Pierstorff

Reputation: 32760

You cannot get keywords instead of sessions - keywords is a dimension, sessions is a metric, and at least one metric is required. So while you cannot get keywords instead of sessions you can get both keywords and sessions, and the sessions metric will be broken down by keyword.

For that you need to pass in an options array with the dimension:

$optParams = array(
    'dimensions' => 'ga:keyword',
);
return $analytics->data_ga->get(
       'ga:' . $profileId,
       '7daysAgo',
       'today',
       'ga:sessions',
       $optParams

);

ga:keyword is the organic keyword; keep in mind that the value is not set for ssl secured searched (now the default), so for 90+ % of orgnaic sessions the value will be "not set".

Upvotes: 2

Related Questions