Skip to content

Add Google Tag Gateway integration to Tag Manager Web_Tag class #11417

@hussain-t

Description

@hussain-t

Feature Description

Tag Manager currently loads GTM scripts from the third-party script googletagmanager.com. When GTG is enabled, Tag Manager should load GTM scripts through the local measurement.php proxy to provide first-party benefits, similar to how Analytics and Ads modules work through the GTag class.

This integration will ensure that when GTG is enabled, Tag Manager benefits from improved data quality and measurement signal recovery by serving GTM scripts through the user's own domain.


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

Acceptance criteria

  • When GTG is enabled and healthy, Tag Manager scripts should load through the first-party domain proxy (gtg/measurement.php).
  • When GTG is disabled or unhealthy, Tag Manager scripts should load directly from googletagmanager.com (current behavior).
  • Tag Manager functionality should work correctly in both GTG enabled and disabled states.
  • No functionality changes when GTG is disabled - maintains backward compatibility.
  • GTG health checks should be appropriately validated before using the proxy.
  • Note: Based on the recent discussion, GTM + GTG should use the gtag snippet approach (same as Analytics/Ads) rather than modifying GTM's custom script loading. The noscript tag should be skipped when GTG is enabled.

Implementation Brief

  • In includes/Modules/Tag_Manager/Web_Tag.php:
    • Add a new protected method is_google_tag_gateway_active. It can simply be a duplicate of Core\Tags\GTag::is_google_tag_gateway_active.
    • Add a new protected methods setup_gtag. It can imitate Modules\Ads\Web_Tag::setup_gtag.
    • Add a new protected methods filter_tag_output. It can imitate Modules\Ads\Web_Tag::filter_tag_output, with the module name in the comment changed to "Google Tag Manager" instead.
    • Update the render and render_no_js methods:
      • Return early if is_google_tag_gateway_active is true.
    • Update the register method:
      • Wrap everything currently existing except the call to the do_init_tag_action method in a conditional block so that it is only executed when is_google_tag_gateway_active is not true.
      • If is_google_tag_gateway_active is true:
        • Hook the setup_gtag method to the googlesitekit_setup_gtag action with a priority of 30.
        • Hook the filter_tag_output method to the script_loader_tag filter.

Test Coverage

  • Add a test suite for Modules\Tag_Manager\Web_Tag.
    • Verify that the tag is placed for Tag Manager.
    • Verify the variance of the tag placed with and without Google Tag Gateway.

QA Brief

  • Set up Site Kit.
  • Turn on the googleTagGateway feature flag in the tester plugin.
  • Set up the Tag Manager module.
  • Verify that the Tag Manager snippet is placed in the site.
  • Turn on Google tag gateway.
  • Verify that the Tag Manager snippet in the site is no longer present; instead, the following appears:
    • <!-- Google Tag Manager snippet added by Site Kit --> is still present.
    • A <script src="{yoursite.com}/wp-content/plugins/google-site-kit/gtg/measurement.php?id={container ID}" id="google_gtagjs-{container ID}-js" async></script> is added (notice the absence of &#038;s=/gtag/js unline other tags).
    • A gtag("config", "{container ID}"); is added to the gtag script.
  • Regardless of whether GTG is enabled or not, verify that:
    • Tag Manager functionality works as expected.
      • Use the Tag Assistant to ensure that the GTM container is loaded on the site.
    • Site Health correctly detects Site Kit placed Tag Manager tag and existing tags.

Changelog entry

  • Use the GTag script via the first-party domain proxy gtg/measurement.php for Tag Manager containers when Google tag gateway for advertisers is active.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Module: Tag ManagerGoogle Tag Manager module related issuesP0High priorityTeam MIssues for Squad 2Type: EnhancementImprovement of an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions