Package: pyga4
English version|Chinese version README.mdDocuments|PypiOverviewOverviewIntroductionHow to Stream GA4 Data to Bigquery in Real-time?FeaturesHow to Use?Download the PackageConnect to Your BigqueryConnect to GA4 TablesEvaluate Query Cost with dry runAnalyze User PropertiesAnalyze Device PropertiesAnalyze EventsIntroductionpyGA4is a Python toolkit designed forextracting, processing, and analyzingdata fromGoogle Analytics 4 (GA4).Whether you're a digital marketing professional, a data analyst, or anyone interested in gaining insights from GA4 data, this package simplifies the process of working with your GA4 data.How to Stream GA4 Data to Bigquery in Real-time?First, we assume that everyone has already integrated GA4 data into their respective platform websites (there are many online tutorials).Next, we will use a free third-party service to stream data into Bigquery. For detailed instructions, please refer to theofficial documentation.If successful, you will see tables in Bigquery similar to the following (analytics_xxxx),ref:FeaturesQuery Cost Estimation: Provides the Bigquerydry runfeature to estimate query cost before execution.Data Extraction: Easily connect to your GA4 property, retrieve data, and save it for analysis.Data Preprocessing: Prepare and clean your GA4 data for analysis with built-in data preprocessing functions.Custom Queries: Execute custom queries to filter and aggregate data based on your specific needs.Data Analysis: Perform various types of analysis, including user behavior analysis, conversion tracking, and more.Data Visualization: Create informative visualizations and reports to communicate your findings effectively.Simple Integration: Seamlessly integratepyGA4into your data pipeline or analytics workflow.How to Use?For more features, please refer to thepackage documentation.Download the Packagepip install pyga4Connect to Your Bigqueryfromgoogle.cloudimportbigqueryclient=bigquery.Client()# Or you can use:# client = bigquery.Client.from_service_account_json(#    './private/service-project-data-dev-01d11c742ba1.json'# )Connect to GA4 Tablesfrompyga4.modelimportGa4Table# Use your project_id, dataset_name (analytics_xxxx)ga4_table=Ga4Table(client,PROJECT_ID,DATASET_NAME)# Show the tables list in dataset, e.g., analytics_date1, analytics_date2table_id_list=ga4_tableall_tables_listprint(table_id_list)# Select the table you want to analyzega4_table.table_id='events_intraday_20200812'Evaluate Query Cost with dry run# Query with dry run:ga4_table.query_config.dry_run=Truequery=f"""SELECT event_timestamp FROM `<project_id>.<dataset_name>.<data_table>`"""results=ga4_table.query(query)# return None, but you can see the query usage!Analyze User PropertiesQuery User ID and Country List# User attributeuser_id_list=ga4_table.user_id_listuser_country_list=ga4_table.geo_country_listQuery User ID and Country Distributionfrompyga4.analyticimportUserAnalytic# UserAnalyticuser_analytic=UserAnalytic(ga4_table)countries_dist=user_analytic.countries_distributionuserid_dist=user_analytic.user_id_distributionAnalyze Device Properties# DeviceAnalyticdevice_analytic=DeviceAnalytic(ga4_table)mobile_brand_dist=device_analytic.mobile_brand_distributionAnalyze Events# EventAnalyticevent_analytic=EventAnalytic(ga4_table)page_loc_dist=event_analytic.pages_distributionBack to top