How can i get the top youtube search data from youtube analytics api?

16 Views Asked by At

I want to get the top youtube search terms for a specific video from traffic source but unable to get it through youtube analytics api? Here is my code:

import csv
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow

from tabulate import tabulate

SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly']

API_SERVICE_NAME = 'youtubeAnalytics'
API_VERSION = 'v2'
CLIENT_SECRETS_FILE = 'yt_credentials.json'

def get_service():
  flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
#   credentials = flow.run_console()
  # alternatively (https://github.com/onlyphantom/youtube_api_python/pull/3/files):
  credentials = flow.run_local_server()
  return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)

def execute_api_request(client_library_function, **kwargs):
  response = client_library_function(
    **kwargs
  ).execute()
  return response

if __name__ == '__main__':
    video_id = "txnSq7wlxjU"
    youtubeAnalytics = get_service()
    result = execute_api_request(
        youtubeAnalytics.reports().query,
        ids='channel==MINE',
        filters=f'video=={video_id}, ',
        startDate='2023-02-20',
        endDate='2023-03-30',
        metrics='views, yt_search',
        dimensions='insightTrafficSourceDetail',
    )
    
    print(result)

I wanted to get the top youtube search terms for specific video and sort them based on views.

0

There are 0 best solutions below