| OLD | NEW |
| (Empty) | |
| 1 <link rel="import" href="../polymer/polymer.html"> |
| 2 <link rel="import" href="account-summaries-import.html"> |
| 3 <link rel="import" href="google-analytics-loader.html"> |
| 4 |
| 5 <!-- |
| 6 Element for selecting the view ID (ids) value for queries inside a |
| 7 `<google-analytics-dashboard>` element. |
| 8 |
| 9 ##### Example |
| 10 |
| 11 <google-analytics-dashboard> |
| 12 |
| 13 <google-analytics-view-selector></google-analytics-view-selector> |
| 14 |
| 15 <google-analytics-chart |
| 16 metrics="ga:sessions" |
| 17 dimensions="ga:date"> |
| 18 </google-analytics-chart> |
| 19 |
| 20 </google-analytics-dashboard> |
| 21 |
| 22 @element google-analytics-view-selector |
| 23 @extends google-analytics-base |
| 24 @blurb Element for selecting the ids value for Google Analytics queries |
| 25 @status alpha |
| 26 @homepage https://googlewebcomponents.github.io/google-analytics |
| 27 --> |
| 28 |
| 29 <dom-module id="google-analytics-view-selector"> |
| 30 <style> |
| 31 select { |
| 32 color: inherit; |
| 33 font: inherit; |
| 34 margin: 0; |
| 35 } |
| 36 </style> |
| 37 <template> |
| 38 <google-analytics-loader all-ready="{{setupReady}}"></google-analytics-loade
r> |
| 39 <span class="control"> |
| 40 <label for="account">Account</label> |
| 41 <select id="account" |
| 42 on-change="updateAccount" |
| 43 value="{{account.id}}"> |
| 44 <template is="dom-repeat" items="{{accounts}}"> |
| 45 <option value="{{item.id}}">{{item.name}}</option> |
| 46 </template> |
| 47 </select> |
| 48 </span> |
| 49 <span class="control"> |
| 50 <label>Property</label> |
| 51 <select id="property" |
| 52 on-change="updateProperty" |
| 53 value="{{property.id}}"> |
| 54 <template is="dom-repeat" items="{{account.properties}}"> |
| 55 <option value="{{item.id}}">{{item.name}}</option> |
| 56 </template> |
| 57 </select> |
| 58 </span> |
| 59 <span class="control"> |
| 60 <label>View</label> |
| 61 <select id="view" |
| 62 on-change="updateView" |
| 63 value="{{view.id}}"> |
| 64 <template is="dom-repeat" items="{{property.views}}"> |
| 65 <option value="{{item.id}}">{{item.name}}</option> |
| 66 </template> |
| 67 </select> |
| 68 </span> |
| 69 </template> |
| 70 </dom-module> |
| 71 <script> |
| 72 |
| 73 (function() { |
| 74 'use strict'; |
| 75 |
| 76 Polymer({ |
| 77 |
| 78 is: 'google-analytics-view-selector', |
| 79 |
| 80 /** |
| 81 * Fired when the users changes the view |
| 82 * |
| 83 * @param {Object} query The updated query params. |
| 84 * @event analytics-dashboard-control-change |
| 85 */ |
| 86 |
| 87 properties: { |
| 88 |
| 89 /** |
| 90 * The `ids` attribute, when found is used to preselect the chosen |
| 91 * account, property, and view. |
| 92 * |
| 93 * 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. |
| 94 * |
| 95 * @property ids |
| 96 * @type string |
| 97 */ |
| 98 ids: { |
| 99 type: String, |
| 100 observer: 'idsChanged', |
| 101 notify: true |
| 102 }, |
| 103 |
| 104 /** |
| 105 * The `summaries` attribute contains an account summaries utility objec
t |
| 106 * with various helper methods for quickly getting account data. |
| 107 * |
| 108 * See the <a href="https://github.com/googleanalytics/javascript-api-ut
ils">Github repo</a> for more details. |
| 109 * |
| 110 * @property summaries |
| 111 * @type Object |
| 112 */ |
| 113 summaries: { |
| 114 type: Object, |
| 115 value: null |
| 116 }, |
| 117 |
| 118 /** |
| 119 * The `account` attribute is the currently selected account. |
| 120 * |
| 121 * @property account |
| 122 * @type Object |
| 123 */ |
| 124 account: { |
| 125 type: Object, |
| 126 observer: 'accountChanged' |
| 127 }, |
| 128 |
| 129 /** |
| 130 * The `property` attribute is the currently selected property. |
| 131 * |
| 132 * @property property |
| 133 * @type Object |
| 134 */ |
| 135 property: { |
| 136 type: Object, |
| 137 observer: 'propertyChanged' |
| 138 }, |
| 139 |
| 140 /** |
| 141 * The `view` attribute is the currently selected view. |
| 142 * |
| 143 * @property view |
| 144 * @type Object |
| 145 */ |
| 146 view: { |
| 147 type: Object, |
| 148 observer: 'viewChanged' |
| 149 }, |
| 150 |
| 151 /** |
| 152 * True if setup is ready |
| 153 * |
| 154 * @attribute setupReady |
| 155 * @type Boolean |
| 156 */ |
| 157 setupReady: { |
| 158 type: Boolean, |
| 159 observer: 'setupReadyChanged' |
| 160 } |
| 161 |
| 162 }, |
| 163 |
| 164 setupReadyChanged: function(newVal, oldVal) { |
| 165 if (newVal) { |
| 166 gaApiUtils.accountSummaries.get().then(function(accountSummaries) { |
| 167 this.summaries = accountSummaries; |
| 168 this.accounts = accountSummaries.all(); |
| 169 |
| 170 if (this.ids) { |
| 171 // Manually call `idsChanged` here. The first change event will |
| 172 // likely happen prior to fetching the accountSummaries data. |
| 173 this.idsChanged(null, this.ids); |
| 174 } else { |
| 175 // When there's no `ids` set, just select the first account, |
| 176 // which will trigger change events and set the property and view. |
| 177 this.account = this.accounts[0]; |
| 178 } |
| 179 }.bind(this)); |
| 180 } |
| 181 else { |
| 182 this.summaries = null; |
| 183 this.accounts = null; |
| 184 this.account = null; |
| 185 this.property = null; |
| 186 this.view = null; |
| 187 this.ids = null; |
| 188 } |
| 189 }, |
| 190 |
| 191 /** |
| 192 * The `updateAccount` method is bound to the change event on the |
| 193 * account `<select>`. It updates the property and view `<select>`s based |
| 194 * on the new account data. It also updates the `ids` attribute. |
| 195 */ |
| 196 updateAccount: function() { |
| 197 this.account = this.summaries.getAccount(this.$.account.value); |
| 198 }, |
| 199 |
| 200 /** |
| 201 * The `updateProperty` method is bound to the change event on the |
| 202 * property `<select>`. It updates the view `<select>` based |
| 203 * on the new property data. It also updates the `ids` attribute. |
| 204 */ |
| 205 updateProperty: function() { |
| 206 this.property = this.summaries.getProperty(this.$.property.value); |
| 207 }, |
| 208 |
| 209 /** |
| 210 * The `updateView` method is bound to the change event on the |
| 211 * view `<select>`. It updates the `ids` attribute. |
| 212 */ |
| 213 updateView: function() { |
| 214 this.view = this.summaries.getView(this.$.view.value); |
| 215 }, |
| 216 |
| 217 accountChanged: function(newAccount, oldAccount) { |
| 218 if (newAccount) { |
| 219 this.property = newAccount.properties[0]; |
| 220 } |
| 221 }, |
| 222 |
| 223 propertyChanged: function(newProperty, oldProperty) { |
| 224 if (newProperty) { |
| 225 this.view = newProperty.views[0]; |
| 226 // this.view = Path.get('views[0]').getValueFrom(newProperty); |
| 227 } |
| 228 }, |
| 229 |
| 230 viewChanged: function(newView, oldView) { |
| 231 this.ids = newView && 'ga:' + newView.id; |
| 232 }, |
| 233 |
| 234 idsChanged: function(newIds, oldIds) { |
| 235 // When `ids` is set or updated prior to fetching the account |
| 236 // summaries, do nothing. |
| 237 if (!this.summaries) return; |
| 238 |
| 239 var viewId = newIds && newIds.slice(3); |
| 240 var account = this.summaries.getAccountByViewId(viewId); |
| 241 var property = this.summaries.getPropertyByViewId(viewId); |
| 242 var view = this.summaries.getView(viewId); |
| 243 |
| 244 // Make sure the account data is valid before firing any events. |
| 245 if (account && property && view) { |
| 246 this.account = account; |
| 247 this.property = property; |
| 248 this.view = view; |
| 249 |
| 250 this.fireChangeEvent(); |
| 251 } |
| 252 // If the account data isn't valid, and there's no previous value |
| 253 // to fall back to, default to the first account. |
| 254 else if (!oldIds) { |
| 255 this.account = this.summaries.all()[0]; |
| 256 } |
| 257 }, |
| 258 |
| 259 /** |
| 260 * Fire a change event passing all the currently stored data. |
| 261 */ |
| 262 fireChangeEvent: function() { |
| 263 this.fire('analytics-dashboard-control-change', { |
| 264 ids: this.ids, |
| 265 account: this.account, |
| 266 property: this.property, |
| 267 view: this.view |
| 268 }); |
| 269 } |
| 270 |
| 271 }); |
| 272 |
| 273 })(); |
| 274 |
| 275 </script> |
| OLD | NEW |