Skip to content

Instantly share code, notes, and snippets.

@javaadpatel
Created August 5, 2020 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javaadpatel/0e81394da543bc747e1c6edb882b0959 to your computer and use it in GitHub Desktop.
Save javaadpatel/0e81394da543bc747e1c6edb882b0959 to your computer and use it in GitHub Desktop.
Google Analytics Netlify Function
/**
* Retrieves unique page views from Google Analytics
* @param {*} pagePath the path to filter from results
*/
async function getUniquePageViews(pagePath) {
const defaults = {
'auth': jwt,
'ids': 'ga:' + process.env.GA_VIEW_ID,
}
await jwt.authorize()
const metricRequestOptions = {
...defaults,
'start-date': startDate,
'end-date': 'today',
'metrics': 'ga:uniquePageviews',
'dimensions': 'ga:pagePath'
};
if (pagePath)
metricRequestOptions.filters = `ga:pagePath==${pagePath}`
const result = await google.analytics('v3').data.ga.get(metricRequestOptions)
return result.data.rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment