Google Analytics PHP API (GAPI) returns no keywords

1.1k Views Asked by At

I'm practicing to work with GAPI and so far, I got most things to work.
At the moment I'm stuck on displaying the 5 most used keywords. All my code returns is an empty array.
I've been trying to find a solution, but so far with no success.
This is my code:

<?php
$ga->requestReportData(GA_PROFILE_ID, 'searchKeyword', 'pageviews', '-pageviews', null, null, null, 1, 5);
foreach($ga->getResults() as $result) {

    echo "$result - ".$result->getpageViews();

}
?>
2

There are 2 best solutions below

1
On BEST ANSWER

Eventually, I found it myself.
Appearantly, searchKeyword is for internal searches, and keyword for source searches.

Source:
Keyword: https://developers.google.com/analytics/devguides/reporting/core/dimsmets/trafficsources#ga:keyword
searchKeyword: https://developers.google.com/analytics/devguides/reporting/core/dimsmets/internalsearch#ga:searchKeyword

1
On

Looks like you are sending in strings for attributes that the docs say should be arrays.

$ga->requestReportData(GA_PROFILE_ID, 'searchKeyword', 'pageviews', '-pageviews', null, null, null, 1, 5);

Try using arrays as the documentation for GAPI recommends:

$ga->requestReportData(GA_PROFILE_ID, array('searchKeyword'), array('pageviews'), array('-pageviews'), null, null, null, 1, 5);