Next Js Blog Most Read Posts from Google Analytics #14720
Unanswered
babyPrince
asked this question in
Help
Replies: 1 comment 1 reply
-
I have the perfect article for you! https://leerob.io/blog/google-analytics-api-nextjs This article takes you from start to finish for creating an API route to fetch information from Google Analytics API. import { google } from 'googleapis';
let googleAuth;
export default async (_, res) => {
googleAuth = new google.auth.GoogleAuth({
credentials: {
client_email: process.env.GOOGLE_CLIENT_EMAIL,
client_id: process.env.GOOGLE_CLIENT_ID,
private_key: process.env.GOOGLE_PRIVATE_KEY
},
scopes: ['https://www.googleapis.com/auth/analytics.readonly']
});
const analytics = google.analytics({
auth,
version: 'v3'
});
const startDate = req.query.startDate;
const response = await analytics.data.ga.get({
'end-date': 'today',
ids: 'ga:ID',
metrics: 'ga:pageviews',
'start-date': startDate
});
return res.status(200).json({
pageViews: response.data.rows[0][0]
});
}; |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey there,
I'm pretty new to Next and trying to figure how to approach and create something similar to https://github.com/BlackBoxVision/gatsby-source-analytics-most-read-posts.
I have Next hooked up to Sanity CMS for my blog and would like to get the 10 most read articles/posts on build based on GA data.
Has anyone worked on something like this?
Currently I don't have any code to share, just gathering information on what's possible.
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions