forked from peterkhayes/inlineAB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
173 lines (139 loc) · 4.65 KB
/
app.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
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
var express = require('express');
var http = require('http');
var path = require('path');
var googleapis = require('googleapis');
var restler = require('restler');
var fs = require('fs');
var url = require('url');
var redirect = process.env.REDIRECT_URL || 'http://inlineAB.azurewebsites.net';
var clientId = process.env.CLIENT_ID || 'JoeyIsSad';
var clientSecret = process.env.CLIENT_SECRET || 'KGBAgentAleksandrProkop';
var browserAPIKey = process.env.B_API_KEY || 'RichsBoyGooglePete';
var serverAPIKey = process.env.S_API_KEY || 'ItsLike_ButFor_ByPeter';
var redirectURL = process.env.REDIRECT_URL || 'GavinWasHere';
var accountId = '46140385'; // used to generate propertyId
var webPropertyId = 'UA-46140385-1'; // used with accountId to generate profileId
var profileId = '79670733'; // used for querying
// Create server
var app = express();
// Configure server
app.set('port', process.env.PORT || 3000);
app.use(express.favicon());
// Mount statics
app.use(express.static(path.join(__dirname, '/client')));
app.use(express.bodyParser());
// Save reference to Oauth2Client
var OAuth2Client = googleapis.OAuth2Client;
app.get('/downloadCustom', function(req, res){
var urlData = url.parse(req.url,true).query;
var expID = urlData.expID;
var variations = urlData.vars;
var snippetID = urlData.snipID;
var snippetSite = urlData.snipSite;
var filePath = __dirname + '/client/js/inlineAB.js';
fs.readFile(filePath, function(err, inlineABjs){
var inlineABstring = inlineABjs.toString();
var customizedScript = inlineABstring
.replace("'PASTE-EXPERIMENT-ID'", "'" + expID + "'")
.replace("['VARIATION1', 'VARIATION2']", variations)
.replace("UA-XXXXXXXX-X", snippetID)
.replace("SNIPPITWEBSITE", snippetSite);
res.setHeader('Content-disposition', 'attachment; filename=inlineAB.js');
res.setHeader('Content-type', 'text/plain');
res.charset = 'UTF-8';
// res.download(customizedScript);
// res.attachment(customizedScript);
res.write(customizedScript);
res.end();
});
});
/*
THIS IS A LARGE BLOCK COMMENT.
*/
app.post('/updateExperiment', function(req,res){
var oauth2Client = new OAuth2Client(clientId, clientSecret, redirectURL);
var access_token = req.body.token.access_token;
oauth2Client.credentials = {
access_token: access_token
};
googleapis
.discover('analytics', 'v3')
.execute(function(err, client) {
var request = client.analytics.management.experiments.update({
accountId : req.body.accountId,
webPropertyId : req.body.webPropertyId,
profileId : req.body.profileId,
experimentId : req.body.experimentId
}, req.body.body)
.withApiKey(serverAPIKey)
.withAuthClient(oauth2Client)
request.execute(function(err,result){
if (err){
console.log(err);
res.send(500);
} else {
res.send(200);
}
});
});
});
app.post('/deleteExperiment', function(req,res){
var oauth2Client = new OAuth2Client(clientId, clientSecret, redirectURL);
var access_token = req.body.token.access_token;
oauth2Client.credentials = {
access_token: access_token
};
googleapis
.discover('analytics', 'v3')
.execute(function(err, client) {
var request = client.analytics.management.experiments.delete({
accountId : req.body.accountId,
webPropertyId : req.body.webPropertyId,
profileId : req.body.profileId,
experimentId : req.body.experimentId
})
.withApiKey(serverAPIKey)
.withAuthClient(oauth2Client)
request.execute(function(err,result){
if (err){
console.log(err);
res.send(500);
} else {
res.send(200);
}
});
});
});
app.post('/createExperiment', function(req,res){
var oauth2Client = new OAuth2Client(clientId, clientSecret, redirectURL);
var access_token = req.body.token.access_token;
oauth2Client.credentials = {
access_token: access_token
};
googleapis
.discover('analytics', 'v3')
.execute(function(err, client) {
var request = client.analytics.management.experiments.insert({
accountId : req.body.accountId,
webPropertyId : req.body.webPropertyId,
profileId : req.body.profileId
}, req.body.body)
.withApiKey(serverAPIKey)
.withAuthClient(oauth2Client)
request.execute(function(err,result){
if (err){
console.log(err);
res.send(500);
} else {
res.send(result);
}
});
});
});
// Start server
http.createServer(app).listen(app.get('port'), function() {
console.log(
'Express server listening on port ' + app.get('port'),
'\nPress Ctrl+C to shutdown'
);
});