Problem/Motivation
We should track a visibility check for AMP pages. Depending on whether the active route is an AMP one, we should tell _google_analytics_visibility_pages() to add its JS script to the current page or not, to prevent the default output of GoogleAnalytics from appearing (an html_head element added in hook_page_attachments()).
This was the reference to the GoogleAnalytics project: #2733291: Add alter hook to _google_analytics_visibility_pages()
Now the discussion is moved to #1813014: Dynamically switch visibility setting
Proposed resolution
in amp_page_bottom(), add an AMP google_analytics_visibility_pages alter hook which check if the route is AMP, then do not render the GoogleAnalytics JS code (set $page_match to FALSE).
In the referenced issue: call alter() in _google_analytics_visibility_pages() and add an alter hook to be able to alter $page_match.
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | add_google_analytics-2731279-9.patch | 715 bytes | tduong |
| #9 | interdiff-2731279-6-9.txt | 353 bytes | tduong |
| #6 | add_google_analytics-2731279-6.patch | 699 bytes | tduong |
Comments
Comment #2
mtiftI don't know a lot about Google Analytics, but I'm wondering about this approach vs what is being worked on for the D7 branch in #2716253: Abstract the AMP Analytics Implementation. This seems like it's potentially a different solution to the same problem?
Comment #3
berdir@tduong, you need to provide a patch for the changes you made. And reference the google_analytics issue that you also created.
@mtift: This is only about preventing that the default output of google analytics (a html_head element added in hook_page_attacments()) from appearing. It's not at all related to how amp specific elements are added.
Comment #4
berdirComment #5
tduong commentedadded reference to the GoogleAnalytics issue.
Comment #6
tduong commentedAdded hook to skip default GA integration for AMP pages.
In the referenced issue I've already uploaded the required patch that makes this feature work combined with this one.
Comment #7
tduong commentedUpdated IS to be more clear.
Comment #8
berdirThis is a hook implementation, it should have a Implements hook__() instead as the first line.
The second part can stay.
This is postponed on the google analytics issue for now.
Comment #9
tduong commentedUpdated that line.
Comment #10
tduong commentedNew reference to the GoogleAnalytics project. Discussion is moved to #1813014: Dynamically switch visibility setting
Comment #11
karens commentedThe linked issue is now two years old and no indication it is going to be accepted, so there is nothing to do here. Should it get in you can create a new issue.
Comment #12
playful commentedIs there any way to skip the default GA integration for AMP pages?
I'm having this problem with amp 8.3 and it is the only issue preventing my pages from validating.
Any help would be greatly appreciated!
Comment #13
playful commentedIs there any way to skip the default GA integration for AMP pages?
I'm having this problem with amp 8.3 and it is the only issue preventing my pages from validating.
Any help would be greatly appreciated!
Comment #14
jedihe commented@playful: amptheme 1.x has a snippet that will filter out script tags added as html_head attachments: https://git.drupalcode.org/project/amptheme/-/blob/8.x-1.x/amptheme/ampt... (you shouldn't need the first "if" that checks for the viewport).
Comment #15
playful commented@jedihe, thanks so much for this tip! I've added it to my AMP subtheme .theme file. It works to remove the default Google Analytics script from my AMP pages, but it's not removing the Mailchimp script (below), which is now the last one keeping my pages from validating. I'm guessing this can be easily fixed by a small adjustment to the snippet, but I'm pretty PHP-illiterate. Any suggestion on this?
<script id="mcjs">!function(c,h,i,m,p){m=c.createElement(h),p=c.getElementsByTagName(h)[0],m.async=1,m.src=i,p.parentNode.insertBefore(m,p)}(document,"script","https://chimpstatic.com/mcjs-connected/js/users/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.js");</script>Comment #16
jedihe commentedGreat! just had a look at mailchimp.module, and it looks like one of these may work:
If that doesn't work, find html.html.twig (inside the theme you use for AMP), locate the page_bottom variable and update it to look like this:
Let us know how it goes! :)
Comment #17
playful commentedThe hook method didn't work, but the html.html.twig override worked. Finally have valid AMP pages. Thank you for your help!