-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgatsby-node.js
More file actions
129 lines (106 loc) · 2.82 KB
/
gatsby-node.js
File metadata and controls
129 lines (106 loc) · 2.82 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
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
const { google } = require('googleapis')
const replace = require('lodash.replace')
require('dotenv').config({
// path: `.env.${process.env.NODE_ENV}`,
path: '.env.production',
})
let data
function getPopularPosts() {
const VIEW_ID = 'ga:196730733'
// fix for failing build on the cloud
const googleApiKey = replace(process.env.GA_SERVICE_ACCOUNT_KEY, new RegExp('\\\\n', 'g'), '\n')
const jwtClient = new google.auth.JWT(
process.env.GA_SERVICE_ACCOUNT,
null,
googleApiKey,
['https://www.googleapis.com/auth/analytics.readonly'],
null,
)
return new Promise((resolve, reject) => {
// Do async job
jwtClient.authorize((err, tokens) => {
if (err) {
console.log(err)
return
}
const analytics = google.analytics('v3')
analytics.data.ga.get(
{
auth: jwtClient,
ids: VIEW_ID,
metrics: 'ga:uniquePageviews',
dimensions: 'ga:pagePath',
'start-date': '2019-01-01', // or 30daysAgo
'end-date': 'today',
sort: '-ga:uniquePageviews',
'max-results': 5,
filters: 'ga:pagePath=~/blog/.*/',
},
(err, response) => {
if (err) {
reject(err)
}
// console.log(JSON.stringify(response, null, 4))
console.log(JSON.stringify(response.data.rows, null, 4))
data = response.data.rows
resolve(data)
},
)
})
})
}
exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }) => {
const { createNode } = actions
await getPopularPosts()
const myData = {
popularPosts: data,
}
const nodeContent = JSON.stringify(myData)
const nodeMeta = {
id: createNodeId('popular-posts'),
parent: null,
children: [],
internal: {
type: 'googleApiData',
mediaType: 'text/html',
content: nodeContent,
contentDigest: createContentDigest(myData),
},
}
const node = Object.assign({}, myData, nodeMeta)
createNode(node)
}
exports.createPages = async ({ actions, graphql, reporter }) => {
const result = await graphql(`
query {
allMdx(filter: { frontmatter: { published: { eq: true } } }) {
nodes {
frontmatter {
slug
}
}
}
}
`)
if (result.errors) {
reporter.panic('failed to create posts', result.errors)
}
const posts = result.data.allMdx.nodes
posts.forEach((post) => {
actions.createPage({
path: post.frontmatter.slug,
component: require.resolve('./src/templates/post.js'),
context: {
slug: post.frontmatter.slug,
},
})
})
}
// exports.onCreateNode = ({ node, getNode, actions }) => {
// const { createNodeField } = actions
// createNodeField({
// node,
// name: 'popularPosts',
// value: data,
// })
// }