Google’s APIs make getting Adsense (or any other Google service) data easy to download. The code below downloads Adsense data saving results to csv file.
The code uses Google’s AdSense Management API, OAuth 2.0 authorization and the google-api-python-client SDK.
When you run this code for the first time it will open a web browser to get approval for the API application to access your Adsense account data.
When this approval is granted the code saves a refresh token that is used for all future executions of the code instead of requiring the web browser approval. This is handy if you want to run this code using a cron job like I do.
Here is the code
In summary, the code does the following:
- Authenticates against API application
- Queries the scope to get list of accounts
- Loops through accounts
- Returns requested metrics and dimensions from Adsense account
- Writes results to csv file
import csv from datetime import datetime, timedelta from apiclient import sample_tools from apiclient.discovery import build from oauth2client import client todaydate = datetime.today().strftime('%Y-%m-%d') def main(): ## Authenticate and construct service scope = ['https://www.googleapis.com/auth/adsense.readonly'] client_secrets_file = 'client_secrets.json' ## Authenticate and construct service service, flags = sample_tools.init('', 'adsense', 'v1.4', __doc__, client_secrets_file, parents=, scope=scope) ## Get accounts accounts = service.accounts().list().execute() try: ## Get account(s) data results = service.accounts().reports().generate( accountId='pub-xxxxxxxxxxxxxxxxxx', startDate='2012-01-01', # choose your own start date endDate= todaydate, metric=['EARNINGS', 'CLICKS','AD_REQUESTS'], dimension=['DOMAIN_NAME','DATE','AD_FORMAT_NAME']).execute() except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize') ## output results into csv header = ['hostname','date','type','earnings','clicks','ad_requests'] with open('output_adsense.csv', 'wb') as file: file.write(','.join(header) + '\n') for row in results.get('rows'): file.write(','.join(row) + '\n') ## print status for log print str(datetime.now()) + ' adsense' if __name__ == '__main__': main()
Create API Application, get client_secrets.json
As alluded to above you need to create an API application. This is done in the Google Developers Console . If you haven’t already signed up for this you have to do that first.
If you haven’t already you will also have to create a Project. Your API application will be inside this Project. In the Developers Console you can create new Project with drop down at top right.
Once you have a Project you can go to the Enabled APIs tab to select which Google service API(s) you want to include in the project. For this code’s purposes you can just select Adsense Management API.
Create Application – Select API Type
Use the Create credentials button to create your new application. (You are creating the credentials for your new app.)
When you click the button you get choice of three options (see below). Important point that raises lots of questions: Adsense cannot use a Service Account to authenticate. That was what I thought I could use to avoid having to do user authentication since I am running code on my server in cron job.
No problem though because as you will see later, there is a one time only step to have user authorize Adsense account access. From that point on, the authentication is done using a refresh token.
So my code above uses the OAuth client ID. So select that for your application too.
Create Application – Select Application Type
Then you will be asked to choose the application type. For my purposes I do not want web application which will require real redirect URIs. I just want simple plain app. So I chose Other.
You will then get client secret and client id which you can copy or get later. You don’t need these as you will get them in the client_secret.json file you download next.
So just change default application name to something unique so you can identify it later.
Create Application – OAuth Consent Screen
This is not something critical but you have to provide an email and application name to show in the browser pop up for authentication.
The idea is to give a user (you) information about who is asking for permission to access your Adsense account (you are).
Create Application – Download Client Secret JSON File
Then click the download button to download the client_secret.sjon file. Once the JSON file is downloaded click create.
The JSON file downloads with longer name like “client_secret-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.json”. You will have to rename the JSON file so it is spelled “client_secret.json”.
Use Code and client_secret.json File to Authenticate
Put the python code above into .py file along with the client_secret.json file into a folder.
Local server (desktop/laptop)
Run the .py file using the command line which should do the following:
- Pop up browser and go to Google login page where and ask you to sign in and allow or allow if you already logged in.
- The command line will advance and finish.
- Creates a .dat file in your folder.
The .dat file name will be whatever you named your .py file. This dat file is your refresh token that will be used to authenticate access in future.
Copy your Python, client_secret and .dat (refresh token) files to server and run their by cron jobs.
You may want to change the Python code to select different Adsense metrics and dimensions. I am only selecting a handful of the API metrics and dimensions. You can check out these in more detail AdSense Management API Metrics and Dimensions documentation.