To retrieve revenue data for Shorts videos based on creator content type using the YouTube Analytics API, follow these steps:
- Set up your project and enable the API.
- Obtain API credentials (OAuth 2.0 client ID) and download the client secret JSON file.
- Authenticate your application and obtain an access token.
- Make API requests using the access token to retrieve revenue data.
- Get the channel ID, content owner ID, and query revenue data using the Reports.query method.
- Filter results by "video" and "shorts" and set the "metrics" parameter to "estimatedRevenue."
from googleapiclient.discovery import build
from google.oauth2 import service_account
# Authenticate with the API using your credentials
credentials = service_account.Credentials.from_service_account_file(
'path/to/client_secret.json',
scopes=['https://www.googleapis.com/auth/youtube.readonly']
)
# Build the YouTube Analytics API service
youtube_analytics = build('youtubeAnalytics', 'v2', credentials=credentials)
# Get the channel ID
channels_response = youtube_analytics.channels().list(mine=True, part='id').execute()
channel_id = channels_response['items'][0]['id']
# Get the content owner ID
content_owners_response = youtube_analytics.contentOwners().list(part='id', onBehalfOfContentOwner=channel_id).execute()
content_owner_id = content_owners_response['items'][0]['id']
# Query revenue data for Shorts videos
analytics_response = youtube_analytics.reports().query(
ids=f'contentOwner=={content_owner_id}',
startDate='yyyy-mm-dd',
endDate='yyyy-mm-dd',
dimensions='video,shorts',
metrics='estimatedRevenue'
).execute()
# Process the response data
for row in analytics_response['rows']:
video_id = row[0]
shorts_revenue = row[1]
# Process the revenue data as per your requirements
Replace 'path/to/client_secret.json' with actual path, set start and end dates, and authorize YouTube Analytics and Content ID API access for revenue data retrieval.