diff --git a/RevokeMsgPatcher/Utils/GAHelper.cs b/RevokeMsgPatcher/Utils/GAHelper.cs
index b9f40108..1de8d423 100644
--- a/RevokeMsgPatcher/Utils/GAHelper.cs
+++ b/RevokeMsgPatcher/Utils/GAHelper.cs
@@ -4,16 +4,19 @@
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
+using System.Web.Script.Serialization;
using System.Windows.Forms;
namespace RevokeMsgPatcher.Utils
{
///
/// 用于软件的 Google Analytics 实现 By huiyadanli
+ /// 20230409 更新 GA4 的实现
/// 相关文档:
- /// 指南 https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
- /// 参数 https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
- /// 测试 https://ga-dev-tools.appspot.com/hit-builder/
+ /// #GA指南(过时) https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
+ /// #GA参数(过时) https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
+ /// GA4教程 https://firebase.google.com/codelabs/firebase_mp
+ /// 测试 https://ga-dev-tools.google/ga4/event-builder/
///
public class GAHelper
{
@@ -26,11 +29,11 @@ public static GAHelper Instance
{
//lock (obj)
//{
- if (instance == null)
- {
- instance = new GAHelper();
- }
- return instance;
+ if (instance == null)
+ {
+ instance = new GAHelper();
+ }
+ return instance;
//}
}
}
@@ -38,15 +41,10 @@ public static GAHelper Instance
// 根据实际情况修改
private static readonly HttpClient client = HttpUtil.Client;
- private const string GAUrl = "https://www.google-analytics.com/collect";
-
- // 根据实际使用分析账号设置
- private const string tid = "UA-80358493-2"; // GA Tracking ID / Property ID.
+ private const string GAUrl = "https://www.google-analytics.com/mp/collect?api_secret=urKlcc29TSy3OIkHr8yFSQ&measurement_id=G-BE6FRPZS1W";
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; }
@@ -59,29 +57,37 @@ public async Task RequestPageViewAsync(string page, string title = null)
{
try
{
- if (!page.StartsWith("/"))
+ if (page.StartsWith("/"))
{
- page = "/" + page;
+ page = page.Remove(0, 1);
}
+ page = page.Replace("/", "_").Replace(".", "_");
// 请求参数
- var values = new Dictionary
+ var values = new Dictionary
{
- { "v", "1" }, // 当前必填1
- { "tid", tid },
- { "cid", cid },
- { "ua", UserAgent },
- { "t", "pageview" },
- { "sr", sr },
- { "dp", page },
- { "dt", title },
+ { "client_id",UserAgent},
+ { "user_id", cid },
+ { "non_personalized_ads", "false" },
+ { "events", new List>()
+ {
+ new Dictionary()
+ {
+ { "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);
+ Console.WriteLine(response.ToString());
}
catch (Exception ex)
{
Console.WriteLine("GAHelper:" + ex.Message);
+ Console.WriteLine(ex.StackTrace);
}
}