Running reports on Google AdWords API with C#

Henrique Dal Bello
Aug 13 · 3 min read

Recently I had to run some reports on Google Ads API, for my surprise I found a difficult documentation on Google Ads developers page.
At least, for C# isn’t the best documentation I’ve seen. But I’m not here to complain about it.

Because of the lack of examples and questions over the internet about doing this with C#, I decided to bring here what I could achieve. So let’s start!

First of all we’ll need some keys to run it, for these follow the documentation instructions about OAuth2 for applications (not server), once finished, we’ll need to put them together in our appsettings.json file like this:

While coding, first of all we will download Google.Adwords package from NuGet with the command install-package GoogleAdWords -version 24.7.1 or if you want to pick the latest, just remove the -version param, or make it through the GUI by clicking Manage NuGet Packages:

First we need to authenticate on OAuth2, I suggest doing it using their playground, so we can get the refresh token and then we won’t need the playground anymore because our application will automatically refresh the authorization using this token.

Since we got our configuration file ready, we’ll now register our AdWordsAppConfig class on Startup.cs:

The most important thing is to use the same names as the above appsettings.json gist, because when you pass the IConfigurationSection reference to AdWordsAppConfig, their lib search for those exactly keys!
If in doubt about it, please check it on their GitHub here.

Everything done, now let’s run reports on AdWords API with their library!
They have several Attributes, Segments and Metrics in a report, which now you should know what you want to filter, what is better for you to group by and which metrics you want to see, for this example I will run Ad Performance report filtering by the following fields:

  1. Attributes: CampaignName and AdGroupName
  2. Segments: Date
  3. Metrics: Cost, CostPerConversion, Ctr, Impressions, Conversions, ConversionRate, Clicks and PercentNewVisitors

Let’s put them together in our service which will call GoogleAdWordsService:

Since we’re calling an external API, making calls asynchronous is the best option.

Explaining the code: The ReportDefinition instance represents your report, what’s the download format, date range, report type, etc.
Notice that all mentioned fields above are inside Selectors’ fields property. (No, you don’t need to specify which are Attributes, Segments or Fields)

After defining our report, we instantiate an User using our configuration and if expired, refresh our token.

And finally we need just more two steps to get our report:

  1. Run report (GetResponse() method);
  2. Download response (DownloadContent() method).

Alternatively you can save it into a path with SaveAsync() method.

To complete our API let’s just inject and call our service inside our ReportController :

Now, once we call /api/report our response will be in XML like this:

The source is available on my GitHub, you can download it if necessary.

Please notice that you need to ask GoogleAdWords for a Basic Api Access before running reports on Production environment. (API Central option on right top menu)

I hope this can help you. Particularly I’ve found really stressful to achieve this result for the first time I’ve made it, took like 2 or 3 days to understand how to authenticate and run the report.
The Google AdWords errors are really clarifying and well structured, but just when you know where to start and which classes to instantiate.


Henrique Dal Bello

Written by

Knowledge is for sharing. Developing bugs since 2012 💻

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade