How can I use the YouTube Analytics API to get revenue from shorts video based on creatorContentType?

373 Views Asked by At

I have monetized videos on my YouTube channel. I use the below YouTube analytics API to get the revenue information from all my videos, including YouTube shorts, but how can I get the revenue information from the YouTube shorts separately?

https://youtubeanalytics.googleapis.com/v2/reports?ids=channel==XXX&startDate=aaa&endDate=bbb&metrics=estimatedRevenue&access_token=xyz

The below API is used to get the views from YouTube videos and shorts using the dimension creatorContentType:

https://youtubeanalytics.googleapis.com/v2/reports?ids=channel==XXX&dimensions=day,creatorContentType&startDate=aaa&endDate=bbb&metrics=views&access_token=xyz

1

There are 1 best solutions below

4
Rajan Thakur On

To retrieve revenue data for Shorts videos based on creator content type using the YouTube Analytics API, follow these steps:

  1. Set up your project and enable the API.
  2. Obtain API credentials (OAuth 2.0 client ID) and download the client secret JSON file.
  3. Authenticate your application and obtain an access token.
  4. Make API requests using the access token to retrieve revenue data.
  5. Get the channel ID, content owner ID, and query revenue data using the Reports.query method.
  6. 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.