Site-kit-wp: Search Console Tag Manager Verification fails with Site Kit's container snippet

Created on 28 Mar 2020  Â·  2Comments  Â·  Source: google/site-kit-wp

Bug Description

When trying to verify a site in Search Console using the Google Tag Manager method, Site Kit's Tag Manager container snippet fails and is flagged as being in the wrong location on the page:

GTM verification failed

Related support topic: https://wordpress.org/support/topic/gsc-says-google-tag-manager-snippet-is-in-the-wrong-location/

Steps to reproduce

  1. Set up Site Kit, connect to Tag Manager, and insert Site Kit's container snippet on a site.
  2. Use a secondary Google account that is not a verified owner to add the site as a new property from Search Console.
  3. Attempt verification from the Search Console account using the Google Tag Manager method (note the secondary Google account must be given Publish permission for the Tag Manager container).
  4. See the above notification that ownership verification failed.

Additional Context

  • PHP Version: 7.3.16
  • OS: MacOS
  • Browser: Chrome
  • Plugin Version: 1.6.0
  • Device: MacBook Air

The documentation has code placement requirements for this verification method. Testing an alternate method to insert the container (i.e. plugin) provided successful verification.

See also #721


_Do not alter or remove anything below. The following sections will be managed by moderators only._

Acceptance criteria

  • GTM container code should be placed in the correct part of the page to ensure that verification succeeds via this method.

Implementation Brief

  • Add an additional hook in Tag_Manager::register for invoking print_gtm_no_js on the wp_body_open action
    The wp_body_open function and its corresponding action were only introduced in WP 5.2 so we need to keep the existing action as well.
    Because of this, we likely can't support this method for older WP or themes that don't implement this hook properly.
  • Add a new private did_gtm_no_js property to Tag_Manager
  • Bail early in print_gtm_no_js if did_gtm_no_js is set
  • Set did_gtm_no_js in print_gtm_no_js when invoked the first time

QA Brief

Changelog entry

  • Place Tag Manager snippet for non-JavaScript support after opening body tag as commonly expected.
Good First Issue P1 Bug

Most helpful comment

Tested the fix with @ernee

Installed SK 1.7.1 zip, activated and connected Tag Manager and inserted Site Kit's container snippet on hellogoodbye.today

Setup a non verified secondary google account and verified from the Search Console account using the Google Tag Manager.

Notice:
image

Passed QA ✅

All 2 comments

IB ✅

We should also use a very early priority on wp_body_open - there's no guarantee we're the earliest, but let's do our best. WordPress is terrible with this anyway, I'm perfectly fine with using something like -9999 :)

Tested the fix with @ernee

Installed SK 1.7.1 zip, activated and connected Tag Manager and inserted Site Kit's container snippet on hellogoodbye.today

Setup a non verified secondary google account and verified from the Search Console account using the Google Tag Manager.

Notice:
image

Passed QA ✅

Was this page helpful?
0 / 5 - 0 ratings