Search code examples
phpgoogle-analyticsgoogle-analytics-api

How to group google sourceMedium and count same referral


I used Gapi.class.php to get data from google analytic.

Here is my php code

 $ga->requestReportData(xxxxx,array('sourceMedium','month','year'),array('sessions'), $sort_metric=array('month'), $filter=null, $start_date=$fromDate, $end_date=$toDate, $start_index=1, $max_results=10000);

I got like this

[dimensions:gapiReportEntry:private] => Array
                (
                    [sourceMedium] => l.facebook.com / referral
                    [month] => 06
                    [year] => 2016
                )
   [dimensions:gapiReportEntry:private] => Array
                (
                    [sourceMedium] => mail.google.com / referral
                    [month] => 06
                    [year] => 2016
                )
     [dimensions:gapiReportEntry:private] => Array
                (
                    [sourceMedium] => mbasic.facebook.com / referral
                    [month] => 06
                    [year] => 2016
                )

I want to know how can I group them and count,

Example 
        source   Count
         fb    =  100
        google =  50

Please.

Updated

enter image description here


Solution

  • Just remove date related dimensions i.e. month and year from your query to get aggregated numbers.

    So your query should look something like this to get expected output.

    $ga->requestReportData(xxxxx,array('sourceMedium','pagePath'), array('pageviews'), $sort_metric=array('pageviews'), $filter=null, $start_date=$fromDate, $end_date=$toDate, $start_index=1, $max_results=10000);
    

    Have a look at following links for your references.

    Google Analytics Dimension and Metrics

    Creating Reports

    Advanced Use Cases of Creating Reports