-
Notifications
You must be signed in to change notification settings - Fork 79
/
google-analytics-query.html
388 lines (341 loc) · 11.5 KB
/
google-analytics-query.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="google-analytics-loader.html">
<!--
Element for querying the Google Analytics Core Reporting API.
##### Example
<google-analytics-query
ids="ga:1174"
metrics="ga:sessions"
dimensions="ga:country"
sort="-ga:sessions"
max-results="5">
</google-analytics-query>
@element google-analytics-query
@demo
@homepage https://googlewebcomponents.github.io/google-analytics
-->
<dom-module id="google-analytics-query">
<template>
<google-analytics-loader all-ready="{{setupReady}}"></google-analytics-loader>
</template>
</dom-module>
<script>
(function() {
'use strict';
Polymer({
is: 'google-analytics-query',
/**
* Fired when a query is successfully run and data has been stored.
*
* @event analytics-query-success
*/
/**
* Fired when an error occurs while running a query.
*
* @event analytics-query-error
*/
properties: {
/**
* The `data` attribute is the response from a query to the Google
* Analytics Core Reporting API. This value will be updated as
* subsequent requests are made.
*
* @attribute data
* @type object
*/
data: {
type: Object,
value: function() { return {}},
notify: true
},
/**
* The `ids` attribute is the unique table ID of the form ga:XXXX,
* where XXXX is the Analytics view (profile) ID for which the query
* will retrieve the data.
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#ids">Core Reporting API parameter reference</a> for more details.
*
* Note: you can find out the `ids` value for any of your Google Analytics account using the <a href="https://ga-dev-tools.appspot.com/explorer/">Google Analytics query explorer</a>.
*
* @attribute ids
* @type string
*/
ids: {
type: String,
value: ''
},
/**
* The `startDate` attribute is the start date for fetching Analytics
* data. Requests can specify a start date formatted as YYYY-MM-DD, or
* as a relative date (e.g., today, yesterday, or NdaysAgo where N is a
* positive integer).
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startDate">Core Reporting API parameter reference</a> for more details.
*
* @attribute startDate
* @default '7daysAgo'
* @type string
*/
startDate: {
type: String,
value: '7daysAgo'
},
/**
* The `endDate` attribute is the end date for fetching Analytics
* data. Requests can specify an end date formatted as YYYY-MM-DD, or
* as a relative date (e.g., today, yesterday, or NdaysAgo where N is a
* positive integer).
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#endDate">Core Reporting API parameter reference</a> for more details.
*
* @attribute endDate
* @default 'yesterday'
* @type string
*/
endDate: {
type: String,
value: 'yesterday'
},
/**
* The `metrics` attribute is a list of comma-separated metrics,
* such as ga:sessions,ga:bounces.
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#metrics">Core Reporting API parameter reference</a> for more details.
*
* @attribute metrics
* @type string
*/
metrics: {
type: String,
value: ''
},
/**
* The `dimensions` attribute is a list of comma-separated dimensions
* for your Analytics data, such as ga:browser,ga:city.
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#dimensions">Core Reporting API parameter reference</a> for more details.
*
* @attribute dimensions
* @type string
*/
dimensions: {
type: String,
value: ''
},
/**
* The `sort` attribute is a list of comma-separated dimensions
* and metrics indicating the sorting order and sorting direction for
* the returned data.
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#sort">Core Reporting API parameter reference</a> for more details.
*
* @attribute sort
* @type string
*/
sort: {
type: String,
value: ''
},
/**
* The `filters` attribute is dimension or metric filters that restrict
* the data returned for your request.
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#filters">Core Reporting API parameter reference</a> for more details.
*
* @attribute filters
* @type string
*/
filters: {
type: String,
value: ''
},
/**
* The `segment` attribute segments the data returned for your
* request.
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#segment">Core Reporting API parameter reference</a> for more details.
*
* @attribute segment
* @type string
*/
segment: {
type: String,
value: ''
},
/**
* The `samplingLevel` attribute sets the desired sampling level.
* Allowed Values: `DEFAULT`, `FASTER`, `HIGHER_PRECISION`.
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#samplingLevel">Core Reporting API parameter reference</a> for more details.
*
* @attribute samplingLevel
* @type string
*/
samplingLevel: {
type: String,
value: ''
},
/**
* The `startIndex` attribute sets the first row of data to retrieve,
* starting at 1. Use this parameter as a pagination mechanism along
* with the max-results parameter.
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startIndex">Core Reporting API parameter reference</a> for more details.
*
* @attribute startIndex
* @type integer
*/
startIndex: {
type: Number,
value: 0
},
/**
* The `maxResults` attribute is the maximum number of rows to include
* in the response.
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#maxResults">Core Reporting API parameter reference</a> for more details.
*
* @attribute maxResults
* @type integer
*/
maxResults: {
type: Number,
value: 0
},
/**
* The `output` attribute sets the desired output type for the
* Analytics data returned in the response. Acceptable values are json
* and dataTable.
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#output">Core Reporting API parameter reference</a> for more details.
*
* @attribute output
* @type string
*/
output: {
type: String,
value: ''
},
/**
* The `fields` attribute is a selector specifying a subset of
* fields to include in the response.
*
* See the <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#fields">Core Reporting API parameter reference</a> for more details.
*
* @attribute fields
* @type string
*/
fields: {
type: String,
value: ''
},
/**
* GA team internal variable for analytics purposes.
*/
_srcParam: {
type: String,
value: 'gwc-ga-query'
},
/**
* true if data is getting loaded
* @attribute loading
* @type Boolean
*/
loading: {
type: Boolean,
value: false,
notify: true
},
/**
*
*
* @attribute getDataResponseHandler
* @type Function
*/
getDataResponseHandler: {
type: Function
},
/**
* True if setup is ready
*
* @attribute setupReady
* @type Boolean
*/
setupReady: {
type: Boolean,
observer: '_setupReadyChanged'
}
},
observers: [
'getData(ids,startDate,endDate,metrics,dimensions,sort,filters,segment,samplingLevel,startIndex,maxResults,output,fields)'
],
_setupReadyChanged: function(newVal) {
if (newVal)
this.getData();
else
this.data = null;
},
/**
* Query the Google Analytics Core Reporting API.
*
* @method getData
*/
getData: function() {
if (this.setupReady && this._hasRequiredParams) {
this.loading = true;
// Required parameters.
var query = {
'ids': this.ids,
'start-date': this.startDate,
'end-date': this.endDate,
'metrics': this.metrics,
'_src': this._srcParam
};
// Optional parameters.
if (this.dimensions) query.dimensions = this.dimensions;
if (this.sort) query.sort = this.sort;
if (this.filters) query.filters = this.filters;
if (this.segment) query.segment = this.segment;
if (this.samplingLevel) query.samplingLevel = this.samplingLevel;
if (this.startIndex) query['start-index'] = this.startIndex;
if (this.maxResults) query['max-results'] = this.maxResults;
if (this.output) query.output = this.output;
if (this.fields) query.fields = this.fields;
gapi.client.analytics.data.ga.get(query)
.execute(this.handleResponse.bind(this));
return true;
}
},
/**
* setData sets data fetched by getData.
* Use it if you override getData response processing
* @method setData
*/
setData: function(data) {
this.data = data;
this.fire('analytics-query-success', data);
},
/**
* The callback for the query run in `getData`. This is a separate
* function so subclasses can alter how the response is handled.
*
* @method handleResponse
*/
handleResponse: function(response) {
this.loading = false;
if (response.error) {
this.fire('analytics-query-error', response.error);
}
else {
if (this.getDataResponseHandler)
this.getDataResponseHandler(response)
else
this.setData(response);
}
},
get _hasRequiredParams() {
return !!(this.ids && this.metrics && this.startDate && this.endDate);
},
});
}());
</script>