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