从 GA4 中提取数据到 AWS Jupyter Notebook

问题描述 投票:0回答:0

我正在尝试将数据从 GA4 提取到我的 AWS Jupyter 笔记本中以进行长期存储。我想点击所有维度/指标,但谷歌分析将查询限制为 10 个指标和 7 个维度。我唯一能想到的解决方案是使用 for 循环,肯定有比我在下面完成的代码更好的方法吗?

# Authenticate with Google Analytics API

credentials = Credentials.from_service_account_file('Path-To-Key')
client = BetaAnalyticsDataClient(credentials=credentials)

# Replace with the path to your JSON credentials file

KEY_FILE_LOCATION = 'Path-To-Key'

# Replace with the Analytics Admin API version you want to use

API_VERSION = 'v1alpha'

# Replace with the Analytics Admin API scope(s) you want to use

SCOPES = \['https://www.googleapis.com/auth/analytics.edit'\]

# Create credentials from the JSON file

credentials_2 = service_account.Credentials.from_service_account_file(KEY_FILE_LOCATION, scopes=SCOPES)

# Create an instance of the Analytics Admin API client

analytics_admin = build('analyticsadmin', API_VERSION, credentials=credentials_2)

# Call the accounts().list() method to retrieve a list of accounts

accounts = analytics_admin.accounts().list().execute()

# Accumulator for all property IDs

all_property_ids = \[\]

# Function to extract property id from properties

def extract_property_ids(data):
property_ids = \[\]
for item in data:
property_name = item\['name'\]
property_number = property_name.split('/')\[1\]
property_id = f"properties/{property_number}"
property_ids.append(property_id)
return property_ids

# Loop through each account and retrieve its properties/property ids

for account in accounts.get('accounts', \[\]):
properties = analytics_admin.properties().list(filter=f"parent:{account\['name'\]}").execute()
property_ids = extract_property_ids(properties.get('properties', \[\]))
all_property_ids.extend(property_ids)

# Define the date range and dimensions/metrics to retrieve

date_range = DateRange(start_date='7daysAgo', end_date='today')
dimensions = \[Dimension(name='country')\]
metrics = \[Metric(name='screenPageViews')\]

for property_id in all_property_ids:

    request = RunReportRequest(
        property=property_id,
        dimensions=dimensions,
        metrics=metrics,
        date_ranges=[date_range],
    )
    
    response = client.run_report(request)
    
    # Convert the response object to a dictionary
    data_dict = MessageToDict(response._pb)
    
    # Convert the dictionary to a JSON string
    data_json = json.dumps(data_dict)
    
    # Save the data to a file on the local disk with name being property id associated with it
    filename = f'GA4 Test Data/Data_{property_id}.json'
    with open(filename, 'w') as f:
        f.write(data_json)
google-analytics-api amazon-sagemaker google-analytics-4
© www.soinside.com 2019 - 2024. All rights reserved.