-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (73 loc) · 2.5 KB
/
Copy pathscript.js
File metadata and controls
82 lines (73 loc) · 2.5 KB
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
// Replace with your client ID from the developer console.
var CLIENT_ID = '';
// Set authorized scope.
var SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'];
// Declare
var gapageviews, gausers;
function authorize(event) {
// Handles the authorization flow.
// `immediate` should be false when invoked from the button click.
var useImmdiate = event ? false : true;
var authData = {
client_id: CLIENT_ID,
scope: SCOPES,
immediate: useImmdiate
};
// Get the value of profileID textfield
var enteredProfileID = document.getElementById("profileID").value;
gapi.auth.authorize(authData, function(response) {
var authButton = document.getElementById('auth-button');
if (response.error) {
authButton.hidden = false;
}
else {
authButton.hidden = true;
// Load the Google Analytics client library.
gapi.client.load('analytics', 'v3').then(function() {
// Use the value of textfield
queryCoreReportingApi(enteredProfileID);
// Kickoff of accounts
queryAccounts();
});
}
});
}
function queryCoreReportingApi(profile) {
// Query the Core Reporting API for the number sessions for
// the past seven days.
gapi.client.analytics.data.ga.get({
'ids': 'ga:' + profile,
'start-date': 'yesterday',
'end-date': 'yesterday',
'metrics': 'ga:users, ga:pageviews'
})
.then(function(response) {
preparedisplay(response.result);
})
.then(null, function(err) {
// Log any errors.
console.log(err);
});
}
function preparedisplay(unformattedJson){
var totals = unformattedJson.totalsForAllResults;
gapageviews = JSON.stringify(totals['ga:pageviews'], null, 2).replace(/\"/g, "");
gausers= JSON.stringify(totals['ga:users'], null, 2).replace(/\"/g, "");
document.getElementById("show-pageviews").innerHTML = gapageviews.concat(" pageviews");
document.getElementById("show-users").innerHTML = gausers.concat(" users");
//sendtoslack(gapageviews, gausers);
}
function sendtoslack(pageviews, users){
var $slack_url = "";
var $text = "Yesterday: *" + users + " users* and *" + pageviews + " pageviews* on your web property." ;
var $payload = 'payload={"channel": "@","username": "Google Analytics", "icon_emoji": ":ga:", "text":"' + $text + '"}';
$.post(
$slack_url,
$payload
);
}
function getnewprofile(){
var $newprofile = document.getElementById("profileID").value;
console.log('Received new profile ID');
queryCoreReportingApi($newprofile);
}