Tuesday 18 September 2018

Google Analytics API PHP Class - Get most popular pages

The following shows example usage to get the most popular pages from Google Analytics using my PHP class for the GA API.
Calling the ->data() method without supplying the to and from dates will get data for the last month. $id is the profile id which is in the format ga:12345
$api->login($login, $password);
$data = $api->data($id, 'ga:pagePath', 'ga:pageviews,ga:uniquePageviews');
print_r($data);
Some example output for the above is as follows:
Array
(
    [/jquery-get-set-form-values/] => Array
        (
            [ga:pageviews] => 4451
            [ga:uniquePageviews] => 4130
        )

    [/google-analytics-api-and-php/] => Array
        (
            [ga:pageviews] => 3733
            [ga:uniquePageviews] => 2210
        )

    [/article/apache/restart-apache/] => Array
        (
            [ga:pageviews] => 3248
            [ga:uniquePageviews] => 3098
        )

    [/article/mysql/delete-all-data-mysql/] => Array
        (
            [ga:pageviews] => 2954
            [ga:uniquePageviews] => 2815
        )

    [/php-http-referer-variable/] => Array
        (
            [ga:pageviews] => 2822
            [ga:uniquePageviews] => 2660
        )

    [/check-uncheck-checkbox-jquery/] => Array
        (
            [ga:pageviews] => 2664
            [ga:uniquePageviews] => 2392
        )

    [/article/mysql/cross-table-update/] => Array
        (
            [ga:pageviews] => 2410
            [ga:uniquePageviews] => 2247
        )

    [/article/networking/open-firewall-msn-icq/] => Array
        (
            [ga:pageviews] => 2090
            [ga:uniquePageviews] => 1975
        )

    [/article/linux-unix-bsd/howto-check-md5-file/] => Array
        (
            [ga:pageviews] => 2064
            [ga:uniquePageviews] => 1994
        )

    [/mysql-find-records-in-one-table-not-in-another-revised/] => Array
        (
            [ga:pageviews] => 2023
            [ga:uniquePageviews] => 1927
        )

)
You could instead loop through the data like so:
$api->login($login, $password);
$data = $api->data($id, 'ga:pagePath', 'ga:pageviews,ga:uniquePageviews');
foreach($data as $dimension => $metrics) {
 echo "$dimension pageviews: {$metrics['ga:pageviews']} unique pageviews: {$metrics['ga:uniquePageviews']}\n";
}
Using the same example data, this would output:
/jquery-get-set-form-values/ pageviews: 4451 unique pageviews: 4130
/google-analytics-api-and-php/ pageviews: 3733 unique pageviews: 2210
/article/apache/restart-apache/ pageviews: 3248 unique pageviews: 3098
/article/mysql/delete-all-data-mysql/ pageviews: 2954 unique pageviews: 2815
/php-http-referer-variable/ pageviews: 2822 unique pageviews: 2660
/check-uncheck-checkbox-jquery/ pageviews: 2664 unique pageviews: 2392
/article/mysql/cross-table-update/ pageviews: 2410 unique pageviews: 2247
/article/networking/open-firewall-msn-icq/ pageviews: 2090 unique pageviews: 1975
/article/linux-unix-bsd/howto-check-md5-file/ pageviews: 2064 unique pageviews: 1994
/mysql-find-records-in-one-table-not-in-another-revised/ pageviews: 2023 unique pageviews: 1927
For more examples and to download my class refer to The Google Analytics API and PHP: A series

Related posts:

0 comments:

Post a Comment