[Python] Library-code library mismaches for google data api GA4

Discussão em 'Python' iniciado por Stack, Janeiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    Because Google is still changing their api's for ga4, I get mismatches in function names and parameter lists. I tried api v.11 that chatGPT uses, I also tries the latest version .16. Right now, I get a runtime error " File "d:\workspaceGA\ga4export\ga4base.py", line 49, in sample_run_report one_of=DimensionValue.OneOf( AttributeError: type object 'DimensionValue' has no attribute 'OneOf'" Any ideas? It becomes a shell game, I would fix one problem like this, then something else errors out elsewhere, a function, class or variable name errors out.

    import sys
    sys.path.append('./')
    from globals import *
    from datetime import datetime, timedelta

    SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
    KEY_FILE_LOCATION = GLOBAL_KEY_FILE_LOCATION
    MEASUREMENT_ID = GLOBAL_MEASUREMENT_ID

    from google.oauth2 import service_account
    from google.analytics.data_v1beta import BetaAnalyticsDataClient
    from google.analytics.data_v1beta.types import (
    DateRange,
    Dimension,
    Metric,
    RunReportRequest,
    CohortSpec,
    Cohort,
    DimensionValue,
    CohortsRange,
    CohortReportSettings,
    )


    def sample_run_report(start_date_day, end_date):
    credentials = service_account.Credentials.from_service_account_file(KEY_FILE_LOCATION, scopes=SCOPES)
    client = BetaAnalyticsDataClient(credentials=credentials)

    start_date = datetime.strptime(start_date_day, '%Y-%m-%d')
    end_date = datetime.strptime(end_date, '%Y-%m-%d')

    while start_date <= end_date:
    # set the end_date for the current day
    current_end_date = (start_date + timedelta(days=1)).strftime('%Y-%m-%d')

    request = RunReportRequest(
    property=f"properties/{MEASUREMENT_ID}",
    dimensions=[
    Dimension(name="customEvent:n20_page_url"),
    Dimension(name="customDimension:n20_nick_name")
    ],
    metrics=[Metric(name="eventCount")],
    date_ranges=[DateRange(start_date=start_date.strftime('%Y-%m-%d'), end_date=current_end_date)],
    cohort_spec=CohortSpec(
    cohorts=[
    Cohort(
    dimension=DimensionValue(
    value="",
    one_of=DimensionValue.OneOf(
    value=DimensionValue.StringValue(
    value="customDimension:n20_nick_name"
    )
    )
    ),
    name="",
    )
    ],
    cohorts_range=CohortsRange(start_offset=0, end_offset=0),
    cohort_report_settings=CohortReportSettings(),
    )
    )

    response = client.run_report(request)

    print(f"Report result for {start_date.strftime('%Y-%m-%d')}:")
    for row in response.rows:
    print(row.dimension_values[0].value, row.dimension_values[1].value, row.metric_values[0].value)

    # set the start_date for the next day
    start_date += timedelta(days=1)


    def main():
    start_date_day = '2023-01-01'
    end_date = '2023-03-10'
    sample_run_report(start_date_day, end_date)


    if __name__ == '__main__':
    main()

    Continue reading...

Compartilhe esta Página