-2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kiran
  • 61
  • 1
  • 7

1 Answers1

-2

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.

  • Hey Ranjan, Do we have dimension 'shorts' in YT Analytics API? Thanks in advance. – Kiran Jul 10 '23 at 14:02
  • 1
    Welcome to Stack Overflow, Rajan Thakur! Both of your recent answers appear likely to be entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting AI-generated content is not allowed here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. We do hope you'll stick around and continue to be a valuable part of our community by posting *your own* quality content. Thanks! – NotTheDr01ds Jul 23 '23 at 02:01
  • 1
    **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. – NotTheDr01ds Jul 23 '23 at 02:01
  • 1
    @NotTheDr01ds I can see errors and misinformation in Ranjan Thakur answer – Kiran Jul 25 '23 at 06:38