首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌分析显示,从缓存的放大器页面点击为直接/无

谷歌分析显示,从缓存的放大器页面点击为直接/无
EN

Stack Overflow用户
提问于 2017-10-01 04:13:02
回答 1查看 1K关注 0票数 1

我实现了AMP页面,他们没有错误的索引,并出现在谷歌搜索。当访问者点击Google上的链接时,他们就会出现在Google (包括缓存的页面)上,并从organic/google中引用。但是,当访问者单击AMP页面上的链接时,有时会期望引用者是referral/ampprogect.org,在很多情况下是direct/none

当然,amp-analytics已经设置好了。

我怀疑,当AMP页面响应缓存页面的单击而从主服务器服务时,就会出现direct/none

以防万一,AMP是几天前出版的,到目前为止还没有全部被发现。

这有什么意义吗?

放大器分析是以一种非常基本的方式实现的。

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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...)到non的单击显示referral/ampproject.org,单击非缓存的AMP (即https : //mydomain.com/amp/something.aspx)显示direct/none

EN

回答 1

Stack Overflow用户

发布于 2017-10-06 00:57:12

多亏了这个伟大的职位,我才知道出了什么问题,并将这些想法应用到了.NET上。其主要思想是捕获amp-analytics配置对象(JSON格式化的)并将其替换为我自己的配置对象(内部有clientId )。

我首先创建了HttpHandler

代码语言:javascript
复制
''//.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注册了它。

代码语言:javascript
复制
<handlers>
  <add name="amp-gtm" verb="GET" path="gtm-amp.json" type="AmpHandlers.AmpConfig" resourceType="Unspecified"/>
</handlers>

最后放入amp-analytics标签。

代码语言:javascript
复制
<amp-analytics config="https : //mydomain.com/gtm-amp.json?id=GTM-zzzzzz&gtm.url=SOURCE_URL" data-credentials="include"></amp-analytics>

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46509133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档