Google provides a wealth of data that can be incredibly valuable for businesses, developers, and researchers. With Python, one can efficiently pull data from various Google services like Google Analytics, Search Console, PageSpeed Insights, and more. In this article, we will explore some of the best practices and useful examples for fetching this data using Python.
Getting Started with Google APIs in Python
Before diving into the code, ensure you have the necessary libraries installed. You’ll need libraries such as google-auth
, google-api-python-client
, and requests
. You can install these using pip:
pip install google-auth google-api-python-client requests googleads pytrends
Fetching Data from Google Analytics
To retrieve data from Google Analytics, you first need to set up a service account in the Google Cloud Platform and grant it access to your Analytics view. Here is a code snippet to get you started:
from googleapiclient.discovery import build
from google.oauth2 import service_account
import json
def get_analytics_data(view_id):
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = 'path/to/service-account-file.json'
credentials = service_account.Credentials.from_service_account_file(
KEY_FILE_LOCATION, scopes=SCOPES)
analytics = build('analyticsreporting', 'v4', credentials=credentials)
response = analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': view_id,
'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:sessions'}]
}]
}
).execute()
return response
analytics_data = get_analytics_data('YOUR_VIEW_ID')
print(json.dumps(analytics_data, indent=2))
Response (JSON)
{
"reports": [
{
"columnHeader": {
"dimensions": [],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:sessions",
"type": "INTEGER"
}
]
}
},
"data": {
"rows": [
{
"metrics": [
{
"values": [
"1234" // Example number of sessions
]
}
]
}
],
"totals": [
{
"values": [
"1234" // Example total number of sessions
]
}
],
"rowCount": 1
}
}
]
}
Accessing Google Search Console Data
Google Search Console provides insights into your website’s search performance. Here’s how you can fetch this data:
from googleapiclient.discovery import build
from google.oauth2 import service_account
import json
def get_search_console_data(site_url):
SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']
KEY_FILE_LOCATION = 'path/to/service-account-file.json'
credentials = service_account.Credentials.from_service_account_file(
KEY_FILE_LOCATION, scopes=SCOPES)
webmasters = build('webmasters', 'v3', credentials=credentials)
response = webmasters.searchanalytics().query(
siteUrl=site_url,
body={
'startDate': '2022-01-01',
'endDate': '2022-01-31',
'dimensions': ['query']
}
).execute()
return response
search_console_data = get_search_console_data('https://www.example.com')
print(json.dumps(search_console_data, indent=2))
Response – Success (JSON)
{
"rows":[
{
"keys":[
"example query"
],
"clicks":123,
"impressions":456,
"ctr":0.27,
"position":5.3
},
"…"
]
}
Utilizing Google PageSpeed Insights
To analyze your website’s performance, you can use the Google PageSpeed Insights API. Below is an example of how to retrieve this data:
import requests
import json
def get_pagespeed_insights(url):
API_URL = f'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={url}'
response = requests.get(API_URL)
return response.json()
pagespeed_insights_data = get_pagespeed_insights('https://www.example.com')
print(json.dumps(pagespeed_insights_data, indent=2))
Response – JSON
{
"lighthouseResult": {
"categories": {
"performance": {
"score": 0.85
},
// other categories...
},
"audits": {
// individual audits...
},
// other lighthouse result data...
},
"loadingExperience": {
// loading experience data...
},
"originLoadingExperience": {
// origin loading experience data...
}
}
Fetching Place Details from Google Places API
The Google Places API allows you to get detailed information about a place. Here is a sample code to get place details:
import requests
import json
def get_place_details(place_id):
API_KEY = 'YOUR_API_KEY'
API_URL = f'https://maps.googleapis.com/maps/api/place/details/json?placeid={place_id}&key={API_KEY}'
response = requests.get(API_URL)
return response.json()
place_details = get_place_details('PLACE_ID')
print(json.dumps(place_details, indent=2))
Response – JSON
{
"html_attributions": [],
"result": {
"address_components": [
{
"long_name": "1600",
"short_name": "1600",
"types": ["street_number"]
},
{
"long_name": "Amphitheatre Parkway",
"short_name": "Amphitheatre Pkwy",
"types": ["route"]
},
{
"long_name": "Mountain View",
"short_name": "Mountain View",
"types": ["locality", "political"]
},
{
"long_name": "Santa Clara County",
"short_name": "Santa Clara County",
"types": ["administrative_area_level_2", "political"]
},
{
"long_name": "California",
"short_name": "CA",
"types": ["administrative_area_level_1", "political"]
},
{
"long_name": "United States",
"short_name": "US",
"types": ["country", "political"]
},
{
"long_name": "94043",
"short_name": "94043",
"types": ["postal_code"]
}
],
"formatted_address": "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry": {
"location": {
"lat": 37.4224764,
"lng": -122.0842499
},
"viewport": {
"northeast": {
"lat": 37.4238253802915,
"lng": -122.0829009197085
},
"southwest": {
"lat": 37.4211274197085,
"lng": -122.0855988802915
}
}
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/geocode-71.png",
"name": "1600 Amphitheatre Parkway",
"place_id": "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types": ["street_address"]
},
"status": "OK"
}
Geocoding with Google Maps API
To convert addresses into geographic coordinates, you can use the Geocoding API. Here’s how:
import requests
import json
def get_geocode(address):
API_KEY = 'YOUR_API_KEY'
API_URL = f'https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={API_KEY}'
response = requests.get(API_URL)
return response.json()
geocode_result = get_geocode('1600 Amphitheatre Parkway, Mountain View, CA')
print(json.dumps(geocode_result, indent=2))
Response – JSON
{
"results": [
{
"address_components": [
{
"long_name": "1600",
"short_name": "1600",
"types": ["street_number"]
},
{
"long_name": "Amphitheatre Parkway",
"short_name": "Amphitheatre Pkwy",
"types": ["route"]
},
{
"long_name": "Mountain View",
"short_name": "Mountain View",
"types": ["locality", "political"]
},
{
"long_name": "Santa Clara County",
"short_name": "Santa Clara County",
"types": ["administrative_area_level_2", "political"]
},
{
"long_name": "California",
"short_name": "CA",
"types": ["administrative_area_level_1", "political"]
},
{
"long_name": "United States",
"short_name": "US",
"types": ["country", "political"]
},
{
"long_name": "94043",
"short_name": "94043",
"types": ["postal_code"]
}
],
"formatted_address": "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry": {
"location": {
"lat": 37.4224764,
"lng": -122.0842499
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 37.4238253802915,
"lng": -122.0829009197085
},
"southwest": {
"lat": 37.4211274197085,
"lng": -122.0855988802915
}
}
},
"place_id": "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"plus_code": {
"compound_code": "CWC8+W5 Mountain View, California, United States",
"global_code": "849VCWC8+W5"
},
"types": ["street_address"]
}
],
"status": "OK"
}
Extracting Data from Google Ads
Google Ads API enables marketers to retrieve insights about their ad campaigns. Here’s how you can get started with fetching Google Ads data:
from googleads import adwords
def get_ads_data(client_customer_id):
adwords_client = adwords.AdWordsClient.LoadFromStorage('path/to/googleads.yaml')
report_downloader = adwords_client.GetReportDownloader(version='v201809')
report_query = (adwords.ReportQueryBuilder()
.Select('CampaignId', 'Impressions', 'Clicks', 'Cost')
.From('CAMPAIGN_PERFORMANCE_REPORT')
.Where('CampaignStatus').In('ENABLED', 'PAUSED')
.During('LAST_7_DAYS')
.Build())
report = report_downloader.DownloadReportWithAwql(report_query, 'CSV', skip_report_header=True, skip_column_header=False, skip_report_summary=True)
with open('google_ads_report.csv', 'w') as report_file:
report_file.write(report)
print("Report downloaded to 'google_ads_report.csv'")
get_ads_data('YOUR_CLIENT_CUSTOMER_ID')
Response CSV
"CampaignId","Impressions","Clicks","Cost"
"123456789","1000","50","250000"
...
Analyzing Trends with Google Trends API
Google Trends can provide valuable insights into what people are searching for online. Here’s a way to programmatically fetch Google Trends data using the pytrends
library:
pip install pytrends
from pytrends.request import TrendReq
import json
def get_trends_data(keyword):
pytrends = TrendReq(hl='en-US', tz=360)
pytrends.build_payload([keyword], cat=0, timeframe='now 7-d', geo='', gprop='')
trends_data = pytrends.interest_over_time()
return trends_data.to_json()
trends_data = get_trends_data('python programming')
print(json.dumps(json.loads(trends_data), indent=2))
Response – JSON
{
"date": {
"2023-06-15T00:00:00Z": 70,
"2023-06-16T00:00:00Z": 74,
"2023-06-17T00:00:00Z": 68,
...
},
"python programming": {
"2023-06-15T00:00:00Z": 70,
"2023-06-16T00:00:00Z": 74,
"2023-06-17T00:00:00Z": 68,
...
}
}
Accessing Google Sheets API
from googleapiclient.discovery import build
from google.oauth2 import service_account
import json
def get_sheets_data(spreadsheet_id, range_name):
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
KEY_FILE_LOCATION = 'path/to/service-account-file.json'
credentials = service_account.Credentials.from_service_account_file(
KEY_FILE_LOCATION, scopes=SCOPES)
service = build('sheets', 'v4', credentials=credentials)
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=spreadsheet_id,
range=range_name).execute()
values = result.get('values', [])
return values
sheets_data = get_sheets_data('YOUR_SPREADSHEET_ID', 'Sheet1!A1:D10')
print(json.dumps(sheets_data, indent=2))
Using Google Calendar API
from googleapiclient.discovery import build
from google.oauth2 import service_account
import json
def list_upcoming_events(calendar_id='primary'):
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
KEY_FILE_LOCATION = 'path/to/service-account-file.json'
credentials = service_account.Credentials.from_service_account_file(
KEY_FILE_LOCATION, scopes=SCOPES)
service = build('calendar', 'v3', credentials=credentials)
now = '2022-07-01T00:00:00Z'
events_result = service.events().list(calendarId=calendar_id, timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
return events
upcoming_events = list_upcoming_events()
print(json.dumps(upcoming_events, indent=2))
Accessing Google Sheets API
from googleapiclient.discovery import build
from google.oauth2 import service_account
import json
def get_sheets_data(spreadsheet_id, range_name):
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
KEY_FILE_LOCATION = 'path/to/service-account-file.json'
credentials = service_account.Credentials.from_service_account_file(
KEY_FILE_LOCATION, scopes=SCOPES)
service = build('sheets', 'v4', credentials=credentials)
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=spreadsheet_id,
range=range_name).execute()
values = result.get('values', [])
return values
sheets_data = get_sheets_data('YOUR_SPREADSHEET_ID', 'Sheet1!A1:D10')
print(json.dumps(sheets_data, indent=2))
Response (JSON)
[
["Name", "Age", "City", "Occupation"],
["Alice", "30", "New York", "Engineer"],
["Bob", "25", "San Francisco", "Designer"],
["Carol", "28", "Los Angeles", "Manager"],
["Dave", "35", "Chicago", "Developer"]
]
Using Google Calendar API
from googleapiclient.discovery import build
from google.oauth2 import service_account
import json
def list_upcoming_events(calendar_id='primary'):
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
KEY_FILE_LOCATION = 'path/to/service-account-file.json'
credentials = service_account.Credentials.from_service_account_file(
KEY_FILE_LOCATION, scopes=SCOPES)
service = build('calendar', 'v3', credentials=credentials)
now = '2022-07-01T00:00:00Z'
events_result = service.events().list(calendarId=calendar_id, timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
return events
upcoming_events = list_upcoming_events()
print(json.dumps(upcoming_events, indent=2))
JSON (Response)
[
{
"kind": "calendar#event",
"etag": "\"3181159875584000\"",
"id": "abcdef1234567890",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=abcdef1234567890",
"created": "2022-06-30T12:00:00Z",
"updated": "2022-06-30T12:00:00Z",
"summary": "Meeting with Team",
"description": "Discuss project updates and timelines.",
"location": "Office",
"creator": {
"email": "[email protected]",
"displayName": "Organizer Name"
},
"organizer": {
"email": "[email protected]",
"displayName": "Organizer Name"
},
"start": {
"dateTime": "2022-07-01T10:00:00Z"
},
"end": {
"dateTime": "2022-07-01T11:00:00Z"
},
"iCalUID": "[email protected]",
"sequence": 0,
"reminders": {
"useDefault": true
}
},
{
"kind": "calendar#event",
"etag": "\"3181160123456000\"",
"id": "ghijkl9876543210",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=ghijkl9876543210",
"created": "2022-06-30T12:30:00Z",
"updated": "2022-06-30T12:30:00Z",
"summary": "Lunch with Client",
"description": "Discuss the new contract.",
"location": "Restaurant",
"creator": {
"email": "[email protected]",
"displayName": "Organizer Name"
},
"organizer": {
"email": "[email protected]",
"displayName": "Organizer Name"
},
"start": {
"dateTime": "2022-07-01T12:00:00Z"
},
"end": {
"dateTime": "2022-07-01T13:00:00Z"
},
"iCalUID": "[email protected]",
"sequence": 0,
"reminders": {
"useDefault": true
}
}
]
Accessing Google Drive API
from googleapiclient.discovery import build
from google.oauth2 import service_account
import json
def list_drive_files():
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
KEY_FILE_LOCATION = 'path/to/service-account-file.json'
credentials = service_account.Credentials.from_service_account_file(
KEY_FILE_LOCATION, scopes=SCOPES)
service = build('drive', 'v3', credentials=credentials)
results = service.files().list(pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
return items
drive_files = list_drive_files()
print(json.dumps(drive_files, indent=2))
Reponse (JSON)
[
{
"id": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms",
"name": "Sample Document 1"
},
{
"id": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upmt",
"name": "Sample Spreadsheet"
},
{
"id": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upmu",
"name": "Sample Presentation"
},
{
"id": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upmv",
"name": "Image File"
},
{
"id": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upmw",
"name": "Sample PDF"
},
{
"id": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upmx",
"name": "Sample Code File"
},
{
"id": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upmy",
"name": "Sample Form"
},
{
"id": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upmz",
"name": "Sample Drawing"
},
{
"id": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upn0",
"name": "Sample Audio"
},
{
"id": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upn1",
"name": "Sample Video"
}
]
These code snippets demonstrate the ease of accessing various Google services using Python. By leveraging these APIs, developers can integrate powerful data insights directly into their applications.