Psst... Dessert Data connects all your marketing data into a neatly organized database. You can use the same query to pull spend for Bing, TikTok, or Google.
Don't worry about APIs changes or database uptime. We handle all that for you.
GET STARTED FREENote - the HTTP requests below are compatible with the VSCode Rest Client. I find this extension easier than Postman because you can check-in your requests with your code. Parameters like {{ $dotenv SOME_ID }}
are referencing environment variables from a .env file.
Alsø Alsø note - this post goes into details for setting up your Google Ads accounts. If you just want the code, then scroll to the bottom.
[your google cloud email]
dessertdata.com
OAuth 2.0 Client IDs
Web application
[https://dessertdata.com, http://localhost]
[https://dessertdata.com, http://localhost]
Note - write down your OAuth Client ID and Client Secret. You will need them in the next step.
// Remove newlines
GET https://accounts.google.com/o/oauth2/v2/auth
?scope=https://www.googleapis.com/auth/adwords
&access_type=offline
&include_granted_scopes=true
&response_type=code
&state=anything
&redirect_uri=http://localhost
&client_id={{ $dotenv GA_CLIENT_ID }}
Once you authorize your client, you will be redirected to a page that seems broken, but the URL will contain a code parameter. Copy that code and use it to get a refresh token.
http://localhost/?state=anything&code=[YOUR CODE]&scope=...
Using the [YOUR CODE] value from the above URL, you can craft an API call to get an access token, and more importantly refresh token.
POST https://oauth2.googleapis.com/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
code=[YOUR CODE]
&client_id={{ $dotenv GA_CLIENT_ID }}
&client_secret={{ $dotenv GA_CLIENT_SECRET }}
&redirect_uri=http://localhost
&grant_type=authorization_code
POST https://www.googleapis.com/oauth2/v3/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token
&client_id={{ $dotenv GA_CLIENT_ID }}
&client_secret={{ $dotenv GA_CLIENT_SECRET }}
&refresh_token={{ $dotenv GA_REFRESH_TOKEN }}
Note - You need to remove dashes from your customer ID and manager ID for the query. So 123-456-7890 becomes 1234567890.
POST https://googleads.googleapis.com/v16/customers/[YOUR CUSTOMER ID]/googleAds:searchStream HTTP/1.1
Content-Type: application/json
Accept: application/json
Authorization: Bearer {{ $dotenv GA_ACCESS_TOKEN }}
linked-customer-id: [YOUR MANAGER ID]
developer-token: {{ $dotenv GA_DEVELOPER_TOKEN }}
{
"query" : "SELECT
segments.date,
metrics.clicks,
metrics.conversions,
metrics.impressions,
metrics.cost_per_all_conversions,
metrics.cost_per_conversion,
metrics.cost_per_all_conversions
FROM keyword_view
WHERE segments.date DURING LAST_30_DAYS"
}