How to send user scoped events from Big Query to GA4 using Measurement Protocol?

58 views Asked by At

I have propensity audience names and predicted date for each user_psuedo_id in a BQ table and want to send the values to GA4 using Measurement Protocol.

The BQ table looks like below:

user_pseudo_id pred_date audience_name
abc 2023-10-10 clusterA

The following is the python script I am running:

from urllib.parse import urlencode
import http.client
from datetime import datetime
from google.oauth2 import service_account
import pandas as pd
import threading
import json
import requests


def create_payload(item_row):
    measurement_id = 'measurement_id'
    api_secret = 'api_secret'

    payload = {
            "client_id":item_row['user_pseudo_id'],
            "non_personalized_ads":False,
            "user_properties":
                {
                    "audience_name":
                        {"value":item_row['audience_name']},
                    "pred_date":
                        {"value":str(item_row['pred_date'])}
                },
            "events":[
                {
                    "name":"lead_propensity_bucket"
                }
            ]
        }
    payload_url = json.dumps(payload)
    return payload_url


def push_data_to_ga4(payload):
    print('Push data to GA4')
    headers = {
        'cache-control': 'no-cache',
        'Content-Type': 'application/json'
    }
    
    url="https://www.google-analytics.com/mp/collect?api_secret=api_secret&measurement_id=measurement_id"
    res = requests.post(url, json=payload, headers=headers)
    code = res.status_code
    if code != 200:
        print(code)


def start_process(event, context):
    project_id = 'project-id'
    dataset_id = 'lpml_model_dataset'
    table_id = 'latest_prediction'

    print('Get BQ data')
    df = pd.read_gbq(f'SELECT * FROM {dataset_id}.{table_id}',
                     project_id=project_id)
    df_len = len(df)

    print('create payload')
    payload = ''
    for index, row in df.iterrows():
        res = create_payload(row)
        payload = "\n".join([payload, res])

        if index !=0 and index % 25 == 0:
            # Create async task
            thread = threading.Thread(target=push_data_to_ga4, args=(payload,))
            thread.start()
            # reset the payload
            payload = ''
        if index == (df_len - 1):
            thread = threading.Thread(target=push_data_to_ga4, args=(payload,))
            thread.start()

    print("Shooting to GA4 is complete")

when I deploy this, the log says that the data has been pushed successfully to GA4. But when I check the UI, that is not case. nothing comes up in the UI - not in the reports, explores, etc. The last push I made was on Monday around 1613 CET. It still has not showed up.

How can I fix this?

0

There are 0 answers