18

我想知道是否可以通过发送自定义 URL 请求将谷歌分析跟踪数据发送给谷歌。我假设我可以构建自己的 URL 并发出请求以获取跟踪的事件,如下所示:http://google.com/analytics-endpoint?id=UA-34900236-1&event=some_event,但我还没有找到任何关于此的文档,并且想知道这是否可能?如果是这样,有人可以指出我正确的文件吗?

感兴趣的人的背景:我目前正在尝试将谷歌分析支持添加到 Mono for Android 应用程序。我无法编译任何 c# google 分析库,因为 Mono for Android 中缺少必需的 .net 库。

4

6 回答 6

22

作为@PT 回答的补充,我想指出谷歌现在发布了一个官方 API 来将数据发送到谷歌分析。这是谷歌分析测量协议。这可能是最安全的解决方案,因为它是一个“官方”且记录在案的 API。

于 2013-06-05T12:10:14.623 回答
8

@i.amniels answer的启发,我围绕Google Analytics Measurement Protocol编写了一个小包装器,用于跟踪我们 Web 应用程序服务器端的事件。

这是您可以开始学习的课程的要点。它只是包装了向 Google Analytics 测量协议端点发送 POST 请求的样板代码。

使用该包装器,您将能够编写以下代码:

GoogleAnalyticsApi.TrackEvent("Video", "Play", "Vacation 2014")
于 2014-04-23T19:42:49.560 回答
4

是的,您可以直接向 Google Analytics 发出 HTTP 请求以跟踪任意应用程序。例如,现有的 Android GA 库就是这样做的(它使用一组非常具体的 URL 参数发出 HTTP_GET 请求)。

没有关于使用底层 HTTP API 作为客户端的官方文档,但考虑到网络上存在大量古老的 JavaScript 片段,以及编译到现有 Android 应用程序中的固定库代码,您可以依赖它相当稳定。GIF 参数故障排除文档解释了分析数据的编码方式。

这是一个为纯 Java 应用程序提供客户端库的现有项目: http ://code.google.com/p/jgoogleanalytics/

如果你想在 C# 中重新实现它,魔法似乎都在这里:http ://code.google.com/p/jgoogleanalytics/source/browse/trunk/src/main/java/com/boxysystems/ jgoogleanalytics/GoogleAnalytics_v1_URLBuildingStrategy.java

于 2012-09-30T02:59:26.320 回答
2

受@Oliver 回答的启发,我更新了 C# 代码,以便在发送 POST 数据时更加及时:

namespace Helpers
{
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Threading.Tasks;

    // More information about API - see https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
    public class GoogleAnalyticsHelper
    {
        private readonly string endpoint = "https://www.google-analytics.com/collect";
        private readonly string googleVersion = "1";
        private readonly string googleTrackingId; // UA-XXXXXXXXX-XX
        private readonly string googleClientId; // 555 - any user identifier

        public GoogleAnalyticsHelper(string trackingId, string clientId)
        {
            this.googleTrackingId = trackingId;
            this.googleClientId = clientId;
        }

        public async Task<HttpResponseMessage> TrackEvent(string category, string action, string label, int? value = null)
        {
            if (string.IsNullOrEmpty(category))
              throw new ArgumentNullException(nameof(category));

            if (string.IsNullOrEmpty(action))
              throw new ArgumentNullException(nameof(action));

            using (var httpClient = new HttpClient())
            {
                var postData = new List<KeyValuePair<string, string>>()
                {
                    new KeyValuePair<string, string>("v", googleVersion),
                    new KeyValuePair<string, string>("tid", googleTrackingId),
                    new KeyValuePair<string, string>("cid", googleClientId),
                    new KeyValuePair<string, string>("t", "event"),
                    new KeyValuePair<string, string>("ec", category),
                    new KeyValuePair<string, string>("ea", action)
                };

                if (label != null)
                {
                    postData.Add(new KeyValuePair<string, string>("el", label));
                }

                if (value != null)
                {
                    postData.Add(new KeyValuePair<string, string>("ev", value.ToString()));
                }


                return await httpClient.PostAsync(endpoint, new FormUrlEncodedContent(postData)).ConfigureAwait(false);
            }
        }
    }
}

可以在GitHub Gist上找到

用法:

var helper = new GoogleAnalyticsHelper("UA-XXXXXXXXX-XX", "555");
var result = helper.TrackEvent("Orders", "Order Checkout", "OrderId #31337").Result;
if (!result.IsSuccessStatusCode)
{
    new Exception("something went wrong");
}
于 2016-12-29T10:35:15.427 回答
1

是的,这是可能的。因为 Google Analytics 会存储它所在的每个 URL 页面请求。这些在左侧菜单的内容选项卡下可见,然后找到 URL 或页面内容。您可以看到列出的每个页面请求。因此,如果您因为<a href="more.php?id=8&event=sales">LINK</a>Analytics 之类的链接而将其关闭,则会存储完整的 URL。

但是,没有通过您提供的 URL 直接访问您的 Google Analytics(分析)帐户的路径,希望得到类似的答案:我认为这是您能做的最好的事情。

您可以制作一个在每个页面上都有跟踪代码的页面。这样,谷歌分析将捕获所有正在发生的事情。然后,您可以将您的“事件”添加到页面上每个链接的末尾,这样当用户单击链接时,它将重定向到您网站上的相应页面,但它也会记录(在来自链接的href)在Google Analytics上,因为GA可以看到页面内发生的一切,包括链接href值的完整URL。因此,如果您的链接看起来像这样,Google Analytics 会记录整个 URL,您可以稍后检索:

<a href="page2.php?id=4492&event=clickedCatalog&preference=yellow">Link!</a>

...将page2.php?id=4492&event=clickedCatalog&preference=yellow在 GA 中记录完整的 URL(

于 2012-09-29T13:56:44.193 回答
0

除了@pavel-morshenyuk 回答之外,如果您想将事件与对其进行操作的用户相关联,下面的代码会将请求映射到 google 客户端 ID。

 string clientId = Guid.NewGuid().ToString();

 if (Request != null && Request.Cookies != null && Request.Cookies.Get("_ga") != null && Request.Cookies.Get("_ga").Value != null)
 {
    clientId = Request.Cookies.Get("_ga").Value;
    clientId = clientId.Replace("GA1.2.", "");
    clientId = clientId.Replace("GA1.1.", "");
 }

请注意,如果 GA 更改其内部 cookie 跟踪,则可能需要对其进行调整。

于 2018-04-20T14:42:41.070 回答