OLD | NEW |
(Empty) | |
| 1 <link rel="import" href="../polymer/polymer.html"> |
| 2 <link rel="import" href="../google-chart/google-chart.html"> |
| 3 <link rel="import" href="../promise-polyfill/promise-polyfill-lite.html"> |
| 4 <link rel="import" href="google-analytics-query.html"> |
| 5 <link rel="import" href="google-analytics-loader.html"> |
| 6 |
| 7 <!-- |
| 8 Element for displaying the results of a Google Analytics query in a |
| 9 Google Chart. |
| 10 |
| 11 ##### Example |
| 12 |
| 13 <google-analytics-chart |
| 14 type="column" |
| 15 ids="ga:1174" |
| 16 metrics="ga:sessions" |
| 17 dimensions="ga:country" |
| 18 sort="-ga:sessions" |
| 19 maxResults="5"> |
| 20 </google-analytics-chart> |
| 21 |
| 22 @element google-analytics-chart |
| 23 @demo |
| 24 --> |
| 25 |
| 26 <dom-module is="google-analytics-chart"> |
| 27 <template> |
| 28 <google-analytics-loader all-ready="{{setupReady}}"></google-analytics-loade
r> |
| 29 <google-analytics-query id="query" |
| 30 loading="{{loading}}" |
| 31 get-data-response-handler="{{_boundResponseHandler}}" |
| 32 data="{{data}}" |
| 33 ids="{{ids}}" |
| 34 start-date="{{startDate}}" |
| 35 end-date="{{endDate}}" |
| 36 metrics="{{metrics}}" |
| 37 dimensions="{{dimensions}}" |
| 38 sort="{{sort}}" |
| 39 filters="{{filters}}" |
| 40 segment="{{segment}}" |
| 41 sampling-level="{{samplingLevel}}" |
| 42 start-index="{{startIndex}}" |
| 43 max-results="{{maxResults}}" |
| 44 output="dataTable" |
| 45 src-param="gwc-ga-chart" |
| 46 ></google-analytics-query> |
| 47 <content select="header,h1,h2,h3,h4,h5,h6"></content> |
| 48 <google-chart id="chart" |
| 49 type="{{type}}" |
| 50 width="{{width}}" |
| 51 height="{{height}}" |
| 52 data="{{data.dataTable}}"> |
| 53 </google-chart> |
| 54 <content select="footer"></content> |
| 55 </template> |
| 56 </dom-module> |
| 57 |
| 58 <script> |
| 59 |
| 60 (function() { |
| 61 |
| 62 'use strict'; |
| 63 |
| 64 Polymer({ |
| 65 |
| 66 is: 'google-analytics-chart', |
| 67 |
| 68 properties: { |
| 69 |
| 70 /** |
| 71 * Sets the type of the chart. |
| 72 * |
| 73 * Should be one of: |
| 74 * - `area`, `bar`, `column`, `line`, `pie`, `geo`. |
| 75 * |
| 76 * @attribute type |
| 77 * @default 'area' |
| 78 * @type string |
| 79 */ |
| 80 type: { |
| 81 type: String, |
| 82 value: 'area' |
| 83 }, |
| 84 |
| 85 /** |
| 86 * Sets the width of the chart on the page. |
| 87 * |
| 88 * @attribute width |
| 89 * @default 480 |
| 90 * @type number of string |
| 91 */ |
| 92 width: { |
| 93 type: Number, |
| 94 value: 480 |
| 95 }, |
| 96 |
| 97 /** |
| 98 * Sets the height of the chart on the page. |
| 99 * |
| 100 * @attribute height |
| 101 * @default 320 |
| 102 * @type number or string |
| 103 */ |
| 104 height: { |
| 105 type: Number, |
| 106 value: 320, |
| 107 }, |
| 108 |
| 109 /** |
| 110 * Sets the options for the chart. |
| 111 * |
| 112 * Example: |
| 113 * <pre>{ |
| 114 * title: "Chart title goes here", |
| 115 * hAxis: {title: "Categories"}, |
| 116 * vAxis: {title: "Values", minValue: 0, maxValue: 2}, |
| 117 * legend: "none" |
| 118 * };</pre> |
| 119 * See <a href="https://google-developers.appspot.com/chart/interactive/
docs/gallery">Google Visualization API reference (Chart Gallery)</a> |
| 120 * for the options available to each chart type. |
| 121 * |
| 122 * @attribute options |
| 123 * @default null |
| 124 * @type object |
| 125 */ |
| 126 options: { |
| 127 type: Object, |
| 128 value: function() { return null; } |
| 129 }, |
| 130 |
| 131 /** |
| 132 * True after the chart has been rendered for the first time. |
| 133 * |
| 134 * @attribute rendered |
| 135 * @type boolean |
| 136 */ |
| 137 rendered: { |
| 138 type: Boolean, |
| 139 value: false, |
| 140 notify: true, |
| 141 reflectToAttribute: true |
| 142 }, |
| 143 |
| 144 /** |
| 145 * True if the chart is currently loading data. |
| 146 * |
| 147 * @attribute loading |
| 148 * @type boolean |
| 149 */ |
| 150 loading: { |
| 151 type: Boolean, |
| 152 value: false, |
| 153 notify: true, |
| 154 reflectToAttribute: true |
| 155 }, |
| 156 |
| 157 /** |
| 158 * True if setup is ready |
| 159 * |
| 160 * @attribute setupReady |
| 161 * @type Boolean |
| 162 */ |
| 163 setupReady: { |
| 164 type: Boolean, |
| 165 observer: 'setupReadyChanged' |
| 166 }, |
| 167 |
| 168 /** |
| 169 * google-analytics-query passthrough properties |
| 170 * See google-analytics-query for documentation |
| 171 * startDate, endDate, data, ids, metrics, dimensions, sort, filters, se
gment, samplingLevel, startIndex, maxResults |
| 172 */ |
| 173 startDate: { |
| 174 type: String, |
| 175 }, |
| 176 |
| 177 endDate: { |
| 178 type: String |
| 179 }, |
| 180 |
| 181 data: { |
| 182 type: Object |
| 183 }, |
| 184 |
| 185 ids: { |
| 186 type: String |
| 187 }, |
| 188 |
| 189 metrics: { |
| 190 type: String |
| 191 }, |
| 192 |
| 193 dimensions: { |
| 194 type: String |
| 195 }, |
| 196 |
| 197 sort: { |
| 198 type: String |
| 199 }, |
| 200 |
| 201 filters: { |
| 202 type: String |
| 203 }, |
| 204 |
| 205 segment: { |
| 206 type: String |
| 207 }, |
| 208 |
| 209 samplingLevel: { |
| 210 type: String |
| 211 }, |
| 212 |
| 213 startIndex: { |
| 214 type: Number |
| 215 }, |
| 216 |
| 217 maxResults: { |
| 218 type: Number |
| 219 } |
| 220 |
| 221 }, |
| 222 |
| 223 ready: function() { |
| 224 this._boundResponseHandler = this.handleResponse.bind(this); |
| 225 merge(this.$.chart.options, getChartOptions(this.type), this.options); |
| 226 }, |
| 227 |
| 228 setupReadyChanged: function(newVal, oldVal) { |
| 229 if (newVal) { |
| 230 metadata.get(); |
| 231 } |
| 232 }, |
| 233 |
| 234 handleResponse: function(response) { |
| 235 this.rendered = true; |
| 236 |
| 237 metadata.get().then(function(map) { |
| 238 switchApiNamesToDisplayNames(response.dataTable, map); |
| 239 ensureProperDataTableTypes(response.dataTable); |
| 240 this.$.query.setData(response); |
| 241 }.bind(this)); |
| 242 } |
| 243 }); |
| 244 |
| 245 /** |
| 246 * @module metadata |
| 247 */ |
| 248 var metadata = (function() { |
| 249 var promise; |
| 250 function queryMetadataAPI() { |
| 251 if (!gapi || !gapi.client || !gapi.client.analytics) { |
| 252 console.warn("Library not loaded yet!"); |
| 253 return; |
| 254 } |
| 255 return new Promise(function(resolve, reject) { |
| 256 gapi.client.analytics |
| 257 gapi.client.analytics.metadata.columns |
| 258 .list({ |
| 259 reportType: 'ga', |
| 260 _src: 'gwc-ga-chart' |
| 261 }) |
| 262 .execute(function(response) { |
| 263 if (response.error) { |
| 264 reject(response.error); |
| 265 } |
| 266 else { |
| 267 var map = {}; |
| 268 response.items.forEach(function(item) { |
| 269 map[item.id] = item.attributes.uiName; |
| 270 }); |
| 271 resolve(map); |
| 272 } |
| 273 }); |
| 274 }); |
| 275 } |
| 276 return { |
| 277 /** |
| 278 * Return the `queryMetadataAPI` promise. If the promise exists, |
| 279 * return it to avoid multiple requests. If the promise does not |
| 280 * exist, initiate the request and cache the promise. |
| 281 */ |
| 282 get: function() { |
| 283 promise = promise || queryMetadataAPI(); |
| 284 return promise.catch(function(err) { |
| 285 console.error(err.stack || err); |
| 286 }); |
| 287 } |
| 288 }; |
| 289 }()); |
| 290 |
| 291 /** |
| 292 * Return an options object for the specified chart type. |
| 293 * These are options suitable to pass to a Google Chart instance. |
| 294 * @return {Object} The chart options. |
| 295 */ |
| 296 function getChartOptions(type) { |
| 297 var chartOptions = { |
| 298 base: { |
| 299 fontSize: 11, |
| 300 chartArea: { |
| 301 width: '100%' |
| 302 }, |
| 303 legend: { |
| 304 position: 'top', |
| 305 alignment: 'start' |
| 306 } |
| 307 }, |
| 308 area: { |
| 309 pointSize: 6, |
| 310 lineWidth: 4, |
| 311 areaOpacity: 0.1, |
| 312 colors: ['#058dc7', '#aadff3'], |
| 313 hAxis: { |
| 314 format: 'MMM d', |
| 315 gridlines: { |
| 316 color: 'transparent' |
| 317 }, |
| 318 baselineColor: 'transparent' |
| 319 }, |
| 320 vAxis: { |
| 321 gridlines: { |
| 322 color: '#e8e8e8', |
| 323 logScale: true, |
| 324 count: 3 |
| 325 }, |
| 326 textPosition: 'in' |
| 327 } |
| 328 }, |
| 329 bar: { |
| 330 colors: ['#058dc7', '#50b432', '#ed561b'], |
| 331 hAxis: { |
| 332 gridlines: { |
| 333 color: '#e8e8e8' |
| 334 } |
| 335 }, |
| 336 vAxis: { |
| 337 textPosition: 'in', |
| 338 textStyle: { |
| 339 strokeWidth: 4, |
| 340 } |
| 341 } |
| 342 }, |
| 343 column: { |
| 344 colors: ['#058dc7', '#50b432', '#ed561b'], |
| 345 hAxis: { |
| 346 gridlines: { |
| 347 color: 'transparent' |
| 348 }, |
| 349 baselineColor: 'transparent' |
| 350 }, |
| 351 vAxis: { |
| 352 gridlines: { |
| 353 color: '#e8e8e8', |
| 354 }, |
| 355 textPosition: 'in' |
| 356 }, |
| 357 |
| 358 }, |
| 359 geo: { |
| 360 colorAxis: { |
| 361 minValue: 0, |
| 362 colors: ['#aadff3', '#058dc7'] |
| 363 } |
| 364 } |
| 365 }; |
| 366 return merge({}, chartOptions.base, chartOptions[type]); |
| 367 } |
| 368 |
| 369 /** |
| 370 * Use data from the Metadata API to change api names |
| 371 * (e.g. `ga:sessions`) to their display names (e.g. "Sessions"). |
| 372 * @param {Object} dataTable - The dataTable data. |
| 373 * @param {Object} map - A key/value store where the keys are the |
| 374 * api names and the values are the display names. |
| 375 */ |
| 376 function switchApiNamesToDisplayNames(dataTable, map) { |
| 377 dataTable.cols.forEach(function(col) { |
| 378 col.label = map[col.id] || col.label; |
| 379 }); |
| 380 } |
| 381 |
| 382 /** |
| 383 * The analytics api erroneously return some values as strings that are |
| 384 * supposed to be numbers. This function fixes that. |
| 385 * @param {Object} dataTable - The dataTable data. |
| 386 */ |
| 387 function ensureProperDataTableTypes(dataTable) { |
| 388 for (var i = 0; i < dataTable.rows.length; i++) { |
| 389 var row = dataTable.rows[i]; |
| 390 for (var j = 0; j < row.c.length; j++) { |
| 391 if (dataTable.cols[j].type === 'number') { |
| 392 row.c[j].v = Number(row.c[j].v); |
| 393 } |
| 394 } |
| 395 } |
| 396 } |
| 397 |
| 398 /** |
| 399 * Merge the source objects, in order, onto the destination object. |
| 400 * Recursively merge nested, plain objects, everything else copy by |
| 401 * reference. |
| 402 * @param {Object} target - The object to receive the merged values. |
| 403 * @param {...Object} source - The object(s) to provide values to the |
| 404 * target. Later sources override previous sources. |
| 405 * @return {Object} The merged target object. |
| 406 */ |
| 407 function merge(target) { |
| 408 var sources = Array.prototype.slice.call(arguments, 1); |
| 409 sources.forEach(function(source) { |
| 410 // Only merge objects. |
| 411 if (!(source && typeof sources == 'object')) return; |
| 412 |
| 413 Object.keys(source).forEach(function(key) { |
| 414 // If the source's key is a 'plain' object, recursively merge. |
| 415 if (typeof source[key] == 'object' && |
| 416 Object.getPrototypeOf(source[key]) == Object.prototype) { |
| 417 target[key] = target[key] == null ? |
| 418 merge({}, source[key]) : merge(target[key], source[key]); |
| 419 } |
| 420 // Otherwise just copy by reference. |
| 421 else if (typeof source[key] != 'undefined') { |
| 422 target[key] = source[key]; |
| 423 } |
| 424 }); |
| 425 }); |
| 426 return target; |
| 427 } |
| 428 }()); |
| 429 |
| 430 </script> |
| 431 |
OLD | NEW |