Problem/Motivation
After creating a new property in Google Analytics, it automatically creates a Google Analytics 4 property. It now uses a new property ID, beginning with a "G".
Steps to reproduce
Proposed resolution
Add possibility to add the "G"-ID to the existing ID-Field for "UA"-IDs. Change the allowed input pattern.
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #26 | google_analytics-ga4-3178179-26-d7.patch | 5.46 KB | jenlampton |
| #24 | 3178179-google-analytics-4.patch | 6 KB | greg boggs |
| #17 | 3178179-13.patch | 5.36 KB | damien laguerre |
| #9 | ga4-3178179-9.patch | 4.44 KB | damien laguerre |
| #7 | 3178179-7.patch | 3.63 KB | qusai taha |
Issue fork google_analytics-3178179
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
zach.bimson commentedPatch from 8.x-3.x to update the validation
Comment #3
mikedance commentedI rerolled your patch to fix an issue with the new measurement-id format causing this check to fail:
if (preg_match('/^G|UA-\d+-\d+$/', $id) && (_google_analytics_visibility_pages() || in_array($status, $trackable_status_codes)) && _google_analytics_visibility_user($account)) {
Causing the gtag function to not get included, etc.
Comment #4
hesco commentedThe mikedance patch worked for me, as described here:
https://gist.github.com/hesco/162b192c89db129fa1f3f8a6fe9fb05f#file-0200...
Comment #5
_gramur commentedAs a side note. I connected the new Google Analytics 4 properties with my old properties via Google Analytics dashboard and the results are coming in.
Granted, this isn't a long term solution but for those looking to integrate fast without patching; this method is a quick solution.
Comment #6
poniesChanged the pattern to
/^(?:UA-\d+-\d+|G-\w+)$/to handle both formats.Comment #7
qusai taha commentedPatch #6 has extra ) when applied it, cause unexpected error I fixed it.
Comment #8
kristofferrom#7 allowed me to enter the G-ID, but the status report still throws this warning:
Comment #9
damien laguerre commentedThis new also fixes hook_requirements.
Comment #10
trebormcIt seems that #9 works well.
Comment #11
igonzalez commentedIt seems that #9 works well.
Comment #12
balis_m commentedPatch from #9 works well.
Comment #17
damien laguerre commentedI also corrected the test, the message was not the same anymore.
Comment #18
damien laguerre commentedComment #19
michel.g commentedThanks for the patch, seems to be working well!
Comment #20
alabandit commented#17 Works for me, thanks all!
Comment #21
timcamps commentedPatch #17 is working. Thanks!
Perhaps change the placeholder as well:
src/Form/GoogleAnalyticsAdminSettingsForm.phpLine 79:
change
'#placeholder' => 'UA-',to
'#placeholder' => 'UA- or G-',to rule out any confusion.
Comment #24
greg boggsComment #25
frankdesign commentedPatch at #24 works perfectly in Drupal 9. Thanks
Comment #26
jenlamptonHere's a more recent patch for Drupal 7. It builds on what was provided in #7.
Comment #27
jenlamptonThe D7 patch here does not result in data being tracked in GA. More details on the D7 issue.
Are people using this on D8 and D9 sites seeing data in GA?
Comment #28
nottaken commentedActively trying to use a working patch for D7 and it isn't reporting for a site I'm launching either.
Comment #29
damien laguerre commentedI'm using the patch #17 on 3 sites (2xD8 and 1xD9) and everything is working fine.
Comment #30
shenron_segamag commentedI'm using D8 and data is tracked in GA.
Comment #31
nottaken commentedI figured out what I was doing wrong using patch #9, which wasn't an issue with that patch, but rather I was using a stream id instead of a "MEASUREMENT ID" found on the admin page for Property > Data Streams > Web. The G-XXXXXX formatted ID is found there. I just needed to step back and understand where I needed to look for what ID.
In short, patch #9 works for me on D7.
Comment #32
Anonymous (not verified) commentedJust checking to see when this will be added to a full release.
Thanks!
Comment #33
damien laguerre commentedComment #34
podarok#24 works in Drupal 9.1.4 and google_analytics 3.1.0 https://github.com/ymcatwincities/openy/pull/2400
Comment #35
Augusto182 commented@nottaken how you manage to apply #9 patch in D7 ?
Currently, I apply #7 in D7. Does not work.
Confirm The D7 patch here does not result in data being tracked in GA.
Any help here is really apreciated.
Comment #36
rovoWill this approach work for the scenario of adding GA4 alongside keeping existing Universal Analytics in place?
Add a Google Analytics 4 property (to a site that already has Analytics)
Set up a Google Analytics 4 property alongside your existing Universal Analytics property.
https://support.google.com/analytics/answer/9744165
Comment #37
imclean commentedComment #38
johan den hollander commentedSuccesfully applied and tested the #24 patch.
Comment #39
kburakozdemir commentedSuccesfully applied and tested the #24 patch.
Comment #40
reloxo95 commentedSuccesfully applied and tested the #24 patch.
Comment #41
lordzik commentedHow about a patch for 8.x-2.5 ?
Comment #42
skaughtmay i suggest: skip a version.
when this is released, the module be released it be set to Version 4.0.0 -- keep alignment that this is GA4 js in google_analytics 3. messy (:
Comment #43
gorkagr commented+1 to the patch in #24; working as expected with the latest version of the module in the latest D9 core version.
Comment #44
mautumn commentedPatch #24 working for me with the latest dev release of the moodule on 8.9.14. Thanks very much.
Comment #45
handkerchiefPatch #24 is working, but my composer had problems with formatting.
Comment #46
g_fontaniere commentedPatch #24 i ok with Drupal 9.1.9.
Comment #47
nitheesh commentedPatch from #24 works. +1 RTBC
Comment #49
gloomcheng commentedPatch from #24 works on Drupal 9.1.9. RTBC +1
Comment #50
johnpitcairn commented+1 from me.
@SKAUGHT: Since there is not even a 4.x branch open yet, I think adding more dev/alpha/beta iterations and testing time, and then forcing all installations wanting GA4 compatibility into a major version module upgrade including who knows what other changes, seems rather a lot to ask just for the sake of an ideological position that the version numbers should align ;-)
Comment #51
japerryThere are actually more google properties that have to be added, per google:
"The property ID may represent measurement for different Google products, including a Universal Analytics property ("UA-XXXXXXXX"), a Google Analytics 4 property ("G-XXXXXXXX"), Google Ads ("AW-XXXXXXXX") or Floodlight ("DC-XXXXXXXX")."
It also needs to support multiple IDs. while that can be a separate issue, marking this as needs work for now.
Comment #54
xurizaemonUpdated to accept above property ID list thanks @japerry! (:wave: hi @mikedance!!)
- https://support.google.com/analytics/answer/9310895?hl=en
- https://support.google.com/searchads/answer/2478311?hl=en
The Google docs linked above don't seem clear on whether the IDs follow strict formats - I know GA / UA properties have UA-123456-01 site IDs on the end, but the docs just say UA-XXXXXXX per the above. I think it makes sense to accept anything that matches the prefix. This isn't a credit card number :)
MR !3 uses
/^(?:(?:UA|G|AW|DC)-[\w-]+|)$- this is looser than before, it wouldn't identify that UA-1234567 lacks a site ID, but I think a sufficient validation to tell whether a property ID has been copied into place.Rather than have the regex appear in multiple places, I've consolidated into
_google_analytics_valid_property_id()(similar pattern to_google_analytics_visibility_pages()). That would be easy to refactor into a service if that's deemed worthwhile, but feels unnecessary here & now.Comment #55
amanp commentedMR !3 patch successfully applied and tested in Drupal 8.9.16 and google_analytics 3.1.0
Comment #56
William Aubert commentedPatch #24 is ok with Drupal 9.2
Comment #57
xurizaemon@William Aubert patch 24 does not account for #51, which is why merge request !3 is submitted
Patch URL for that: https://git.drupalcode.org/project/google_analytics/-/merge_requests/3.diff
(This is using the new Gitlab Merge Request interface; links there appear below the patches table.)
Comment #58
pere orgaFor some reason, GitLab is broken at this moment (Unable to load the merge request widget. Try reloading the page.)
I fail to see why we need to add support for IDs such as DC-XXXXXXX or AW-XXXXXXX in this module, but if that is the case, I understand the change should be:
Not changing the status (it is "Needs work" already).
Comment #59
xurizaemonFair enough. In fact Google refer to it in https://support.google.com/analytics/answer/9310895?hl=en as
So maybe with GAv4 we will see it called "Measurement ID" ... but I'm just going to take your suggestion and go with "property ID" for now :)
Pere if you know more about the other property IDs (ie, whether they are or aren't compatible with this module), then I'd appreciate your thoughts! My situation was only that a colleague needed a re-rolled patch for this and I saw an opportunity to address the issue raised by @japerry, but I don't have a boat in the race either way.
It'd be great if someone using those other products mentioned in #3178179-51: Support new Google Analytics 4 can test this out and give feedback.
Comment #60
japerryThere is now a 4.x branch that covers the new Google Analytics 4 gtag. Please test the dev branch and see how it works for you! Probably will end up spawning new issues to tackle specific things related to the new version.
Note, the 4.x branch will be built to replace both the 8.x-2.x and 8.x-3.x branches. Once a 4.x release is made, the only reason someone should need the old versions is for unsupported versions of Drupal 8 (8.8 and lower)
You can composer in the 4.x version here until there is a alpha or beta release.
https://www.drupal.org/project/google_analytics/releases/4.x-dev
I have a call with the GA devs on Thursday to go over the module, and spawn the issues that need to be tackled for full 4.x release.
Comment #61
orodicio commentedHi:
I've tried 4.x-dev version is not compatible with drupal 9.
Comment #62
xurizaemon@orodicio suggest to check if there's an existing issue reporting the symptoms you saw; if not then it's helpful to open a new issue and show details (eg command and error output) of the incompatibility you observed.
The release co-ordination issue for the 4.x release is #3219719: Plan for 4.x Branch of Google Analytics and 4.x issues should ideally be associated with that using the "Related issues" field. This issue relates to testing the new Google Analytics property identifiers.
@japerry there are ~80K sites using 8.x-2.x and 8.x-3.x currently - these might want consideration if issues (esp bugs) are moved to 4.x before there's a release of that branch ready for review. Less pressing for a feature issue that will only affect sites with the new property IDs I'm sure.
Comment #63
aneida commentedAfter running
composer require 'drupal/google_analytics:4.x-dev@dev'I get
- Syncing drupal/google_analytics (dev-4.x 5deae36) into cache
Comment #64
jukka792 commentedIs it possible to run this new Google Analytics 4 module with the old module installed simultaneously?
It would be usefull to be able to have both google analytics tags running for one year to get enough data to be able to compare are they similar etc.
Like here for example, they say it is good practice to have both analytics installed during this transition period:
https://www.dreweastmead.com/blog/collect-both-google-analytics-4-univer...
Comment #65
japerry"Is it possible to run this new Google Analytics 4 module with the old module installed simultaneously?"
The quick answer to this is no. The alternative is that the 4.x module will support multiple accounts, which will allow you to run multiple tags within the same module. This also means that 2.x and 3.x should be able to be EOL. According to Google, the gtag system should have full backwards compatibility with the UA system that exists in the 2.x branch.
Comment #66
japerryCan you give any more details here? I'm not seeing anything in upgrade_status or on my Drupal 9 site to show its not d9 compatible. It definitely should be. I wonder if its related to some composer cache issue? The error seen above using composer is not fetching the correct branch. You can see the correct one here:
https://git.drupalcode.org/project/google_analytics/-/tree/4.x
Comment #67
xurizaemon@japerry we could enable tests for 4.x / D9 to see? https://www.drupal.org/node/49388/qa
Comment #68
chikeOn 9.2.5, 4.x-dev is working.
Trying to install on 8.9.18 I got this error,
Error: Class 'Drupal\Component\EventDispatcher\Event' not found in include() (line 11 of /web/modules/contrib/google_analytics/src/Event/GoogleAnalyticsConfigEvent.php)So I rolled back the D8 site to 8.x-3.1 and applied MR !3 patch successfully.
Comment #69
japerryFixed the issues raised in #68, it should work both on Drupal 8.9 and 9.2+
Comment #70
japerryWith help from the Google Analytics team, I'm happy to announce the first release of the 4.0 version of the module! You can get it here:
https://www.drupal.org/project/google_analytics/releases/4.0.0-alpha1
For now, marking this issue as closed. Specific issues with the 4.x branch should be made in new issues.
Comment #71
chikeSure, 4.0.0-alpha1 is working on 8.9.19 and 9.2.6.
Comment #73
damienmckennaCould someone please update the project page to make it clear which version(s) support GA4? Thank you.
Comment #74
ronlee commentedUA is being depreciated in 2023. We still don't have a D7 fix.
Comment #75
vinmassaro commentedComment #76
Webbeh#73 is now an issue: #3276981: Update project page details for GA4-compatibility
I've updated #74's issue as well for our Drupal 7 folks: #3276873: Support new Google Analytics 4
Thanks for the follow-ups, y'all.
Comment #77
xem8vfdh commentedCan someone explain the current state of this issue? There is a (non-alpha, non-beta) 4.0.0 branch. The Release Notes say:
The 4.0.0 version is still showing as Yellow (whatever that means), and I don't believe it is the recommended release, despite January 2022 having passed months ago.
Are people wanting to use GA4 advised to move to the 4.0.0 branch?
Comment #78
rviner commentedI am also interested to know the answer to #77 as there appears to be a lot more focus towards GA4 now?
Comment #79
dustinyoder commentedI would also like to put in a request for a D7 update to support GA4 because UA is going to stop tracking new traffic July 2023. Might be wrong about that date, but it's sometime in the middle of 2023. And there are a massive amound of D7 sites still out there.