erebusnz / gapi-google-analytics-php-interface

GA:PI() - Google Analytics PHP Interface
167 stars 86 forks source link

Is this possible to fetch events? #128

Closed tttp closed 8 years ago

tttp commented 9 years ago

Hi,

I'm using google analytics to track events, and I'd like to pull the results, but not sure if gapi is only fetching visitors or if it can retrieve events too. Is there an example already?

rcknr commented 9 years ago

Yes, it's possible. This class provides the interface to query Google Analytics Core Reporting API, which you can use to query all kinds of data this API supports. Take a look at Query Explorer tool from Google to see the possibilities. The example is something like:

$ga = new gapi($service_account_email, $path_to_p14_file);
$ga->requestReportData(
                $tracker_id, // GA tracker ID
                array('eventCategory', 'eventAction', 'eventLabel'), // Metrics
                array('totalEvents', 'uniqueEvents', 'eventValue'), // Dimensions
                array('-totalEvents'), // Sorting, events number descending
                $filters, // Filters, e.g. 'ga:eventAction==MyAction', see Query Explorer for more
                $date_from, // The rest is self-explanatory I suppose
                $date_to,
                $page,
                50
            );