-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathga-code.js
134 lines (116 loc) · 3.12 KB
/
ga-code.js
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
//Debug Mode
var debugMode = true;
$(document).ready(function($) {
if (debugMode) {
$("#autofill").click(function() {
$("#metric").val("ga:sessions");
$("#dim").val("ga:date");
$("#start").val("2016-12-01");
$("#end").val("2017-02-28");
});
};
$("#error").hide();
});
gapi.analytics.ready(function() {
var gaID = "";
var options = {
ids: ""
};
var startDate, endDate, gaOutput;
gapi.analytics.auth.authorize({
container: 'embed-api-auth-container',
//ADD API KEY HERE:
clientid: '877106660496-bvm53u3hi8jkb7nfmiait66dfgsmioku.apps.googleusercontent.com'
});
var viewSelector = new gapi.analytics.ViewSelector({
container: 'view-selector-container'
});
// Render the view selector to the page.
viewSelector.execute();
function grabValue(id) {
var value = $("#" + id).val();
return value
}
/**
* Create a new DataChart instance with the given query parameters
* and Google chart options. It will be rendered inside an element
* with the id "chart-container".
*/
viewSelector.on('change', function(ids) {
options.ids = ids;
gaID = ids;
console.log(options.ids);
});
//
// Button Click
//
$("#get-data").click(function() {
var campaign = $("#campaign").val();
//console.log("campaign = " + campaign)
//grabDates("start","end");
var dataChart = new gapi.analytics.googleCharts.DataChart({
query: {
ids: gaID,
metrics: grabValue("metric"),
dimensions: grabValue("dim"),
'start-date': grabValue("start"),
'end-date': grabValue("end")
},
chart: {
container: 'chart-container',
type: 'LINE',
options: {
width: '100%'
}
}
});
var dataTable = new gapi.analytics.report.Data({
query: {
ids: gaID,
metrics: grabValue("metric"),
dimensions: grabValue("dim"),
'start-date': grabValue("start"),
'end-date': grabValue("end")
}
})
if (campaign.length > 0) {
//pullCampaignParam()
dataTable.set({
query: {
filters: "ga:campaign==" + campaign
}
})
dataChart.set({
query: {
filters: "ga:campaign==" + campaign
}
})
};
//console.log("button clicked")
dataTable.on('success', function(response) {
console.log(response);
console.log(response.query.metrics[0])
//console.log(JSON.stringify(response.totalsForAllResults))
for (var prop in response.totalsForAllResults) {
console.log(response.totalsForAllResults)
console.log('break')
break;
};
console.log(Object.keys(response.totalsForAllResults)[0])
console.log(Object.values(response.totalsForAllResults)[0])
$("#result-text").html("<h2>Results:</h2><p class='lead'>There were <b>"+Object.values(response.totalsForAllResults)[0]+"</b> "+Object.keys(response.totalsForAllResults)[0]+" between "+grabValue('start')+" and "+grabValue('end')+"</p>")
});
dataTable.on('error', function(response){
console.log('error');
console.log(response.error.message)
});
dataChart.on('error', function(response){
console.log('error');
$("#error").show();
$('#error-msg').html(response.error.message)
});
dataTable.execute();
dataChart.execute();
$("#error").hide();
});
});