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

Comments

tduong created an issue. See original summary.

mtift’s picture

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

berdir’s picture

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

berdir’s picture

Title: Add google_analytics hook for AMP pages » Add google_analytics hook to skip default GA integration for AMP pages
tduong’s picture

added reference to the GoogleAnalytics issue.

tduong’s picture

Assigned: Unassigned » tduong
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new699 bytes

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

tduong’s picture

Issue summary: View changes

Updated IS to be more clear.

berdir’s picture

Status: Needs review » Postponed
+++ b/amp.module
@@ -262,6 +262,19 @@ function amp_page_bottom(array &$page_bottom) {
+ * Tracking visibility check for pages.
+ *
+ * Depending on whether the active route is an AMP one, this function returns
+ * TRUE if JS code should be added to the current page, otherwise FALSE.
+ */
+function amp_google_analytics_visibility_pages_alter(&$page_match) {

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

tduong’s picture

StatusFileSize
new353 bytes
new715 bytes

Updated that line.

tduong’s picture

New reference to the GoogleAnalytics project. Discussion is moved to #1813014: Dynamically switch visibility setting

karens’s picture

Status: Postponed » Closed (won't fix)

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

playful’s picture

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

playful’s picture

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

jedihe’s picture

@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).

playful’s picture

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

jedihe’s picture

Great! just had a look at mailchimp.module, and it looks like one of these may work:

// my_theme.theme, 'my_theme' must be replaced by the machine name of the theme you're using for AMP).
function my_theme_page_bottom(array &$page_bottom) {
  unset($page_bottom['mailchimp_connected']);
}

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:

{{ page_bottom|without('mailchimp_connected') }}

Let us know how it goes! :)

playful’s picture

The hook method didn't work, but the html.html.twig override worked. Finally have valid AMP pages. Thank you for your help!