コード例 #1
0
def get_goals(client_id: str, client_secret: str, refresh_token: str, view_name: str) -> dict:
    ga_ids = get_ga_ids(client_id, client_secret, refresh_token, view_name)
    access_token = get_access_token(client_id, client_secret, refresh_token)
    endpoint = 'https://www.googleapis.com/analytics/v3/management/accounts/{}/webproperties/{}/profiles/{}/goals'.format(
        ga_ids['account_id'], ga_ids['property_id'], ga_ids['view_id'])

    return requests.get(endpoint, headers=headers).json()
コード例 #2
0
def get_data(client_id: str, client_secret: str, refresh_token: str, view_name: str, data_date: str, dimensions: list, metrics=list) -> dict:
    data = []
    view_id = get_ga_ids(client_id, client_secret,
                         refresh_token, view_name)['view_id']
    access_token = get_access_token(client_id, client_secret, refresh_token)
    endpoint = 'https://www.googleapis.com/analytics/v3/data/ga'

    headers = {
        'Authorization': access_token
    }

    params = {
        'ids': 'ga:{}'.format(view_id),
        'start-date': data_date,
        'end-date': data_date,
        'samplingLevel': 'HIGHER_PRECISION',
        'include-empty-rows': 'false',
        'metrics': ','.join(metrics),
        'dimensions': ','.join(dimensions)
    }

    response = requests.get(endpoint, params=params, headers=headers).json()

    for i in range(math.ceil(response['totalResults']/1000)):
        params.setdefault('start-index', (i*1000)+1)
        data.append(requests.get(
            endpoint, params=params, headers=headers).json())
コード例 #3
0
def get_videos_list(client_id: str, client_secret: str, refresh_token: str,
                    channel_id: str, start_date: str, end_date: str) -> list:
    data = list()
    access_token = get_access_token(client_id, client_secret, refresh_token)
    endpoint = 'https://www.googleapis.com/youtube/v3/search/'
    params = {
        'channelId': channel_id,
        'maxResults': 50,
        'type': 'video',
        'part': 'id',
        'order': 'date',
        'publishedAfter': '{}T00:00:00Z'.format(start_date),
        'publishedBefore': '{}T00:00:00Z'.format(end_date)
    }
    headers = {'Authorization': 'Bearer {}'.format(access_token)}

    response = requests.get(endpoint, headers=headers, params=params).json()

    while endpoint is not None:
        if response.get('items'):
            data.extend(
                [video['id']['videoId'] for video in response['items']])
        else:
            endpoint = None
        if response.get('nextPageToken'):
            endpoint = response['nextPageToken']
            response = requests.get(endpoint).json()
        else:
            endpoint = None
    return data
コード例 #4
0
def get_lifetime_channel_community(client_id: str, client_secret: str,
                                   refresh_token: str, channel_id: str) -> int:
    access_token = get_access_token(client_id, client_secret, refresh_token)
    headers = {'Authorization': 'Bearer {}'.format(access_token)}
    endpoint = 'https://www.googleapis.com/youtube/v3/channels'
    params = {'id': channel_id, 'part': 'statistics'}
    response = requests.get(endpoint, headers=headers, params=params).json()
    return response['items'][0]['statistics']['subscriberCount']
コード例 #5
0
def get_daily_channel_demographics(client_id: str, client_secret: str,
                                   refresh_token: str, channel_id: str,
                                   data_date: str) -> list:
    data = list()
    access_token = get_access_token(client_id, client_secret, refresh_token)

    headers = {'Authorization': 'Bearer {}'.format(access_token)}
    params = {
        'dimensions': 'ageGroup,gender',
        'startDate': data_date,
        'endDate': data_date,
        'ids': 'channel=={}'.format(channel_id),
        'metrics': 'viewerPercentage'
    }

    response = requests.get(endpoint, headers=headers, params=params).json()
    return response['rows']
コード例 #6
0
def get_ga_ids(client_id: str, client_secret: str, refresh_token: str, view_name: str) -> int:
    access_token = get_access_token(client_id, client_secret, refresh_token)
    endpoint = 'https://www.googleapis.com/analytics/v3/management/accountSummaries'
    headers = {
        'Authorization': access_token
    }
    response = requests.get(endpoint, headers=headers).json()
    for account in accounts_summary['items']:
        for web_property in account['webProperties']:
            for view in web_property['profiles']:
                if view['name'] == view_name:
                    return {
                        'account_id': account['id'],
                        'property_id': web_property['id'],
                        'view_id': view['id']
                    }
                else:
                    return None
コード例 #7
0
def get_daily_channel_insights(client_id: str, client_secret: str,
                               refresh_token: str, channel_id: str,
                               start_date: str, end_date: str) -> list:
    data = list()
    access_token = get_access_token(client_id, client_secret, refresh_token)
    metrics = get_available_channel_metrics(access_token, channel_id)

    headers = {'Authorization': 'Bearer {}'.format(access_token)}
    endpoint = "https://youtubeanalytics.googleapis.com/v2/reports"
    params = {
        'startDate': start_date,
        'endDate': end_date,
        'ids': 'channel=={}'.format(channel_id),
        'metrics': metrics
    }

    response = requests.get(endpoint, headers=headers, params=params).json()
    return response['rows']
コード例 #8
0
def get_daily_video_trafficinsights(client_id: str, client_secret: str,
                                    refresh_token: str, channel_id: str,
                                    data_date: str, video_id: str) -> list:
    data = list()
    access_token = get_access_token(client_id, client_secret, refresh_token)
    metrics = get_available_channel_metrics(access_token, channel_id)

    headers = {'Authorization': 'Bearer {}'.format(access_token)}
    endpoint = "https://youtubeanalytics.googleapis.com/v2/reports"
    params = {
        'dimensions': 'video,insightTrafficSourceType',
        'startDate': data_date,
        'endDate': data_date,
        'ids': 'channel=={}'.format(channel_id),
        'metrics': 'views,estimatedMinutesWatched',
        'filters': 'video=={}'.format(video_id)
    }

    response = requests.get(endpoint, headers=headers, params=params).json()
    return response['rows']