【问题标题】:Google analytics shows clicks from cached amp pages as direct/noneGoogle 分析将来自缓存 amp 页面的点击显示为直接/无
【发布时间】:2017-11-10 16:57:57
【问题描述】:

我实现了 AMP 页面,它们被正确编入索引并出现在 Google 搜索中。当访问者点击 Google SERP 上的链接时,他们会出现在 Google Analytics(包括缓存页面)上,正如 organic/google 所引用的那样。但是,当访问者点击该 AMP 页面上的链接时,有时会期望引用者为 referral/ampprogect.org,在许多情况下为 direct/none
当然,amp-analytics 已设置。
我怀疑direct/none 出现在从主服务器提供的 AMP 页面以响应来自缓存页面的点击时。
以防万一,AMP 是几天前发布的,现在还没有全部被发现。
这有意义吗?
Amp-analytics 以非常基本的方式实现

<amp-analytics type="googleanalytics">
<script type="application/json">
{
  "vars": {
    "account": "UA-XXXXX-Y" //real account id for sure
  },
  "triggers": {
    "trackPageview": {
      "on": "visible",
      "request": "pageview"
    }
  }
}
</script>
</amp-analytics>

更新

我为 AMP 设置了 Google 跟踪代码管理器并将 amp-analitics 块更改为

<amp-analytics config="https://www.googletagmanager.com/amp.json?id=GTM-zzzzzz&gtm.url=SOURCE_URL" data-credentials="include"></amp-analytics>

结果相同。
缓存AMP页面(即https://google.com/mydomain-com.cdn...)到非amp的点击显示referral/ampproject.org,点击非缓存的AMP(即https : //mydomain.com/amp/something.aspx)显示direct/none

【问题讨论】:

  • 基于此blog,当用户导航到您的网站时,Google Analytics(分析)将不知道用户来自哪里,因此会话将被标记为direct / none(除非存在以前的广告系列数据对于那个 cookie)。

标签: google-analytics google-tag-manager amp-html amp-analytics


【解决方案1】:

感谢this great post,我明白出了什么问题,并将这些想法应用于.NET。主要思想是捕捉amp-analytics配置对象(JSON格式)并用我自己的替换它(里面有clientId)。
首先我创建了HttpHandler

''//.VB
Namespace AmpHandlers
    Public Class AmpConfig
        Implements IHttpHandler

        Private Const unixStart As DateTime = #1/1/1970# ''//start of epoc

        Public ReadOnly Property IsReusable As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property

        Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest
            context.Response.Clear()
            ''//ecpected request
            ''// https : //mydomain.com/gtm-amp.json?id=GTM-zzzzzz&gtm.url=SOURCE_URL
            If String.IsNullOrEmpty(context.Request.QueryString("id")) OrElse context.Request.QueryString("id") <> "GTM-zzzzzz" Then
                ''//no answer
                context.Response.End()
                Return
            End If
            Dim clientId As String = ""
            If context.Request.Cookies("_ga") IsNot Nothing Then
                Dim ga As String = context.Request.Cookies("_ga").Value ''//GA1.2.12321354.1507250223
                clientId = Regex.Match(ga, "(\d+?\.\d+?$)").Groups(1).Value
            Else
                Dim rand As New Random()
                ''//Majic 2147483647 is upper limit of Google's random part of _ga cookie
                ''//The second part is Unix time, in seconds
                clientId = rand.Next(2147483647) & "." & CInt(DateTime.UtcNow.Subtract(unixStart).TotalSeconds)
            End If
            ''//Set cookie and response headers
            context.Response.ContentType = "application/json" '; charset=UTF-8
            context.Response.SetCookie(New HttpCookie("_ga") With {.Value = "GA1.2." & clientId,
                .Path = "/", .Domain = context.Request.Url.Host, .Expires = DateTime.UtcNow.AddYears(2)
                                       })
            context.Response.AddHeader("Access-Control-Allow-Origin", "https://mydomain-com.cdn.ampproject.org")
            context.Response.AddHeader("Access-Control-Expose-Headers", "AMP-Access-Control-Allow-Source-Origin")
            context.Response.AddHeader("AMP-Access-Control-Allow-Source-Origin", "https://" & context.Request.Url.Host)
            context.Response.AddHeader("Access-Control-Allow-Source-Origin", "https://" & context.Request.Url.Host)
            context.Response.AddHeader("Access-Control-Allow-Credentials", "true")
            context.Response.AddHeader("Content-Disposition", "attachment; filename=""GTM-NZPM27T.json""")
            context.Response.AddHeader("cache-control", "no-cache, no-store, must-revalidate")

            ''//https://www.googletagmanager.com/amp.json?id=GTM-zzzzzz&gtm.url=SOURCE_URL response is saved locally and edited
            ''//possibly it is not the best colution
            Dim sr As New IO.StreamReader(context.Server.MapPath("~/amp-gtm.config"))
            Dim str As String = sr.ReadToEnd()
            str = str.Replace("[[clientId]]", clientId)
            context.Response.Write(str)
            context.Response.Flush()
            context.Response.End()
        End Sub
    End Class
End Namespace

接下来我在web.config注册了它。

<handlers>
  <add name="amp-gtm" verb="GET" path="gtm-amp.json" type="AmpHandlers.AmpConfig" resourceType="Unspecified"/>
</handlers>

最后放入amp-analytics标签。

<amp-analytics config="https : //mydomain.com/gtm-amp.json?id=GTM-zzzzzz&gtm.url=SOURCE_URL" data-credentials="include"></amp-analytics>

现在来自缓存和非缓存 AMP 页面的所有点击都显示organic/google

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多