简体   繁体   English

Python 云 Shell 测量协议 Google Analytic Universal

[英]Python Cloud Shell Measurement Protocol Google Analytic Universal

I'm trying to write python to run in cloud shell as a POC in Google cloud.我正在尝试编写 python 以在云 shell 中作为 Google 云中的 POC 运行。

The objective is to use big query to get the GA client id of some users & for each row in the output send a measurement protocol event to universal analytics.目标是使用大查询获取某些用户的 GA 客户端 ID,并针对 output 中的每一行向通用分析发送一个测量协议事件。

My python skills are somewhat limited and all of our dev team work in python in the cloud.我的 python 技能有些有限,我们所有的开发团队都在云中的 python 工作。 So I'm stuck with it.所以我坚持下去。

The code errors on line 41 when trying to send the request, but I can work out why & pythons error code are not particularly helpful.尝试发送请求时第 41 行的代码错误,但我可以弄清楚为什么 & pythons 错误代码不是特别有用。

在此处输入图像描述

from google.cloud import bigquery
from xmlrpc.client import ServerProxy


# Set up your Google Analytics tracking ID
TRACKING_ID = 'UA-XXXXXXXX-1'

# Set up the BigQuery client
client = bigquery.Client()

# Set up the query to get the data from the BigQuery table
query = """
SELECT
  '2042839989.1670364010' as client_id,  
  'test' as event_category,
  'test' as event_action,
  'test' as event_label,
  0 as event_value
"""

# Run the query and get the results
results = client.query(query).result()

print(results)

# Send the event data to Google Analytics for each row in the result
for row in results:
  data = {
    'v': '1',  # Protocol version
    'tid': TRACKING_ID,  # Tracking ID
    'cid': row['client_id'],  # Anonymous client ID
    't': 'event',  # Event hit type
    'ec': row['event_category'],  # Event category
    'ea': row['event_action'],  # Event action
    'el': row['event_label'],  # Event label
    'ev': row['event_value'],  # Event value
  }

  print(data)

  proxy = ServerProxy('https://www.google-analytics.com/collect')

  response = proxy.call(data)
  
  print(response)

Did some reading & testing of other packages send measurement protocol.是否对其他包进行了一些阅读和测试并发送了测量协议。

Founds request package and factored that into the code发现请求 package 并将其分解到代码中

from google.cloud import bigquery
import requests

# Set up your Google Analytics tracking ID
TRACKING_ID = 'UA-XXXXXXXX-1'

# Set up the BigQuery client
client = bigquery.Client()

# Set up the query to get the data from the BigQuery table
query = """
SELECT
  '2042839989.1670364010' as client_id,  
  'audience' as event_category,
  'test' as event_action,
  'test' as event_label,
  0 as event_value
"""

# Run the query and get the results
results = client.query(query).result()

print(results)

# Send the event data to Google Analytics for each row in the result
for row in results:
  payload = {
    'v': '1',  # Protocol version
    'tid': TRACKING_ID,  # Tracking ID
    'cid': row['client_id'],  # Anonymous client ID
    'uip': '92.232.199.9', # Hardcode Manual IP address
    'ua': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36', # Hardcode User Agent
    't': 'event',  # Event hit type
    'ec': row['event_category'],  # Event category
    'ea': row['event_action'],  # Event action
    'el': row['event_label'],  # Event label
    'ev': row['event_value'],  # Event value
  }

  print(payload)

  response = requests.get('https://www.google-analytics.com/collect', params=payload)
  
  print(response)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM