mirror of
https://github.com/huiyadanli/RevokeMsgPatcher.git
synced 2025-07-07 15:56:08 +08:00
[#] 升级为GA4
This commit is contained in:
parent
6b63398222
commit
3407aff012
|
@ -4,16 +4,19 @@ using System.Linq;
|
|||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Script.Serialization;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace RevokeMsgPatcher.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// 用于软件的 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/
|
||||
/// </summary>
|
||||
public class GAHelper
|
||||
{
|
||||
|
@ -26,11 +29,11 @@ namespace RevokeMsgPatcher.Utils
|
|||
{
|
||||
//lock (obj)
|
||||
//{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new GAHelper();
|
||||
}
|
||||
return instance;
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new GAHelper();
|
||||
}
|
||||
return instance;
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
@ -38,15 +41,10 @@ namespace RevokeMsgPatcher.Utils
|
|||
// 根据实际情况修改
|
||||
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 @@ namespace RevokeMsgPatcher.Utils
|
|||
{
|
||||
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
|
||||
{ "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<Dictionary<string, string>>()
|
||||
{
|
||||
new Dictionary<string, string>()
|
||||
{
|
||||
{ "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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user