Created
December 20, 2016 13:59
-
-
Save LindaLawton/eb95ed53c7ae18273434ff6b4789dc7d to your computer and use it in GitHub Desktop.
Powershell example using the Google analytics reporting api. Requires a refresh token to work.
This file contains 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
Set-PSDebug -Off | |
Clear-Host | |
########################################################################################################################## | |
# | |
# Using refresh token to get new access token | |
# The access token is used to access an api by sending the access_token parm with any request. | |
# Access tokens are only valid for about an hour after that you will need to request a new one using your refresh_token | |
# | |
########################################################################################################################## | |
$clientId = "-9c6a8mimeradap3nn24ac76urlpdvhoe.apps.googleusercontent.com"; | |
$secret = "jUFTGhA8Z7FelntdvUz10fP5"; | |
$redirectURI = "urn:ietf:wg:oauth:2.0:oob"; | |
$refreshToken = "1/t8yW_v0gnqudE3y0_J6RKOqV5ek25Whogp49leCGqt8"; | |
$refreshTokenParams = @{ | |
client_id=$clientId; | |
client_secret=$secret; | |
refresh_token=$refreshToken; | |
grant_type='refresh_token'; | |
} | |
$token = Invoke-RestMethod -Uri "https://accounts.google.com/o/oauth2/token" -Method POST -Body $refreshTokenParams | |
"Response:" | |
"Access Token: $($token.access_token)" | |
"----------------------------------------------" | |
$seporator = " "; | |
$viewid = "66472356" | |
$startDate = "2014-09-01" | |
$endDate = "2014-10-01" | |
$dimensions = '{"name":"ga:usertype"},{"name":"ga:browser"}' | |
$metrics = '{"expression":"ga:sessions"},{"expression":"ga:users"}' | |
$analyticsRequest = '{"reportRequests":[{"viewId":"' + $($viewid) + '","dateRanges":[{"startDate":"' + $($startDate) + '","endDate":"' + $($endDate) + '"}],"samplingLevel":"LARGE","dimensions":[' + $($dimensions) + '],"metrics":[' + $($metrics) + '],"pageToken":"0","pageSize":1000,"includeEmptyRows":true,"hideTotals":true,"hideValueRanges":true}]}' | |
$analyticsRequest | |
$data = Invoke-RestMethod -ContentType 'application/json' -Uri "https://analyticsreporting.googleapis.com/v4/reports:batchGet?access_token=$($token.access_token)" -Method POST -Body $analyticsRequest | |
$data | Select-Object -ExpandProperty reports | Select -ExpandProperty data | Select -ExpandProperty rows | Format-Table $_.dimensions | |
$data | Format-List | |
<# | |
foreach ($report in $data.reports) { | |
"Rows:" + $report.data.rows.Count | |
foreach ($row in $report.data.rows) { | |
$output = "" | |
foreach($dim in $row.dimensions) { | |
$output += $dim + $seporator | |
} | |
foreach($met in $row.metrics) { | |
foreach($v in $met.values) { | |
$output += $v + $seporator | |
} | |
} | |
#| Out-File -Append C:\Users\linda_l\Documents\debug.txt | |
$output | |
} | |
} | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment