| OLD | NEW |
| (Empty) | |
| 1 <link rel="import" href="../polymer/polymer.html"> |
| 2 <!-- |
| 3 Element for selecting the start and end date values for queries inside a |
| 4 `<google-analytics-dashboard>` element. |
| 5 |
| 6 ##### Example |
| 7 |
| 8 <google-analytics-dashboard> |
| 9 |
| 10 <google-analytics-date-selector |
| 11 startDate="30daysAgo" |
| 12 endDate="today"> |
| 13 </google-analytics-date-selector> |
| 14 |
| 15 <google-analytics-chart |
| 16 ids="ga:1174" |
| 17 metrics="ga:sessions" |
| 18 dimensions="ga:date"> |
| 19 </google-analytics-chart> |
| 20 |
| 21 </google-analytics-dashboard> |
| 22 |
| 23 @element google-analytics-date-selector |
| 24 @extends google-analytics-base |
| 25 @blurb Element for selecting the start and end date values for Google Analytics
queries |
| 26 @status alpha |
| 27 @homepage https://googlewebcomponents.github.io/google-analytics |
| 28 --> |
| 29 |
| 30 <dom-module id="google-analytics-date-selector"> |
| 31 <style> |
| 32 input { |
| 33 color: inherit; |
| 34 font: inherit; |
| 35 margin: 0; |
| 36 } |
| 37 </style> |
| 38 <template> |
| 39 <span class="control"> |
| 40 <label for="startDate">Start Date</label> |
| 41 <input |
| 42 id="startDate" |
| 43 type="date" |
| 44 value="{{startDate::change}}" |
| 45 min="{{minStartDate}}" |
| 46 max="{{endDate}}"> |
| 47 </span> |
| 48 <span class="control"> |
| 49 <label for="endDate">End Date</label> |
| 50 <input |
| 51 id="endDate" |
| 52 type="date" |
| 53 value="{{endDate::change}}" |
| 54 min="{{startDate}}" |
| 55 max="{{maxEndDate}}"> |
| 56 </span> |
| 57 </template> |
| 58 </dom-module> |
| 59 <script> |
| 60 |
| 61 (function() { |
| 62 |
| 63 'use strict'; |
| 64 |
| 65 /** |
| 66 * Fired when the users changes the start or end date. |
| 67 * |
| 68 * @param {Object} query The updated query params. |
| 69 * @event analytics-dashboard-control-change |
| 70 */ |
| 71 |
| 72 |
| 73 var nDaysAgo = /(\d+)daysAgo/; |
| 74 var dateFormat = /\d{4}\-\d{2}\-\d{2}/; |
| 75 |
| 76 /** |
| 77 * Convert a date acceptable to the Core Reporting API (e.g. `today`, |
| 78 * `yesterday` or `NdaysAgo`) into the format YYYY-MM-DD. Dates |
| 79 * already in that format are simply returned. |
| 80 * @return {string} The formatted date. |
| 81 */ |
| 82 function convertDate(str) { |
| 83 // If str is in the proper format, do nothing. |
| 84 if (dateFormat.test(str)) return str |
| 85 |
| 86 var match = nDaysAgo.exec(str); |
| 87 if (match) { |
| 88 return daysAgo(+match[1]) |
| 89 } else if (str == 'today') { |
| 90 return daysAgo(0) |
| 91 } else if (str == 'yesterday') { |
| 92 return daysAgo(1) |
| 93 } else { |
| 94 throw new Error('Cannot convert date ' + str); |
| 95 } |
| 96 } |
| 97 |
| 98 /** |
| 99 * Accept a number and return a date formatted as YYYY-MM-DD that |
| 100 * represents that many days ago. |
| 101 * @return {string} The formatted date. |
| 102 */ |
| 103 function daysAgo(numDays) { |
| 104 var date = new Date(); |
| 105 date.setDate(date.getDate() - numDays); |
| 106 var month = String(date.getMonth() + 1); |
| 107 month = month.length == 1 ? '0' + month: month; |
| 108 var day = String(date.getDate()); |
| 109 day = day.length == 1 ? '0' + day: day; |
| 110 return date.getFullYear() + '-' + month + '-' + day; |
| 111 } |
| 112 |
| 113 Polymer({ |
| 114 |
| 115 is: 'google-analytics-date-selector', |
| 116 |
| 117 properties: { |
| 118 /** |
| 119 * The `startDate` attribute is the start date for fetching Analytics |
| 120 * data. Requests can specify a start date formatted as YYYY-MM-DD, or |
| 121 * as a relative date (e.g., today, yesterday, or NdaysAgo where N is a |
| 122 * positive integer). |
| 123 * |
| 124 * 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. |
| 125 * |
| 126 * @attribute startDate |
| 127 * @default '7daysAgo' |
| 128 * @type string |
| 129 */ |
| 130 startDate: { |
| 131 type: String, |
| 132 value: convertDate('7daysAgo'), |
| 133 observer: 'startDateChanged', |
| 134 notify: true |
| 135 }, |
| 136 |
| 137 /** |
| 138 * The `endDate` attribute is the end date for fetching Analytics |
| 139 * data. Requests can specify an end date formatted as YYYY-MM-DD, or |
| 140 * as a relative date (e.g., today, yesterday, or NdaysAgo where N is a |
| 141 * positive integer). |
| 142 * |
| 143 * 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. |
| 144 * |
| 145 * @attribute endDate |
| 146 * @default 'yesterday' |
| 147 * @type string |
| 148 */ |
| 149 endDate: { |
| 150 type: String, |
| 151 value: convertDate('yesterday'), |
| 152 observer: 'endDateChanged', |
| 153 notify: true |
| 154 }, |
| 155 |
| 156 /** |
| 157 * The `minStartDate` attribute is used as the `min` attribute on the |
| 158 * start date `<input>`. |
| 159 * |
| 160 * @attribute minStartDate |
| 161 * @default '2005-01-01' |
| 162 * @type string |
| 163 */ |
| 164 minStartDate: { |
| 165 type: String, |
| 166 value: '2005-01-01' |
| 167 }, |
| 168 |
| 169 /** |
| 170 * The `maxEndDate` attribute is used as the `max` attribute on the |
| 171 * end date `<input>`. |
| 172 * |
| 173 * @attribute maxEndDate |
| 174 * @default 'today' |
| 175 * @type string |
| 176 */ |
| 177 maxEndDate: { |
| 178 type: String, |
| 179 value: convertDate('today') |
| 180 } |
| 181 }, |
| 182 |
| 183 startDateChanged: function(cur, old) { |
| 184 this.startDate = convertDate(cur); |
| 185 this.$.startDate.value = this.startDate; |
| 186 this.fire('analytics-dashboard-control-change', { |
| 187 startDate: this.startDate |
| 188 }); |
| 189 }, |
| 190 |
| 191 endDateChanged: function(cur, old) { |
| 192 this.endDate = convertDate(cur); |
| 193 this.$.endDate.value = this.endDate; |
| 194 this.fire('analytics-dashboard-control-change', { |
| 195 endDate: this.endDate |
| 196 }); |
| 197 } |
| 198 }); |
| 199 |
| 200 }()); |
| 201 |
| 202 </script> |
| 203 |
| OLD | NEW |