-
Notifications
You must be signed in to change notification settings - Fork 79
/
google-analytics-date-selector.html
218 lines (188 loc) · 5.76 KB
/
google-analytics-date-selector.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
<link rel="import" href="../polymer/polymer.html">
<!--
Element for selecting the start and end date values for queries inside a
`<google-analytics-dashboard>` element.
##### Example
<google-analytics-dashboard>
<google-analytics-date-selector
start-date="30daysAgo"
end-date="today">
</google-analytics-date-selector>
<google-analytics-chart
ids="ga:1174"
metrics="ga:sessions"
dimensions="ga:date">
</google-analytics-chart>
</google-analytics-dashboard>
@element google-analytics-date-selector
@demo
@homepage https://googlewebcomponents.github.io/google-analytics
-->
<dom-module id="google-analytics-date-selector">
<style>
:host {
display: inline-block;
}
.control {
@apply(--google-analytics-date-selector-control);
}
label {
@apply(--google-analytics-date-selector-label);
}
input {
color: inherit;
font: inherit;
margin: 0;
@apply(--google-analytics-date-selector-input);
}
input:focus {
@apply(--google-analytics-date-selector-input-focus);
}
</style>
<template>
<span class="control">
<label for="startDate">Start Date</label>
<input
id="startDate"
type="date"
value="{{startDate::change}}"
min="{{minStartDate}}"
max="{{endDate}}">
</span>
<span class="control">
<label for="endDate">End Date</label>
<input
id="endDate"
type="date"
value="{{endDate::change}}"
min="{{startDate}}"
max="{{maxEndDate}}">
</span>
</template>
</dom-module>
<script>
(function() {
'use strict';
/**
* Fired when the users changes the start or end date.
*
* @param {Object} query The updated query params.
* @event analytics-dashboard-control-change
*/
var nDaysAgo = /(\d+)daysAgo/;
var dateFormat = /\d{4}\-\d{2}\-\d{2}/;
/**
* Convert a date acceptable to the Core Reporting API (e.g. `today`,
* `yesterday` or `NdaysAgo`) into the format YYYY-MM-DD. Dates
* already in that format are simply returned.
* @return {string} The formatted date.
*/
function convertDate(str) {
// If str is in the proper format, do nothing.
if (dateFormat.test(str)) return str
var match = nDaysAgo.exec(str);
if (match) {
return daysAgo(+match[1])
} else if (str == 'today') {
return daysAgo(0)
} else if (str == 'yesterday') {
return daysAgo(1)
} else {
throw new Error('Cannot convert date ' + str);
}
}
/**
* Accept a number and return a date formatted as YYYY-MM-DD that
* represents that many days ago.
* @return {string} The formatted date.
*/
function daysAgo(numDays) {
var date = new Date();
date.setDate(date.getDate() - numDays);
var month = String(date.getMonth() + 1);
month = month.length == 1 ? '0' + month: month;
var day = String(date.getDate());
day = day.length == 1 ? '0' + day: day;
return date.getFullYear() + '-' + month + '-' + day;
}
Polymer({
is: 'google-analytics-date-selector',
properties: {
/**
* 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: convertDate('7daysAgo'),
observer: 'startDateChanged',
notify: true
},
/**
* 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: convertDate('yesterday'),
observer: 'endDateChanged',
notify: true
},
/**
* The `minStartDate` attribute is used as the `min` attribute on the
* start date `<input>`.
*
* @attribute minStartDate
* @default '2005-01-01'
* @type string
*/
minStartDate: {
type: String,
value: '2005-01-01'
},
/**
* The `maxEndDate` attribute is used as the `max` attribute on the
* end date `<input>`.
*
* @attribute maxEndDate
* @default 'today'
* @type string
*/
maxEndDate: {
type: String,
value: convertDate('today')
}
},
startDateChanged: function(cur, old) {
this.startDate = convertDate(cur);
this.$.startDate.value = this.startDate;
this.fire('analytics-dashboard-control-change', {
startDate: this.startDate
});
},
endDateChanged: function(cur, old) {
this.endDate = convertDate(cur);
this.$.endDate.value = this.endDate;
this.fire('analytics-dashboard-control-change', {
endDate: this.endDate
});
}
});
}());
</script>