[#] 升级为GA4

This commit is contained in:
huiyadanli 2023-04-09 13:42:29 +08:00
parent 6b63398222
commit 3407aff012

View File

@ -4,16 +4,19 @@ using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms; using System.Windows.Forms;
namespace RevokeMsgPatcher.Utils namespace RevokeMsgPatcher.Utils
{ {
/// <summary> /// <summary>
/// 用于软件的 Google Analytics 实现 By huiyadanli /// 用于软件的 Google Analytics 实现 By huiyadanli
/// 20230409 更新 GA4 的实现
/// 相关文档: /// 相关文档:
/// 指南 https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide /// #GA指南(过时) https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
/// 参数 https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters /// #GA参数(过时) https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
/// 测试 https://ga-dev-tools.appspot.com/hit-builder/ /// GA4教程 https://firebase.google.com/codelabs/firebase_mp
/// 测试 https://ga-dev-tools.google/ga4/event-builder/
/// </summary> /// </summary>
public class GAHelper public class GAHelper
{ {
@ -38,15 +41,10 @@ namespace RevokeMsgPatcher.Utils
// 根据实际情况修改 // 根据实际情况修改
private static readonly HttpClient client = HttpUtil.Client; private static readonly HttpClient client = HttpUtil.Client;
private const string GAUrl = "https://www.google-analytics.com/collect"; private const string GAUrl = "https://www.google-analytics.com/mp/collect?api_secret=urKlcc29TSy3OIkHr8yFSQ&measurement_id=G-BE6FRPZS1W";
// 根据实际使用分析账号设置
private const string tid = "UA-80358493-2"; // GA Tracking ID / Property ID.
private static readonly string cid = Device.Value(); // Anonymous Client ID. // Guid.NewGuid().ToString() private static readonly string cid = Device.Value(); // Anonymous Client ID. // Guid.NewGuid().ToString()
// 屏幕分辨率(可选)
private static readonly string sr = Screen.PrimaryScreen.Bounds.Width + "x" + Screen.PrimaryScreen.Bounds.Height;
public string UserAgent { get; set; } public string UserAgent { get; set; }
@ -59,29 +57,37 @@ namespace RevokeMsgPatcher.Utils
{ {
try try
{ {
if (!page.StartsWith("/")) if (page.StartsWith("/"))
{ {
page = "/" + page; page = page.Remove(0, 1);
} }
page = page.Replace("/", "_").Replace(".", "_");
// 请求参数 // 请求参数
var values = new Dictionary<string, string> var values = new Dictionary<string, object>
{ {
{ "v", "1" }, // 当前必填1 { "client_id",UserAgent},
{ "tid", tid }, { "user_id", cid },
{ "cid", cid }, { "non_personalized_ads", "false" },
{ "ua", UserAgent }, { "events", new List<Dictionary<string, string>>()
{ "t", "pageview" }, {
{ "sr", sr }, new Dictionary<string, string>()
{ "dp", page }, {
{ "dt", title }, { "name",page},
}
}
},
}; };
var content = new FormUrlEncodedContent(values); JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(values);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(GAUrl, content); var response = await client.PostAsync(GAUrl, content);
Console.WriteLine(response.ToString());
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("GAHelper:" + ex.Message); Console.WriteLine("GAHelper:" + ex.Message);
Console.WriteLine(ex.StackTrace);
} }
} }