Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying multiple metrics with Google Analytics API PHP Client

I am just trying to get product sales by number of items sold and the total revenue. I am using Google APIs client library for PHP: https://github.com/google/google-api-php-client.git

I am able to get one metric per product, eg I can list all the products that had a purchase in the last week. Or I can get the total revenue of sales per product in the last week. What I am trying to do is get both at the same time.

So what I tried was...

  $sessions = new Google_Service_AnalyticsReporting_Metric();
  $sessions->setExpression("ga:itemQuantity,ga:itemRevenue");

FYI the rest of the code is pretty much exactly the same as per the https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php#3_setup_the_sample example, I have obviously added in my details and it all works fine if I do just ga:itemQuantity or ga:itemRevenue.

Any documentation I ready says to just comma separate the metrics but it just dies, the whole page doesn't even load so I get no errors etc..

Is what I want to do possible? If so how do I go about doing it?

like image 343
Simon Pollard Avatar asked May 25 '16 11:05

Simon Pollard


2 Answers

I haven't tried using comma separated values, but what you can definitely do is create multiple metrics, like this:

$metric1 = new Google_Service_AnalyticsReporting_Metric();
$metric1->setExpression("ga:itemQuantity");

$metric2 = new Google_Service_AnalyticsReporting_Metric();
$metric2->setExpression("ga:itemRevenue");

Then, when you create the request, you can "attach" both of those metrics:

// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
...
$request->setMetrics(array($metric1, $metric2));
....

// Follow the samples that you mentioned for the rest

That should work, and in the response metric data you should now have an array with the values for both metrics.

Good luck!

like image 51
Micael Gustafsson Avatar answered Sep 30 '22 08:09

Micael Gustafsson


Approach 1:-

Create multiple metrics, like this:

// Create the Metrics object.
$metrics = array();
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:itemQuantity");
$sessions->setAlias("ItemQuantity");
$metrics[] = $sessions;

$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:itemRevenue");
$sessions->setAlias("ItemRevenue");
$metrics[] = $sessions;

and set Metrics in Google Report Request

// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setMetrics($metrics); // METRIC
$request->setDateRanges($dateRange);
$request->setDimensions(array($browser, $country));

Approach 2:-

// Create the Analytics API object
$analytics = new Google_Service_Analytics($client);

// Setup and run the query to Analytics API
$generalstats = $analytics->data_ga->get(
 'ga:XXXXXXXX', // analytics view id
 '2019-05-01', // start date
 '2019-05-31', // end date
 'ga:users,ga:sessions,ga:hits,ga:pageviewsPerSession,ga:avgSessionDuration,ga:bounceRate,ga:goalCompletionsAll,ga:goalConversionRateAll');

Simply replace the above parameters (in your case ga:itemQuantity,ga:itemRevenue) and display as you wish

// Output all API results
echo '<pre>';
print_r($generalstats);
echo '</pre>';
like image 23
Stack Programmer Avatar answered Sep 30 '22 08:09

Stack Programmer