Skip to content

Commit 082bbe9

Browse files
committed
Start with API call
0 parents  commit 082bbe9

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
*.txt
3+
*.env

index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const fetch = require('node-fetch');
2+
3+
const token = process.env.NETLIFY_TOKEN;
4+
const siteId = process.env.NETLIFY_SITE_ID;
5+
6+
7+
const startDate = new Date();
8+
startDate.setHours(0);
9+
startDate.setMinutes(0);
10+
startDate.setSeconds(0)
11+
startDate.setMilliseconds(0);
12+
const endDate = new Date();
13+
endDate.setHours(23);
14+
endDate.setMinutes(59);
15+
endDate.setSeconds(59);
16+
endDate.setMilliseconds(999);
17+
18+
let timezone = startDate.getTimezoneOffset() / 60 * -100;
19+
if (timezone >= 1000) {
20+
timezone = "+" + timezone;
21+
}
22+
if (timezone < 1000) {
23+
timezone = "+0" + timezone;
24+
}
25+
26+
if (timezone < 0) {
27+
timezone = timezone.replace("+", "-");
28+
}
29+
30+
const url = `https://analytics.services.netlify.com/v1/${siteId}/pageviews?from=${startDate.getTime().toFixed()}&to=${endDate.getTime()}&timezone=${timezone}&resolution=day`
31+
32+
console.log(url);
33+
34+
async function start() {
35+
var res = await fetch(url, {
36+
"credentials": "include",
37+
"headers": {
38+
"Content-Type": "application/json",
39+
"Authorization": `Bearer ${token}`,
40+
"Pragma": "no-cache",
41+
"Cache-Control": "no-cache"
42+
},
43+
"method": "GET",
44+
"mode": "cors"
45+
});
46+
47+
console.log(await res.json());
48+
}
49+
50+
start();

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "netlify-analytics-collector",
3+
"version": "0.0.1",
4+
"description": "A simple NodeJs application which saves netlify analytics to a CSV file",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"dependencies": {
10+
"node-fetch": "^2.6.1"
11+
},
12+
"devDependencies": {},
13+
"scripts": {
14+
"test": "echo \"Error: no test specified\" && exit 1"
15+
},
16+
"author": "Niklas Merz",
17+
"license": "MIT"
18+
}

0 commit comments

Comments
 (0)