├── .gitignore
├── doc
└── debug.png
├── Resources
├── config
│ ├── routing_dev.yml
│ └── services.yml
└── views
│ └── Debug
│ └── connect.html.twig
├── GoogleAnalyticsApi.php
├── composer.json
├── DependencyInjection
├── Configuration.php
└── GoogleAnalyticsApiExtension.php
├── LICENSE
├── Controller
└── DebugController.php
├── README.md
└── Service
└── GoogleAnalyticsService.php
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor/
2 | composer.lock
3 |
--------------------------------------------------------------------------------
/doc/debug.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mediafigaro/google-analytics-api-symfony/HEAD/doc/debug.png
--------------------------------------------------------------------------------
/Resources/config/routing_dev.yml:
--------------------------------------------------------------------------------
1 | google_analytics_api:
2 | resource: "@GoogleAnalyticsApi/Controller/"
3 | type: annotation
4 | prefix: /analytics-api
5 |
--------------------------------------------------------------------------------
/GoogleAnalyticsApi.php:
--------------------------------------------------------------------------------
1 | =2.3",
16 | "symfony/twig-bundle": ">=2.0",
17 | "twig/twig": ">=1.6"
18 | },
19 | "require-dev": {
20 | "phpunit/phpunit": "6.5.*"
21 | },
22 | "autoload": {
23 | "psr-4": {
24 | "MediaFigaro\\GoogleAnalyticsApi\\": "."
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/DependencyInjection/Configuration.php:
--------------------------------------------------------------------------------
1 | root('google_analytics_api');
22 |
23 | $rootNode
24 | ->children()
25 | ->scalarNode('google_analytics_json_key')
26 | ->end()
27 | ;
28 |
29 | return $treeBuilder;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Resources/views/Debug/connect.html.twig:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Analytics API
6 |
7 | Results on the 30 last days
8 |
9 | {{ data.sessions|number_format(0, '', ' ') }} sessions
10 | {{ data.bounce_rate|number_format(2, '.', ',') }} % bounce rate
11 | {{ data.avg_time_on_page|number_format(2, '.', ',') }} s average time on page
12 | {{ data.page_view_per_session|number_format(2, '.', ',') }} page view per session
13 | {{ data.percent_new_visits|number_format(2, '.', ',') }} % new visits
14 | {{ data.page_views|number_format(0, '', ' ') }} page views
15 | {{ data.avg_page_load_time|number_format(2, '.', ' ') }} s average page load time
16 |
17 | Google_Client
18 |
19 | {{ dump(client) }}
20 |
21 | Google_Service_AnalyticsReporting
22 |
23 | {{ dump(analytics) }}
24 |
25 | Google_Service_AnalyticsReporting_GetReportsResponse
26 |
27 | {{ dump(report) }}
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 MEDIA.figaro
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/DependencyInjection/GoogleAnalyticsApiExtension.php:
--------------------------------------------------------------------------------
1 | processConfiguration($configuration, $configs);
24 |
25 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26 | $loader->load('services.yml');
27 |
28 | $container->setParameter('google_analytics_api.google_analytics_json_key', $config['google_analytics_json_key']);
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Controller/DebugController.php:
--------------------------------------------------------------------------------
1 | get('google_analytics_api.api');
26 |
27 | $client = $analyticsService->getClient();
28 |
29 | $analytics = $analyticsService->getAnalytics();
30 |
31 | // demo purpose : dump of the result object
32 |
33 | // Create the DateRange object
34 | $dateRange = new Google_Service_AnalyticsReporting_DateRange();
35 | $dateRange->setStartDate("30daysAgo");
36 | $dateRange->setEndDate("today");
37 |
38 | // Create the Metrics object
39 | $sessions = new Google_Service_AnalyticsReporting_Metric();
40 | $sessions->setExpression("ga:sessions");
41 | $sessions->setAlias("sessions");
42 |
43 | // Create the ReportRequest object
44 | $request = new Google_Service_AnalyticsReporting_ReportRequest();
45 | $request->setViewId($viewId);
46 | $request->setDateRanges($dateRange);
47 | $request->setMetrics([$sessions]);
48 |
49 | $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
50 | $body->setReportRequests([$request]);
51 |
52 | $report = $analytics->reports->batchGet($body);
53 |
54 | // above code included into this helper method :
55 |
56 | $sessions = $analyticsService->getSessionsDateRange($viewId,'30daysAgo','today');
57 | $bounceRate = $analyticsService->getBounceRateDateRange($viewId,'30daysAgo','today');
58 | $avgTimeOnPage = $analyticsService->getAvgTimeOnPageDateRange($viewId,'30daysAgo','today');
59 | $pageViewsPerSession = $analyticsService->getPageviewsPerSessionDateRange($viewId,'30daysAgo','today');
60 | $percentNewVisits = $analyticsService->getPercentNewVisitsDateRange($viewId,'30daysAgo','today');
61 | $pageViews = $analyticsService->getPageViewsDateRange($viewId,'30daysAgo','today');
62 | $avgPageLoadTime = $analyticsService->getAvgPageLoadTimeDateRange($viewId,'30daysAgo','today');
63 |
64 | return [
65 | 'analytics' => $analytics,
66 | 'client' => $client,
67 | 'report' => $report,
68 | 'data' => [
69 | 'sessions' => $sessions,
70 | 'bounce_rate' => $bounceRate,
71 | 'avg_time_on_page' => $avgTimeOnPage,
72 | 'page_view_per_session' => $pageViewsPerSession,
73 | 'percent_new_visits' => $percentNewVisits,
74 | 'page_views' => $pageViews,
75 | 'avg_page_load_time' => $avgPageLoadTime
76 | ]
77 | ];
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Google Analytics API v4 Symfony bundle
2 | ======================================
3 |
4 | [](https://insight.sensiolabs.com/projects/da6423cf-b198-402a-8d23-e82e7833f9f6)
5 |
6 | [](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)
7 | [](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)
8 | [](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)
9 | [](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)
10 | [](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)
11 | [](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)
12 | [](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)
13 |
14 | # Use
15 |
16 | At MEDIA.figaro http://media.figaro.fr, the advertising department of the french newspaper Le Figaro and part of the Figaro Group (CCM Benchmark), we use this bundle to monitor our digital platforms with Google Analytics.
17 |
18 | It's a simple package that wraps the Google Analytics API version 4, and that gives you all the information to go straight to the point of getting some main metrics from GA.
19 |
20 | Development by Kendrick https://github.com/kendrick-k.
21 |
22 | To be able to use it, you have to setup a project on Google Console for Google Analytics, get the json key, then configure this package by setting the path for it. You'll have to add the developer email defined into the Google Console to the GA views to authorize it, otherwise the view won't be accessible through the API.
23 |
24 | You can use the debug routes to go live and test a profile (ex id : 111111111, here with [Docker](https://github.com/mediafigaro/docker-symfony)) :
25 |
26 | http://symfony.dev/app_dev.php/analytics-api/111111111
27 |
28 | 
29 |
30 | # Installation
31 |
32 | composer require mediafigaro/google-analytics-api-symfony
33 |
34 | without Flex, add to /app/AppKernel.php :
35 |
36 | $bundles = [
37 | ...
38 | new MediaFigaro\GoogleAnalyticsApi\GoogleAnalyticsApi(),
39 | ];
40 |
41 | # Versions
42 |
43 | ## 1.2
44 |
45 | Adding filterMetric and filterDimension to getDataDateRangeMetricsDimensions method which is a simple wrapper to Google Api Client ex :
46 |
47 | $analyticsService = $this->get('google_analytics_api.api');
48 |
49 | $data = $analyticsService->getDataDateRangeMetricsDimensions(
50 | 'myanalyticsviewid', // viewid
51 | '2018-01-01', // date start
52 | 'today', // date end
53 | ['sessions','users','percentNewSessions','bounceRate'], // metric
54 | ['source','campaign','fullReferrer','sourceMedium','pagePath'], // dimension
55 | [ // order metric and/or dimension
56 | 'fields' => ['sessions'],
57 | 'order' => 'descending'
58 | ],
59 | [ // metric
60 | 'metric_name' => 'sessions',
61 | 'operator' => 'LESS_THAN',
62 | 'comparison_value' => '100'
63 | ],
64 | [ // dimension
65 | 'dimension_name' => 'sourceMedium',
66 | 'operator' => 'EXACT',
67 | 'expressions' => [
68 | 'trading / native'
69 | ]
70 | ]
71 | );
72 |
73 | ## 1.1
74 |
75 | Symfony 4 simple adaptation with a public service and a new public method that takes in charge metrics and dimensions with sorting options :
76 |
77 | getDataDateRangeMetricsDimensions($viewId,$dateStart,$dateEnd,$metrics='sessions',$dimensions=null,$sorting=null)
78 |
79 | Query explorer https://ga-dev-tools.appspot.com/query-explorer/ to build your query.
80 |
81 | $viewId : https://developers.google.com/analytics/devguides/reporting/core/v3/reference#ids
82 |
83 | $dateStart : https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startDate
84 |
85 | $dateEnd: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#endDate
86 |
87 | $metrics: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#metrics (without the 'ga:', array or string)
88 |
89 | $dimensions: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#dimensions (without the 'ga:', array or string)
90 |
91 | $sorting: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#sort
92 |
93 | without the 'ga:', array or string, eg. :
94 |
95 | [
96 | 'fields' => ['pagePath','sessions'], // or : 'sessions'
97 | 'order' => 'descending'
98 | ]
99 |
100 | example :
101 |
102 | $analyticsService = $this->get('google_analytics_api.api');
103 |
104 | $data = $analyticsService->getDataDateRangeMetricsDimensions(
105 | 'myanalyticsviewid',
106 | '30daysAgo',
107 | 'today',
108 | ['sessions','users','percentNewSessions','bounceRate'],
109 | ['source','campaign','fullReferrer','sourceMedium','pagePath'],
110 | [
111 | 'fields' => ['pagePath','sessions'],
112 | 'order' => 'descending'
113 | ]
114 | )
115 |
116 | $data :
117 |
118 | array(329) {
119 | [0]=>
120 | array(2) {
121 | ["metrics"]=>
122 | array(4) {
123 | ["sessions"]=>
124 | string(5) "16738"
125 | ["users"]=>
126 | string(5) "15602"
127 | ["percentNewSessions"]=>
128 | string(17) "88.39168359421676"
129 | ["bounceRate"]=>
130 | string(17) "83.95268251881946"
131 | }
132 | ["dimensions"]=>
133 | array(5) {
134 | ["source"]=>
135 | string(7) "trading"
136 | ["campaign"]=>
137 | string(7) "my-campaign"
138 | ["fullReferrer"]=>
139 | string(7) "trading"
140 | ["sourceMedium"]=>
141 | string(16) "trading / native"
142 | ["pagePath"]=>
143 | string(50) "/my-url"
144 | }
145 | }
146 | [1]=>
147 | array(2) {
148 | ["metrics"]=>
149 | array(4) {
150 | ["sessions"]=>
151 | string(4) "6506"
152 | ["users"]=>
153 | string(4) "6200"
154 | ["percentNewSessions"]=>
155 | string(17) "87.05810021518599"
156 | ["bounceRate"]=>
157 | string(17) "87.74976944359054"
158 | }
159 | ["dimensions"]=>
160 | array(5) {
161 | ["source"]=>
162 | string(7) "trading"
163 | ["campaign"]=>
164 | string(7) "my-campaign-2"
165 | ["fullReferrer"]=>
166 | string(7) "trading"
167 | ["sourceMedium"]=>
168 | string(19) "trading / 320x480-1"
169 | ["pagePath"]=>
170 | string(50) "/my-url-2"
171 | }
172 | }
173 | [2]=>
174 | ...
175 |
176 | **Session - Dimensions & Metrics Explorer** : https://developers.google.com/analytics/devguides/reporting/core/dimsmets
177 |
178 | ## 1.0
179 |
180 | First version with a quick connector to consume Google Analytics v4 with Google API client with getDataDateRange private method (that handles only metrics), wrapped with some public methods to get a quick access to main metrics such as sessions (eg. getBounceRateDateRange($viewId,$dateStart,$dateEnd)), bounce rate, average time on page, page view per session, new visits, page views and average page load time.
181 | Debug route included for a simple setup and test.
182 |
183 | # Configuration
184 |
185 | google_analytics_api.google_analytics_json_key
186 |
187 | Set the relative path for your json key (set it on your server, better not into your repository) from execution path, ex: /data/analytics/analytics-27cef1a4c0fd.json.
188 |
189 | /app/config/parameters.yml
190 |
191 | google_analytics_json_key: "../data/analytics/analytics-27cef1a4c0fd.json"
192 |
193 | /app/config/config.yml
194 |
195 | google_analytics_api:
196 | google_analytics_json_key: "%google_analytics_json_key%"
197 |
198 | # Google API key
199 |
200 | Generate the json file from https://console.developers.google.com/start/api?id=analyticsreporting.googleapis.com&credential=client_key by creating a project, check the documentation : https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php.
201 |
202 | # Google Analytics API v4
203 |
204 | List of metrics for report building with search engine : https://developers.google.com/analytics/devguides/reporting/core/dimsmets eg. ga:sessions, ga:visits, ga:bounceRate ...
205 |
206 | Objects : https://github.com/google/google-api-php-client-services/tree/master/AnalyticsReporting
207 |
208 | (example : ReportData object : https://github.com/google/google-api-php-client-services/blob/master/AnalyticsReporting/ReportData.php)
209 |
210 | Samples : https://developers.google.com/analytics/devguides/reporting/core/v4/samples
211 |
212 | # Debug
213 |
214 | Add the debug routes for development purposes :
215 |
216 | /app/config/routing_dev.yml
217 |
218 | _google_analytics_api:
219 | resource: "@GoogleAnalyticsApi/Resources/config/routing_dev.yml"
220 |
221 | http://symfony.dev/app_dev.php/analytics-api/000000000
222 |
223 | 000000000 = profile id that you can find in the analytics URL, p000000000 :
224 |
225 | https://analytics.google.com/analytics/web/?hl=en&pli=1#management/Settings/a222222222w1111111111p000000000/%3Fm.page%3DPropertySettings/
226 |
227 | Result of this debug page :
228 |
229 | 
230 |
231 | # Errors
232 |
233 | In that 403 error case, follow the link and authorize the API v4.
234 |
235 | ...
236 | "message": "Google Analytics Reporting API has not been used in project xxxxxx-xxxxxx-000000
237 | before or it is disabled. Enable it by visiting
238 | https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview?project=xxxxxx-xxxxxx-000000
239 | then retry. If you enabled this API recently, wait a few minutes for the action to propagate
240 | to our systems and retry.",
241 | "domain": "global",
242 | "reason": "forbidden"
243 | }
244 | ],
245 | "status": "PERMISSION_DENIED"
246 |
247 | # Example
248 |
249 | Call the service :
250 |
251 | $analyticsService = $this->get('google_analytics_api.api');
252 | $analytics = $analyticsService->getAnalytics();
253 |
254 | Use the method helpers to get the main metrics within a date range :
255 |
256 | $viewId = '000000000'; // set your view id
257 |
258 | // get some metrics (last 30 days, date format is yyyy-mm-dd)
259 | $sessions = $analyticsService->getSessionsDateRange($viewId,'30daysAgo','today');
260 | $bounceRate = $analyticsService->getBounceRateDateRange($viewId,'30daysAgo','today');
261 | $avgTimeOnPage = $analyticsService->getAvgTimeOnPageDateRange($viewId,'30daysAgo','today');
262 | $pageViewsPerSession = $analyticsService->getPageviewsPerSessionDateRange($viewId,'30daysAgo','today');
263 | $percentNewVisits = $analyticsService->getPercentNewVisitsDateRange($viewId,'30daysAgo','today');
264 | $pageViews = $analyticsService->getPageViewsDateRange($viewId,'30daysAgo','today');
265 | $avgPageLoadTime = $analyticsService->getAvgPageLoadTimeDateRange($viewId,'30daysAgo','today');
266 |
267 | # Contribution
268 |
269 | You are welcome to contribute to this small Google Analytics v4 wrapper, to create more helpers or more.
270 |
271 | # More tools
272 |
273 | Try the Symfony Debug Toolbar Git : https://github.com/kendrick-k/symfony-debug-toolbar-git and the docker Service Oriented Architecture for Symfony : https://github.com/mediafigaro/docker-symfony.
274 |
275 | # Tutorial
276 |
277 | French [tutorial](https://www.supinfo.com/articles/single/2423-symfony-27-integration-google-analytics) by Jérémy PERCHE, SUPINFO student.
278 |
--------------------------------------------------------------------------------
/Service/GoogleAnalyticsService.php:
--------------------------------------------------------------------------------
1 | client = new Google_Client();
52 | $this->client->setApplicationName("GoogleAnalytics");
53 | $this->client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
54 | $this->client->setAuthConfig($keyFileLocation);
55 |
56 | $this->analytics = new Google_Service_AnalyticsReporting($this->client);
57 |
58 | }
59 |
60 | /**
61 | * @return Google_Service_AnalyticsReporting
62 | */
63 | public function getAnalytics() {
64 |
65 | return $this->analytics;
66 |
67 | }
68 |
69 | /**
70 | * @return Google_Client
71 | */
72 | public function getClient() {
73 |
74 | return $this->client;
75 |
76 | }
77 |
78 | /**
79 | * getDataDateRangeMetricsDimensions
80 | *
81 | * simple helper & wrapper of Google Api Client
82 | *
83 | * @param $viewId
84 | * @param $dateStart
85 | * @param $dateEnd
86 | * @param array $metrics
87 | * @param array $dimensions
88 | * @param array $sorting ( = [ ['fields']=>['sessions','bounceRate',..] , 'order'=>'descending' ] )
89 | * @param array $filterMetric ( = [ ['metric_name']=>['sessions'] , 'operator'=>'LESS_THAN' , 'comparison_value'=>'100' ] )
90 | * @param array $filterDimension ( = [ ['dimension_name']=>['sourceMedium'] , 'operator'=>'EXACT' , 'expressions'=>['my_campaign'] ] )
91 | * @return mixed
92 | *
93 | * @link https://developers.google.com/analytics/devguides/reporting/core/dimsmets
94 | * @link https://ga-dev-tools.appspot.com/query-explorer/
95 | * @link https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-php
96 | * @link https://developers.google.com/analytics/devguides/reporting/core/v4/samples
97 | * @link https://github.com/google/google-api-php-client
98 | *
99 | */
100 | public function getDataDateRangeMetricsDimensions($viewId,$dateStart,$dateEnd,$metrics='sessions',$dimensions=null,$sorting=null,$filterMetric=null,$filterDimension=null) {
101 |
102 | // Create the DateRange object
103 | $dateRange = new Google_Service_AnalyticsReporting_DateRange();
104 | $dateRange->setStartDate($dateStart);
105 | $dateRange->setEndDate($dateEnd);
106 |
107 | if (isset($metrics) && !is_array($metrics)) {
108 | $metrics = [$metrics];
109 | }
110 |
111 | if (isset($metrics) && is_array($metrics)) {
112 |
113 | $this->reportingDimensions = [];
114 |
115 | foreach ($metrics as $metric) {
116 |
117 | // Create the Metrics object
118 | $reportingMetrics = new Google_Service_AnalyticsReporting_Metric();
119 | $reportingMetrics->setExpression("ga:$metric");
120 | $reportingMetrics->setAlias("$metric");
121 |
122 | $this->reportingMetrics[] = $reportingMetrics;
123 |
124 | }
125 |
126 | }
127 |
128 | if (isset($dimensions) && !is_array($dimensions)) {
129 | $dimensions = [$dimensions];
130 | }
131 |
132 | if (isset($dimensions) && is_array($dimensions)) {
133 |
134 | $this->reportingDimensions = [];
135 |
136 | foreach ($dimensions as $dimension) {
137 |
138 | // Create the segment(s) dimension.
139 | $reportingDimensions = new Google_Service_AnalyticsReporting_Dimension();
140 | $reportingDimensions->setName("ga:$dimension");
141 |
142 | $this->reportingDimensions[] = $reportingDimensions;
143 |
144 | }
145 | }
146 |
147 | // Create the ReportRequest object
148 | $request = new Google_Service_AnalyticsReporting_ReportRequest();
149 | $request->setViewId($viewId);
150 | $request->setDateRanges($dateRange);
151 |
152 | // add dimensions
153 | if (isset($this->reportingDimensions) && is_array($this->reportingDimensions))
154 | $request->setDimensions($this->reportingDimensions);
155 |
156 | // add metrics
157 | if (isset($this->reportingMetrics) && is_array($this->reportingMetrics))
158 | $request->setMetrics($this->reportingMetrics);
159 |
160 | // sorting
161 | // @link https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#SortOrder
162 |
163 | if (isset($sorting) && is_array($sorting)) {
164 |
165 | $orderBy = new Google_Service_AnalyticsReporting_OrderBy();
166 |
167 | if (isset($sorting['fields']) && is_array($sorting['fields'])) {
168 |
169 | $fields = $sorting['fields'];
170 |
171 | foreach ($fields as $sortingFieldName) {
172 |
173 | $orderBy->setFieldName("ga:$sortingFieldName");
174 |
175 | }
176 |
177 | if (isset($sorting['order'])) {
178 |
179 | $order = $sorting['order'];
180 |
181 | $orderBy->setSortOrder($order);
182 |
183 | }
184 |
185 | }
186 |
187 | $request->setOrderBys($orderBy);
188 |
189 | }
190 |
191 | // metric filter (simple wrapper)
192 | // @link https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#metricfilter
193 |
194 | if (isset($filterMetric) && is_array($filterMetric)) {
195 |
196 | if (isset($filterMetric['metric_name']) && isset($filterMetric['operator']) && isset($filterMetric['comparison_value'])) {
197 |
198 | // Create the DimensionFilter.
199 | $metricFilter = new Google_Service_AnalyticsReporting_MetricFilter();
200 | $metricFilter->setMetricName('ga:'.$filterMetric['metric_name']);
201 | $metricFilter->setOperator($filterMetric['operator']);
202 | $metricFilter->setComparisonValue($filterMetric['comparison_value']);
203 |
204 | // Create the DimensionFilterClauses
205 | $metricFilterClause = new Google_Service_AnalyticsReporting_MetricFilterClause();
206 | $metricFilterClause->setFilters([$metricFilter]);
207 |
208 | // add to request
209 | $request->setMetricFilterClauses($metricFilterClause);
210 |
211 | }
212 |
213 | }
214 |
215 |
216 |
217 | // dimension filter (simple wrapper)
218 | // @link https://developers.google.com/analytics/devguides/reporting/core/v3/reference#filters
219 |
220 | if (isset($filterDimension) && is_array($filterDimension)) {
221 |
222 | if (isset($filterDimension['dimension_name']) && isset($filterDimension['operator']) && isset($filterDimension['expressions'])) {
223 |
224 | if (!is_array($filterDimension['expressions'])) {
225 | $filterDimension['expressions'] = [ $filterDimension['expressions'] ];
226 | }
227 |
228 | // Create the DimensionFilter.
229 | $dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
230 | $dimensionFilter->setDimensionName('ga:'.$filterDimension['dimension_name']);
231 | $dimensionFilter->setOperator($filterDimension['operator']);
232 | $dimensionFilter->setExpressions($filterDimension['expressions']);
233 |
234 | // Create the DimensionFilterClauses
235 | $dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
236 | $dimensionFilterClause->setFilters(array($dimensionFilter));
237 |
238 | // add to request
239 | $request->setDimensionFilterClauses(array($dimensionFilterClause));
240 |
241 | }
242 |
243 | }
244 |
245 | $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
246 | $body->setReportRequests([$request]);
247 |
248 | $reports = $this->analytics->reports->batchGet($body);
249 |
250 | $data = [];
251 |
252 | foreach ($reports->getReports()[0]->getData()->getRows() as $row) {
253 |
254 | // arrays
255 | $dimensionsArray = $row->getDimensions();
256 | $valuesArray = $row->getMetrics()[0]->getValues();
257 |
258 | $dimensionsKeyValue = [];
259 |
260 | if (isset($dimensionsArray)) {
261 |
262 | $i=0;
263 |
264 | foreach ($dimensionsArray as $k => $v) {
265 | $dimensionsKeyValue[$dimensions[$i]] = $v;
266 | $i++;
267 | }
268 |
269 | }
270 |
271 | $metricsKeyValue = [];
272 |
273 | if (isset($metrics)) {
274 |
275 | $i = 0;
276 |
277 | foreach ($metrics as $k => $v) {
278 | $metricsKeyValue[$metrics[$i]] = $valuesArray[$i];
279 | $i++;
280 | }
281 |
282 | }
283 |
284 | $data[] = [
285 | 'metrics' => $metricsKeyValue,
286 | 'dimensions' => $dimensionsKeyValue
287 | ];
288 |
289 | }
290 |
291 | return $data;
292 |
293 | }
294 |
295 | /**
296 | * @param $viewId
297 | * @param $dateStart
298 | * @param $dateEnd
299 | * @return mixed
300 | *
301 | * https://ga-dev-tools.appspot.com/query-explorer/
302 | *
303 | */
304 | private function getDataDateRange($viewId,$dateStart,$dateEnd,$metric) {
305 |
306 | // Create the DateRange object
307 | $dateRange = new Google_Service_AnalyticsReporting_DateRange();
308 | $dateRange->setStartDate($dateStart);
309 | $dateRange->setEndDate($dateEnd);
310 |
311 | // Create the Metrics object
312 | $sessions = new Google_Service_AnalyticsReporting_Metric();
313 | $sessions->setExpression("ga:$metric");
314 | $sessions->setAlias("$metric");
315 |
316 | if (isset($dimensions) && is_array($dimensions)) {
317 |
318 | $this->reportingDimensions = [];
319 |
320 | foreach ($dimensions as $dimension) {
321 |
322 | // Create the segment dimension.
323 | $reportingDimensions = new Google_Service_AnalyticsReporting_Dimension();
324 | $reportingDimensions->setName("ga:$dimension");
325 |
326 | $this->reportingDimensions[] = $reportingDimensions;
327 |
328 | }
329 | }
330 |
331 | // Create the ReportRequest object
332 | $request = new Google_Service_AnalyticsReporting_ReportRequest();
333 | $request->setViewId($viewId);
334 | $request->setDateRanges($dateRange);
335 |
336 | // add dimensions
337 | if (isset($this->reportingDimensions) && is_array($this->reportingDimensions))
338 | $request->setDimensions($this->reportingDimensions);
339 |
340 | $request->setMetrics([$sessions]);
341 |
342 | $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
343 | $body->setReportRequests([$request]);
344 |
345 | $report = $this->analytics->reports->batchGet($body);
346 |
347 | $result = $report->getReports()[0]
348 | ->getData()
349 | ->getTotals()[0]
350 | ->getValues()[0]
351 | ;
352 |
353 | return $result;
354 |
355 | }
356 |
357 | /**
358 | * @param $viewId
359 | * @param $dateStart
360 | * @param $dateEnd
361 | * @return mixed
362 | */
363 | public function getSessionsDateRange($viewId,$dateStart,$dateEnd) {
364 | return $this->getDataDateRange($viewId,$dateStart,$dateEnd,'sessions');
365 | }
366 |
367 | /**
368 | * @param $viewId
369 | * @param $dateStart
370 | * @param $dateEnd
371 | * @return mixed
372 | */
373 | public function getBounceRateDateRange($viewId,$dateStart,$dateEnd) {
374 | return $this->getDataDateRange($viewId,$dateStart,$dateEnd,'bounceRate');
375 | }
376 |
377 | /**
378 | * @param $viewId
379 | * @param $dateStart
380 | * @param $dateEnd
381 | * @return mixed
382 | */
383 | public function getAvgTimeOnPageDateRange($viewId,$dateStart,$dateEnd) {
384 | return $this->getDataDateRange($viewId,$dateStart,$dateEnd,'avgTimeOnPage');
385 | }
386 |
387 | /**
388 | * @param $viewId
389 | * @param $dateStart
390 | * @param $dateEnd
391 | * @return mixed
392 | */
393 | public function getPageviewsPerSessionDateRange($viewId,$dateStart,$dateEnd) {
394 | return $this->getDataDateRange($viewId,$dateStart,$dateEnd,'pageviewsPerSession');
395 | }
396 |
397 | /**
398 | * @param $viewId
399 | * @param $dateStart
400 | * @param $dateEnd
401 | * @return mixed
402 | */
403 | public function getPercentNewVisitsDateRange($viewId,$dateStart,$dateEnd) {
404 | return $this->getDataDateRange($viewId,$dateStart,$dateEnd,'percentNewVisits');
405 | }
406 |
407 | /**
408 | * @param $viewId
409 | * @param $dateStart
410 | * @param $dateEnd
411 | * @return mixed
412 | */
413 | public function getPageViewsDateRange($viewId,$dateStart,$dateEnd) {
414 | return $this->getDataDateRange($viewId,$dateStart,$dateEnd,'pageviews');
415 | }
416 |
417 | /**
418 | * @param $viewId
419 | * @param $dateStart
420 | * @param $dateEnd
421 | * @return mixed
422 | */
423 | public function getAvgPageLoadTimeDateRange($viewId,$dateStart,$dateEnd) {
424 | return $this->getDataDateRange($viewId,$dateStart,$dateEnd,'avgPageLoadTime');
425 | }
426 |
427 | }
428 |
--------------------------------------------------------------------------------