-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
160 lines (126 loc) · 3.66 KB
/
index.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
// https://developers.google.com/adsense/management/reporting/relative_dates
const Client = require("./src/Client");
const Request = require("./src/Request");
const DimensionFilter = require("./src/DimensionFilter");
const MetricFilter = require("./src/MetricFilter");
const ResultParser = require("./src/ResultParser");
const { google } = require("googleapis");
const cloneDeep = require("clone-deep");
const SimpleGA = function(param) {
this.client = null;
if (typeof param === "string") {
this.client = Client.createFromKeyFile(param);
} else {
if (param.key) {
this.client = Client.createFromKey(param.key);
}
if (param.keyFile) {
this.client = Client.createFromKeyFile(param.keyFile);
}
}
if (!this.client) {
throw Error("Google Analytics client wasn't created!");
}
this.client.authorize(function(err, tokens) {
if (err) {
throw new Error(err);
}
});
this.analytics = google.analyticsreporting("v4");
};
SimpleGA.prototype.runRaw = function(request, params = {}, currentPage = 1) {
var that = this;
return new Promise(function(resolve, reject) {
var entries = [];
var headers = null;
that.analytics.reports.batchGet(
{
auth: that.client,
resource: {
reportRequests: [request.make()]
}
},
async function(err, response) {
if (err) {
return reject(err);
}
var report = response.data.reports[0];
if (currentPage == 1) {
var metricColumnHeader = report.columnHeader.metricHeader.metricHeaderEntries.map(function(entry) {
return entry.name;
});
headers = {
dimensions: ResultParser.cleanKeys(report.columnHeader.dimensions),
metrics: ResultParser.cleanKeys(metricColumnHeader)
};
}
if(!("rows" in report.data)) {
return resolve({ headers, entries });
}
report.data.rows.forEach(function(entry) {
entries.push({
dimensions: entry.dimensions,
metrics: entry.metrics[0].values
});
});
// If we're at the last page, stop
if (params.pages && currentPage === params.pages) {
return resolve({ headers, entries });
}
// If there are more pages, get the results
if (report.nextPageToken) {
request.pageToken(report.nextPageToken);
var requestedData = await that.runRaw(
request,
{
pages: params.pages
},
currentPage + 1
);
entries = entries.concat(requestedData.entries);
}
return resolve({ headers, entries });
}
);
});
};
SimpleGA.prototype.run = function(request, params = {}) {
var that = this;
return new Promise(async function(resolve, reject){
if(request.get("pageSize") && !params.pages) {
params.pages = 1;
}
var result = await that.runRaw(request, params);
if (params.rawResults) {
return result;
}
var processedResult = [];
result.entries.forEach(function(entry) {
let dimensions = ResultParser.mergeKeyValueArrays(result.headers.dimensions, entry.dimensions);
let metrics = ResultParser.mergeKeyValueArrays(result.headers.metrics, entry.metrics);
processedResult.push(Object.assign({},dimensions,metrics));
});
if(params.rename && Object.keys(params.rename).length > 0 && processedResult.length > 0) {
let oldKeys = Object.keys(params.rename);
let entry = processedResult[0];
oldKeys = oldKeys.filter(function(oldKey){
return oldKey in entry;
});
processedResult = processedResult.map(function(entry){
oldKeys.forEach(function(oldKey){
let newKey = params.rename[oldKey];
entry[newKey] = entry[oldKey];
delete entry[oldKey];
});
return entry;
});
}
resolve(processedResult);
});
};
module.exports = {
SimpleGA,
Request,
DimensionFilter,
MetricFilter
};