Lv110版本源码上传

This commit is contained in:
西瓜°
2024-11-04 15:00:40 +08:00
parent a6dbccdead
commit e029da72e4
6642 changed files with 131203 additions and 136087 deletions
View File
+84
View File
@@ -0,0 +1,84 @@
from __future__ import annotations
# Google Analytics 上报脚本
from urllib.parse import quote_plus
import requests
import platform
import uuid
from core.store import store
# note: 查看数据地址 https://analytics.google.com/analytics/web/#/
# note: 当发现上报失败时,可以将打印的post body复制到 https://ga-dev-tools.web.app/hit-builder/ 进行校验,看是否缺了参数,或者有参数不符合格式
# note: 参数文档 https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
GA_API_URL = "https://www.google-analytics.com/collect"
GA_TRACKING_ID = "UA-212229431-2"
headers = {
"user-agent": "dcalc",
}
def is_windows() -> bool:
return platform.system() == "Windows"
def get_cid():
return "{}-{}".format(platform.node(), uuid.getnode())
common_data = {
"v": "1", # API Version.
"tid": GA_TRACKING_ID, # Tracking ID / Property ID.
"cid": get_cid(), # Anonymous Client Identifier. Ideally, this should be a UUID that is associated with particular user, device, or browser instance.
"ua": "djc_helper",
"an": "dcalc",
"av": store.get("version"),
"ds": "dcalc",
"sr": "1920x1080",
}
GA_REPORT_TYPE_EVENT = "event"
GA_REPORT_TYPE_PAGE_VIEW = "page_view"
def track_event(category: str, action: str, label=None, value=0, ga_misc_params: dict | None = None):
try:
if ga_misc_params is None:
ga_misc_params = {}
data = {
**common_data,
"t": "event", # Event hit type.
"ec": category, # Event category.
"ea": action, # Event action.
"el": label, # Event label.
"ev": value, # Event value, must be an integer
**ga_misc_params, # 透传的一些额外参数
}
res = requests.post(GA_API_URL, data=data, headers=headers, timeout=10)
except Exception:
pass
def track_page(page: str, ga_misc_params: dict | None = None):
try:
if ga_misc_params is None:
ga_misc_params = {}
page = quote_plus(page)
data = {
**common_data,
"t": "pageview", # Event hit type.
"dh": "djc-helper.com", # Document hostname.
"dp": page, # Page.
"dt": "", # Title.
**ga_misc_params, # 透传的一些额外参数
}
res = requests.post(GA_API_URL, data=data, timeout=10)
except Exception:
pass
if __name__ == "__main__":
# track_event("example", "test")
track_page("/example/test_quote")
+62
View File
@@ -0,0 +1,62 @@
# Google Analytics 4 上报脚本
import platform
import uuid
import requests
# from log import logger
def get_cid():
return "{}-{}".format(platform.node(), uuid.getnode())
# note: 查看数据地址 https://analytics.google.com/analytics/web/#/
# note: 当发现上报失败时,可以将打印的post body复制到 https://ga-dev-tools.web.app/ga4/event-builder/ 进行校验,看是否缺了参数,或者有参数不符合格式
# note: 参数文档 https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=gtag#payload_query_parameters
GA_API_BASE_URL = "https://www.google-analytics.com/mp/collect"
# GA_API_BASE_URL = "https://www.google-analytics.com/debug/mp/collect"
GA_API_SECRET = "XMAI1cjPQbqz9zoONwudOA"
GA_MEASUREMENT_ID = "G-Y0WSE6LW2Z"
GA_API_URL = f"{GA_API_BASE_URL}?measurement_id={GA_MEASUREMENT_ID}&api_secret={GA_API_SECRET}"
headers = {
"user-agent": "dcalc",
}
def track_event(category: str, event_name: str):
try:
event_name = event_name.replace("/", "_")
json_data = {
"client_id": get_cid(),
"user_id": get_cid(),
"events": [
{
"name": category,
"params": {
"engagement_time_msec": "1000",
"session_id": get_cid(),
"event_name": event_name,
},
}
],
}
res = requests.post(GA_API_URL, json=json_data,
headers=headers, timeout=10)
# 打印日志,方便调试
# debug_msg = f"request info: body = {res.request.body!r}"
# logFunc = logger.debug
# if "debug" in GA_API_BASE_URL:
# debug_msg += f" res = {res.text}"
# logFunc = logger.warning
# logFunc(debug_msg)
except Exception as ex:
print(ex)
pass
if __name__ == "__main__":
track_event("test_category", "test_event/name_1")
track_event("test_category", "test_event_name_2")
+56
View File
@@ -0,0 +1,56 @@
import os
import sys
import threading
from typing import Any
import utils.analysis.ga4 as ga4
import utils.analysis.ga as ga
def async_call(cb, *args, **params):
threading.Thread(target=cb, args=args, kwargs=params, daemon=True).start()
def increase_counter(
name: Any = "",
report_to_google_analytics=True,
ga_type=ga.GA_REPORT_TYPE_EVENT,
ga_category="",
ga_misc_params=None,
):
name = str(name)
if name == "":
raise AssertionError("increase_counter name not set")
def _cb():
if report_to_google_analytics:
increase_counter_sync_google_analytics(name, ga_type, ga_category, ga_misc_params)
async_call(_cb)
def increase_counter_sync_google_analytics(name: str, ga_type: str, ga_category: str, ga_misc_params: dict):
try:
# 上报谷歌分析(v3和v4同时上报)
if ga_type == ga.GA_REPORT_TYPE_EVENT:
if ga_category == "":
# 如果ga_category为空,则尝试从name中解析,假设name中以/分隔的第一个部分作为ga_category
parts = name.split("/", 1)
if len(parts) == 2:
ga_category, name = parts
else:
ga_category = "counter"
ga.track_event(ga_category, name, ga_misc_params)
ga4.track_event(ga_category, name)
elif ga_type == ga.GA_REPORT_TYPE_PAGE_VIEW:
ga.track_page(name, ga_misc_params)
ga4.track_event("page_view", name)
else:
pass
except Exception as ex:
print(ex)
def test():
os.system("PAUSE")
if __name__ == "__main__":
test()