Migrate Google Analytics to Google Tag
In our site install process, we enable modules through a configuration push. Since upgrading to Drupal 10, we've noticed a few modules that do not like this process and need to be patched temporarily to allow the site install process to function.
Modules Affected:
- Google Analytics
-
User ProtectComments: - This is done for now but I want to keep it alive to monitor as we wait for patches to be moved into tagged releases. (Michael Lee - Dec 7, 2023)
- User Protect 1.3.0 implements the patch so it is now fixed for that module. (Michael Lee - Jul 8, 2024)
- Per the Google Analytics module page it's stated that the module is considered to be obsolete in favor of using features provided in the 2.0 version of Google Tag. We already use Google Tag so I need to investigate this to confirm this is correct and make a plan for retiring the module on our sites if so. (Michael Lee - Aug 7, 2024)
- There are built-in hooks in Google Tag 2.x that should migrate configuration set within Google Analytics but it's a little complicated. The hooks are meant during install and we already have Google Tag installed. I'm curious about the possible solution:
- Export existing Google Tag configuration (
google_tag.container.primary.yml
) - Uninstall Google Tag
- Upgrade Google Tag to 2.x
- Install and hope the hooks run to migrate Google Analytics configuration
If this doesn't work, it'll likely require some manual work or custom code. (Michael Lee - Aug 7, 2024)
- google_tag.container.primary.yml
uuid: 876a3f87-3416-4214-83c3-1eed8bf1b671
langcode: en
status: true
dependencies: { }
id: primary
label: Primary
weight: 0
container_id: GTM-P8MKDW6
hostname: www.googletagmanager.com
data_layer: dataLayer
include_classes: false
whitelist_classes: |-
google
nonGooglePixels
nonGoogleScripts
nonGoogleIframes
blacklist_classes: |-
customScripts
customPixels
include_environment: false
environment_id: ''
environment_token: ''
path_toggle: 'exclude listed'
path_list: |-
/admin*
/batch*
/node/add*
/node/*/edit
/node/*/delete
/user/*/edit*
/user/*/cancel*
role_toggle: 'exclude listed'
role_list: { }
status_toggle: 'exclude listed'
status_list: |-
403
404
conditions: { }``` (Michael Lee - Aug 7, 2024)
- Exporting existing configuration will not work **but** uninstalling Google Tag, upgrading, and then reinstalling will pull over existing Google Analytics config. So, I think the solution here is to do that and then manually re-insert the GTM-P8MKDW6 tag and push config to re-add to all sites.
A couple things I want to double check:
1. Are there things missing in the GA to GT migration that we need to be concerned about?
2. Are there configs that need to be ignored again to prevent overwriting GA tags on each site?
- `google_analytics.settings` is ignored which allows users to add unique IDs on each site. We need to find a solution here.
- Google Tag allows for multiple tag containers and it actually creates a unique one when migrating GA. We could ignore `google_tag.container.*` to ignore ALL tag containers, check the box that allows you to have multiple tag containers, and then use `~google_tag.container.primary.yml` to force import on the primary container which will hold the GTM-P8MKDW6 tag. This should keep unique IDs separate and prevent them from being overwritten. (Michael Lee - Aug 7, 2024)
- New Test! Doing this Thursday, Aug 8
1. On a fresh site, uninstall Google Tag
2. Upgrade Google Tag to 2.x
3. Install Google Tag
4. Check box in to allow for multiple Tag containers
5. Add GTM tag to existing Primary container
6. Run `drush cex` to export current config
7. Add the config ignores mentioned in a previous comment
8. Make a change in both containers
9. Run `drush cim` to import config
10. Verify that things were ignored correctly (Michael Lee - Aug 7, 2024)