Google analytics shows clicks from cached amp pages as direct/none

1.1k Views Asked by At

I implemented AMP pages and they are indexed with no errors and appear in Google search. When a visitor clicks on a link on Google SERP then they appear on Google Analytics (including cached pages) as referenced from organic/google. But when a visitor clicks on a link on that AMP page then the referrer is sometimes expected referral/ampprogect.org and in many cases direct/none.
Of course, amp-analytics is set.
I suspect that direct/none appear when AMP pages served from the main server in response to a click from cached page.
Just in case, AMP were published a few days ago and not all have been discovered by now.
Does it make any sense?
Amp-analytics is implemented in a very basic way

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

Update

I set up Google Tag Manager for AMP and changed amp-analitics block with

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

with the same result.
The click from cached AMP page (that is https://google.com/mydomain-com.cdn...) to non-amp shows referral/ampproject.org and click on non-cached AMP (that is https : //mydomain.com/amp/something.aspx) shows direct/none.

1

There are 1 best solutions below

0
On

Thanks to this great post I understood what goes wrong and applied the ideas to .NET. The main idea is to catch amp-analytics configuration object (JSON formatted) and replace it with my own (with clientId inside).
First I created 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

Next I registered it in web.config.

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

and finally put into amp-analytics tag.

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

Now all clicks from cached and non-cached AMP pages show organic/google.