-
Notifications
You must be signed in to change notification settings - Fork 79
/
google-analytics-dashboard.html
137 lines (107 loc) · 3.46 KB
/
google-analytics-dashboard.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../google-signin/google-signin-aware.html">
<!--
Container element for binding Google Analytics controls to Google Analytics charts.
`<google-analytics-chart>` elements inside a `<google-analytics-dashboard>`
element will automatically update as control elements (e.g.
`<google-analytics-view-selector>` or `<google-analytics-date-selector>`)
update query parameters.
##### Example
<google-analytics-dashboard>
<google-analytics-view-selector></google-analytics-view-selector>
<google-analytics-date-selector></google-analytics-date-selector>
<google-analytics-chart
metrics="ga:sessions"
dimensions="ga:country"
sort="-ga:sessions"
max-results="5"
chart-type="column">
</google-analytics-chart>
</google-analytics-dashboard>
@element google-analytics-dashboard
@demo
@homepage https://googlewebcomponents.github.io/google-analytics
-->
<dom-module id="google-analytics-dashboard">
<template>
<google-signin-aware
on-google-signin-aware-success="_signedIn"
on-google-signin-aware-signed-out="_signedOut"></google-signin-aware>
<content id="content"></content>
</template>
</dom-module>
<script>
(function() {
'use strict';
Polymer({
is: 'google-analytics-dashboard',
properties: {
/**
* The `query` attribute represents the internal query object of this
* dashboard. It is updated when control elements fire the
* `analytics-dashboard-control-change` event and pass along query data.
*/
query: {
type: Object,
value: function() { return {}; }
},
/**
* True if user has been authorized
*/
authorized: {
type: Boolean,
value: false,
reflectToAttribute: true
}
},
listeners: {
'analytics-dashboard-control-change': 'queryUpdated'
},
ready: function() {
this.updateChildren();
},
/**
* The `queryUpdated` method is the callback for whenever the
* `analytics-dashboard-control-change` event is fired. It updates the
* query attribute, which is then sent to child charts.
*
* @method queryUpdated
* @param {CustomEvent} event - The event with the query data.
*/
queryUpdated: function(event) {
if (!this.query) {
this.query = {};
}
// Update `this.query` with the passed event data.
Object.keys(event.detail).forEach(function(key) {
this.query[key] = event.detail[key];
}.bind(this))
this.updateChildren();
},
/**
* The `updateChildren` method updates each of this dashboards
* `<google-analytics-chart>` element with its current query value.
*
* @method updateChildren
*/
updateChildren: function() {
if (!this.authorized) {
return;
}
var charts = Polymer.dom(this).querySelectorAll('google-analytics-chart');
for (var i = 0, chart; chart = charts[i]; i++) {
Object.keys(this.query).forEach(function(key) {
chart[key] = this.query[key];
}.bind(this));
}
},
_signedIn: function() {
this.authorized = true;
this.updateChildren();
},
_signedOut: function() {
this.authorized = false;
}
});
}());
</script>