-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
36 lines (32 loc) · 954 Bytes
/
gatsby-node.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
const { google } = require('googleapis')
exports.sourceNodes = async (
{ actions, createContentDigest },
pluginOptions
) => {
const { createNode } = actions
const { email, keyFile, viewId, startDate } = pluginOptions
const scopes = [`https://www.googleapis.com/auth/analytics.readonly`]
const jwt = new google.auth.JWT(email, keyFile, null, scopes)
await jwt.authorize()
const views = await google.analytics('v3').data.ga.get({
auth: jwt,
ids: `ga:${viewId}`,
'start-date': startDate || '2019-01-01',
'end-date': 'today',
dimensions: 'ga:pagePath',
metrics: 'ga:pageViews',
sort: '-ga:pageViews',
})
for (let [path, count] of views.data.rows) {
createNode({
id: path,
slug: path,
count: Number(count),
internal: {
type: `SheetViews`,
mediaType: `text/plain`,
contentDigest: createContentDigest(JSON.stringify({ path, count })),
},
})
}
}