通过 Node.js 和 Express 连接到 google analytics API 的最佳方法是什么?
what is the best method to connect to google analytics API via Node.js and Express?
我想知道使用 Node.js 连接到我的 google 分析 API 的最佳方式是什么。我已经尝试了下面的方法,但我遇到了一些小错误,并且很难找到帮助我解决问题的文档。只是想知道这种方法是否是最好的方法。感谢阅读!
我正在尝试可视化 google 网络应用程序上的分析数据。因此,我认为第一步是从 google 分析 API 请求某些数据。我在 Node.js 和 Express 工作。 googleAuth.getClient 调用后出现未找到文件错误。这是我当前的代码:
const Router = require('express').Router;
const {google} = require('googleapis');
const {auth: googleAuth} = require('google-auth-library');
const responses = require('../lib/responses');
const auth = require('../lib/auth');
const router = Router({ mergeParams: true });
// Use the auth middleware to require a token
// for each non whitelisted request
router.use(auth.authMiddleware);
const viewId = '*********';
router.get('/data', async (req, res) => {
try {
const client = await googleAuth.getClient({
scopes: [
'https://www.googleapis.com/auth/analytics.readonly'
]
});
}catch(err){
console.log(err);
}
console.log("client ", client)
const url = `https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A${viewId}&start-date=30daysAgo&end-date=2018-08-28&metrics=ga%3Apageviews&dimensions=ga%3ApagePath&sort=-ga%3Apageviews`;
try {
const outcome = await client.request({ url });
}catch(err){
console.log(err);
}
return responses
.success ( res, outcome.data )
.error ( );
});
module.exports = router;
https://flaviocopes.com/google-analytics-api-nodejs/
const { google } = require('googleapis')
const scopes = 'https://www.googleapis.com/auth/analytics.readonly'
const jwt = new google.auth.JWT(process.env.CLIENT_EMAIL, null,
process.env.PRIVATE_KEY, scopes)
const view_id = 'XXXXX'
async function getData() {
const response = await jwt.authorize()
const result = await google.analytics('v3').data.ga.get({
'auth': jwt,
'ids': 'ga:' + view_id,
'start-date': '30daysAgo',
'end-date': 'today',
'metrics': 'ga:pageviews'
})
console.dir(result)
}
getData()
我想知道使用 Node.js 连接到我的 google 分析 API 的最佳方式是什么。我已经尝试了下面的方法,但我遇到了一些小错误,并且很难找到帮助我解决问题的文档。只是想知道这种方法是否是最好的方法。感谢阅读!
我正在尝试可视化 google 网络应用程序上的分析数据。因此,我认为第一步是从 google 分析 API 请求某些数据。我在 Node.js 和 Express 工作。 googleAuth.getClient 调用后出现未找到文件错误。这是我当前的代码:
const Router = require('express').Router;
const {google} = require('googleapis');
const {auth: googleAuth} = require('google-auth-library');
const responses = require('../lib/responses');
const auth = require('../lib/auth');
const router = Router({ mergeParams: true });
// Use the auth middleware to require a token
// for each non whitelisted request
router.use(auth.authMiddleware);
const viewId = '*********';
router.get('/data', async (req, res) => {
try {
const client = await googleAuth.getClient({
scopes: [
'https://www.googleapis.com/auth/analytics.readonly'
]
});
}catch(err){
console.log(err);
}
console.log("client ", client)
const url = `https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A${viewId}&start-date=30daysAgo&end-date=2018-08-28&metrics=ga%3Apageviews&dimensions=ga%3ApagePath&sort=-ga%3Apageviews`;
try {
const outcome = await client.request({ url });
}catch(err){
console.log(err);
}
return responses
.success ( res, outcome.data )
.error ( );
});
module.exports = router;
https://flaviocopes.com/google-analytics-api-nodejs/
const { google } = require('googleapis')
const scopes = 'https://www.googleapis.com/auth/analytics.readonly'
const jwt = new google.auth.JWT(process.env.CLIENT_EMAIL, null,
process.env.PRIVATE_KEY, scopes)
const view_id = 'XXXXX'
async function getData() {
const response = await jwt.authorize()
const result = await google.analytics('v3').data.ga.get({
'auth': jwt,
'ids': 'ga:' + view_id,
'start-date': '30daysAgo',
'end-date': 'today',
'metrics': 'ga:pageviews'
})
console.dir(result)
}
getData()