Skip to content

Commit

Permalink
[#] 升级为GA4
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyadanli committed Apr 9, 2023
1 parent 6b63398 commit 3407aff
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions RevokeMsgPatcher/Utils/GAHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <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
{
Expand All @@ -26,27 +29,22 @@ public static GAHelper Instance
{
//lock (obj)
//{
if (instance == null)
{
instance = new GAHelper();
}
return instance;
if (instance == null)
{
instance = new GAHelper();
}
return 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; }

Expand All @@ -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<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);
}
}

Expand Down

0 comments on commit 3407aff

Please sign in to comment.