amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Preserve URLs when sending to Google Analytics via amp-analytics

Open joshcp opened this issue 9 years ago • 7 comments

What's the issue?

We're using event tracking to capture the URL when a user clicks an outbound link. However, the data is urlencoded - which is a problem when trying to compare with non-urlencoded data from the primary (non-amp) property.

How do we reproduce the issue?

Currently collecting event data as outlined in the Google Analytics AMP documentation. (Note issue #8692 related to documentation error)

<amp-analytics>
    <script type="application/json" type="googleanalytics">
        {
          "vars": {
            "account": "UA-XXXXXX-YY"
          },
          "triggers": {
            "trackPageview": {
              "on": "visible",
              "request": "pageview"
            }, 
            "trackOutboundClick": {
                "on": "click",
                "selector": "[href*='/offsite/']",
                "request": "event",
                "vars": {
                    "eventCategory": "outbound-link-aff", 
                    "eventAction": "${outboundLink}"
                }
            }
          }
        }
    </script>
</amp-analytics>

and

<a href="http://example.com/offsite/page" data-vars-outbound-link="http://example.com/offsite/page">Some link</a>

Data from amp-analytics shows up in Google Analytics as http%3A%2F%2Fexample.com%2Foffsite%2Fpage

Data from standard analytics event tracking shows up as http://example.com/offsite/page

What browsers are affected?

Behavior seems to be consistent across browsers.

Which AMP version is affected?

Currently using AMP ⚡ HTML – Version 1492571781980

joshcp avatar Apr 24 '17 23:04 joshcp

We've been experiencing the same thing - ${outboundLink} ends up url-encoded and if we send it as an eventLabel to GA, our data people are staring at not-human-friendly URLs 😆

kschmidtdev avatar Apr 24 '17 23:04 kschmidtdev

/to @lannka

jridgewell avatar Apr 25 '17 18:04 jridgewell

Do we know if this needs to be addressed at the AMP layer or the GA layer? Does the same encoding of the data go over the network from both pages but then it just get handled differently on the GA side for AMP vs. non-AMP?

cc @avimehta

rudygalfi avatar Apr 25 '17 23:04 rudygalfi

It looks like the amp-analytics is url-encoding some parameters, but everything already gets url-encoded, so it's double-encoded. Illustrated here: screen shot 2017-04-25 at 4 56 41 pm

The resulting URL is: https://www.google-analytics.com/collect?v=1&_v=a1&ds=AMP&aip=true&_s=2&dt=La%20Nuit%20Tr%C3%A9sor&sr=375x667&_utmht=1493164531609&cid=k2zQnzz0m_6T7IyLuQ0DTD_lsTeNk84KVDnEdbn3ch_EmOIofdjizY4cX2PCag9u&tid=UA-51977016-42&dl=https%3A%2F%2Famp.mobify.net%2Flancome%2Fhttps%3A%2F%2Fwww.lancome-usa.com%2Ffragrance%2Fcollection%2Fla-nuit-tresor&dr=&sd=24&ul=en-us&de=UTF-8&t=event&jid=&ec=outbound&ea=click&el=&ev=0&a=9294&z=0.933893780719955&cd1=plp You can see that el is double-url-encoded.

kschmidtdev avatar Apr 26 '17 00:04 kschmidtdev

@avimehta should this be part of your "Filter" work?

lannka avatar Jun 16 '17 04:06 lannka

@lannka To take another look.

rudygalfi avatar Jul 14 '17 20:07 rudygalfi

This is indeed a bug. It happens when a var is defined recursively:

"vars": {
  "a": "${b}",
  "b": "${c}",
  "c": "https://google.com"
}

it gives:

a:https%253A%252F%252Fgoogle.com
b:https%3A%2F%2Fgoogle.com
c:https://google.com

I'm looking into a fix. /cc @avimehta

lannka avatar Jul 20 '17 21:07 lannka