Created
August 5, 2020 15:44
-
-
Save javaadpatel/0e81394da543bc747e1c6edb882b0959 to your computer and use it in GitHub Desktop.
Google Analytics Netlify Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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