site-kit-wp: Prevent duplicate Ads conversion tracking ID output

Bug Description

While #8313 implements an independent ability for the Ads module to output the Ads conversion tracking ID, it doesn’t prevent the Analytics module from still outputting this tag if still set.

We have #8248 that migrates the Ads conversion tracking ID from the Analytics to the Ads module thus effectively removing it from the Analytics module, however, keep in mind that this migration is conditional. This migration will only happen if the user goes to Site Kit Settings and opens the Analytics settings edit/view screens. See this AC from #8248:

For users who were previously using the value of the GA4 Ads Conversion ID label:

  • Upon visiting the legacy settings field for the first time following plugin update, the value should be migrated to the new location applicable to the settings defined in the Ads module

Let’s take the following scenario into consideration:

  1. A pre-existing Site Kit installation has the Analytics module set up with an Ads conversion tracking ID entered.
  2. Now, the new “Ads” module is launched.
  3. The user does not feel any necessity to open the Analytics settings edit/view screens, as a result, the migration of the existing Ads conversion tracking ID does not happen from the Analytics to the Ads module.
  4. They see the new “Ads” module and set it up with the same or different Ads conversion tracking ID.

The above steps will make Site Kit output two tags for the Ads conversion tracking ID, one from the Analytics module, and the other from the Ads module. Here’s a screenshot of an experiment I did to replicate the above scenario: image

I think the above scenario can potentially cause duplicate/erroneous tracking, and should be addressed.

Steps to reproduce

  1. Set up Site Kit and the Analytics module.
  2. Do not turn on the adsModule feature flag yet.
  3. Add an “Ads Conversion ID” to the Analytics module.
  4. Now, go back to the Site Kit settings (with Analytics settings accordion closed).
  5. Turn on the adsModule feature flag.
  6. Do not open the Analytics settings edit/view screens after this point, otherwise, the migration will be performed.
  7. Go to Site Kit settings -> Connect More Services and connect the Ads module with a different "Conversion Tracking ID`.
  8. View page source of the front end and notice that two ads conversion tracking IDs are placed.

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

Acceptance criteria

  • There should only be one Ads tag rendered on the page for users of prior GA4 tracking and newly added Ads module
    • Users who do not visit the GA4 settings screen should not have duplicate gtags printed in the source
    • The (new) Ads module Conversion ID value should be used for gtag printing
    • The (legacy) GA4 module Conversion ID value should be ignored if an Ads equivalent is present

Implementation Brief

Version Specific Migration

  • Create a new Migration_n_e_x_t.php (replacing n_e_x_t with the version this feature is expected to be released) which always migrates the adsConversionID from the Analytics Module to the Ads Module, if found, and activates the Ads Module. Use the includes/Core/Util/Migration_1_123_0.php file as the base structure of the migration.
    • Set the DB_VERSION property in the Migration class to the version in which this feature will be deployed.
    • Create $context, $options, $ads_settings and $analytics_settings class properties and in the constructor, set them in the same way as in the 1.123.0 migration, with the addition of the Google\Site_Kit\Modules\Ads\Settings class which should be included and instantiated to $ads_settings property.
    • The migrate method should check the current database version using $this->options->get( self::DB_VERSION_OPTION ); and if this value is falsy or below self::DB_VERSION using the version_compare function then it should run two migration methods:
      • $this->migrate_analytics_conversion_id_setting();
      • $this->activate_ads_module();
    • In the migrate_analytics_conversion_id_setting method:
      • Return null if ! $this->analytics_settings->has().
      • Get the settings from the analytics-4 module using $this->options->get( Analytics_4::MODULE_SLUG );
      • If there the adsConversionID setting exists and is not empty on the analytics-4 module:
        • Check if there is an existing adsConversionID on the Ads module, in this case, remove the value from the Analytics module settings using $this->analytics_settings->delete('adsConversionID').
        • If there is not an existing adsConversionID in the ads module:
          • Set the adConversionID on the Ads module settings using $this->ads_settings->set('adsConversionID', $analytics_settings['adsConversionID'])
          • Set the adsConversionIDMigratedAtMs on the Ads module settings to the current timestamp, in order to show the banner in the frontend later, using $this->ads_settings->set('adsConversionIDMigratedAtMs', time() * 1000)
          • Remove the value from the Analytics module settings using $this->analytics_settings->delete('adsConversionID').
    • In the activate_ads_module method:
      • Check if the Ads module is already active by looking for the ads slug in the $active_modules = $this->options->get( 'googlesitekit-active-modules' ); array.
      • Check if the adsConversionID has been added to the Ads module options in the migration_analytics_conversion_id_setting method, by getting the ads module options using $this->ads_settings->get('adsConversionID') and checking that it is truthy.
      • If both of these checks are met, activate the Ads module by adding the ads module key to the $active_modules array using $active_modules[] = Ads::MODULE_SLUG then updating the active modules using $this->options->set( Modules::OPTION_ACTIVE_MODULES, $option );

Remove Frontend Migration Logic

  • Remove assets/js/modules/analytics-4/hooks/useMigrateAdsConversionID.js and both uses of the useMigrateAdsConversionID hook from Analytics SettingsView and SettingsEdit.

Update the Analytics Notification Logic

  • Confirm that the assets/js/modules/analytics-4/components/settings/AdsConversionIDSettingsNotice.js behaves as expected with the adsConversionIDMigratedAtMs setting now being added by the migration.

Test Coverage

  • Remove assets/js/modules/analytics-4/hooks/useMigrateAdsConversionID.test.js.
  • Add a thorough set of tests in tests/phpunit/integration/Core/Util/Migration_n_e_x_tTest.php that tests these key cases:
    • Migration where neither the Analytics or Ads module are active
    • Migration where the Analytics module is active but does not have an adsConversionID
    • Migration where the Analytics module is active and has an adsConversionID where:
      • The Ads module is inactive
      • The Ads module is active with an existing adsConversionID

QA Brief

  • Configure a previous version of the plugin with an Analytics module with and adsConversionID set.
  • Upgrade the plugin to this version to trigger the migration to run.
  • You will now see the Ads module has been activated, and the adsConversionID has been migrated.
  • You should also see the migration notice message when viewing the Analytics module settings.
  • If you enable the Ads module manually and set a new adsConversionID before updating the version and running the migration, the Ads module adsConversionID will not be overwritten, the Analytics module value will be removed.
  • Test all cases setup cases from the Bug Bash notes including:
    • Analytics Module not setup before migration.
    • Analytics Module setup with Ads Conversion ID before migration.
    • Analytics Module and Ads Module set up, both with conversion IDs set, before migration. (in this case the Ads module conversion ID is retained.)

Changelog entry

  • Update the conversion tracking migration to happen on the backend side.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15

Most upvoted comments

@nfmohit @10upsimon I think a one time DB migration makes sense for the handling related to moving the conversion ID value between modules. The concern of a temporary notice within GA can be handled separately similar what we do for new badges, but the two don’t need to happen at the same time. It’s possible they could use some shared state but if we had to choose, I’d rather have the temporary notice triggered by the migration in the background rather than trigger the migration as a side-effect of viewing the module settings (even if that means the notice isn’t seen right away).

@nfmohit @benbowler let’s follow up with an issue to rename this migration for consistency with the others. Thanks!

Hey @mohitwp, really good catch here! I found the issue and have created a follow up PR where the notice should show correctly.

To be clear, in Scenario 2, this message should not show, because in this case the user has already visited and added the conversion ID to the Ads module themselves so the value hasn’t actually been migrated and the banner wouldn’t make sense.

For Scenario 1 this banner should show after the migration has occurred, until the user dismisses it or until 28 days after the migration.

@benbowler Can you create that follow-up issue? Fine to leave an outline and move it to the triage/AC column 👍🏻

IB here looks good, I think it makes sense 🙂

IB ✅