Google Analytics(分析)将来自缓存放大器页面的点击次数显示为直接/无

时间:2017-10-01 04:13:02

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

我实施了AMP网页,它们被编入索引,没有任何错误,并出现在Google搜索中。当访问者点击Google SERP上的链接时,他们会在organic/google引用的Google Analytics(包括缓存页面)上显示。但是,如果访问者点击该AMP网页上的链接,则有时会指定引荐来源referral/ampprogect.org,在许多情况下direct/none。 当然,amp-analytics已设定 我怀疑,当来自主服务器的AMP页面响应来自缓存页面的点击时,会出现direct/none 以防万一,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...)到非放大器的点击会显示referral/ampproject.org并点击未缓存的AMP(即{{1} })显示https : //mydomain.com/amp/something.aspx

1 个答案:

答案 0 :(得分:0)

感谢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