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

Command icon 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

Eric Heydrich created an issue. See original summary.

zach.bimson’s picture

Patch from 8.x-3.x to update the validation

mikedance’s picture

StatusFileSize
new3.67 KB

I 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.

hesco’s picture

The mikedance patch worked for me, as described here:
https://gist.github.com/hesco/162b192c89db129fa1f3f8a6fe9fb05f#file-0200...

_gramur’s picture

As 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.

ponies’s picture

StatusFileSize
new3.61 KB

Changed the pattern to /^(?:UA-\d+-\d+|G-\w+)$/ to handle both formats.

qusai taha’s picture

StatusFileSize
new3.63 KB

Patch #6 has extra ) when applied it, cause unexpected error I fixed it.

kristofferrom’s picture

#7 allowed me to enter the G-ID, but the status report still throws this warning:

Google Analytics module has not been configured yet. Please configure its settings from the Google Analytics settings page.

damien laguerre’s picture

StatusFileSize
new4.44 KB

This new also fixes hook_requirements.

trebormc’s picture

It seems that #9 works well.

igonzalez’s picture

It seems that #9 works well.

balis_m’s picture

Status: Active » Reviewed & tested by the community

Patch from #9 works well.

The last submitted patch, 2: google_analytics-ga4_support-3178179-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

The last submitted patch, 3: ga4-3178179-3.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

The last submitted patch, 7: 3178179-7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: ga4-3178179-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

damien laguerre’s picture

StatusFileSize
new5.36 KB

I also corrected the test, the message was not the same anymore.

damien laguerre’s picture

Status: Needs work » Needs review
michel.g’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the patch, seems to be working well!

alabandit’s picture

#17 Works for me, thanks all!

timcamps’s picture

Patch #17 is working. Thanks!

Perhaps change the placeholder as well:
src/Form/GoogleAnalyticsAdminSettingsForm.php

Line 79:
change '#placeholder' => 'UA-',
to '#placeholder' => 'UA- or G-',

to rule out any confusion.

Greg Boggs made their first commit to this issue’s fork.

greg boggs’s picture

StatusFileSize
new6 KB
frankdesign’s picture

Patch at #24 works perfectly in Drupal 9. Thanks

jenlampton’s picture

StatusFileSize
new5.46 KB

Here's a more recent patch for Drupal 7. It builds on what was provided in #7.

jenlampton’s picture

Status: Reviewed & tested by the community » Needs work

The 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?

nottaken’s picture

Actively trying to use a working patch for D7 and it isn't reporting for a site I'm launching either.

damien laguerre’s picture

I'm using the patch #17 on 3 sites (2xD8 and 1xD9) and everything is working fine.

shenron_segamag’s picture

I'm using D8 and data is tracked in GA.

nottaken’s picture

I 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.

Anonymous’s picture

Just checking to see when this will be added to a full release.

Thanks!

damien laguerre’s picture

Status: Needs work » Reviewed & tested by the community
podarok’s picture

#24 works in Drupal 9.1.4 and google_analytics 3.1.0 https://github.com/ymcatwincities/openy/pull/2400

Augusto182’s picture

@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.

rovo’s picture

Will 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

imclean’s picture

johan den hollander’s picture

Succesfully applied and tested the #24 patch.

kburakozdemir’s picture

Succesfully applied and tested the #24 patch.

reloxo95’s picture

Succesfully applied and tested the #24 patch.

lordzik’s picture

How about a patch for 8.x-2.5 ?

skaught’s picture

may 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 (:

gorkagr’s picture

+1 to the patch in #24; working as expected with the latest version of the module in the latest D9 core version.

mautumn’s picture

Patch #24 working for me with the latest dev release of the moodule on 8.9.14. Thanks very much.

handkerchief’s picture

Patch #24 is working, but my composer had problems with formatting.

g_fontaniere’s picture

Patch #24 i ok with Drupal 9.1.9.

nitheesh’s picture

Patch from #24 works. +1 RTBC

gloomcheng’s picture

Patch from #24 works on Drupal 9.1.9. RTBC +1

johnpitcairn’s picture

+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 ;-)

japerry’s picture

Status: Reviewed & tested by the community » Needs work

There 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.

xurizaemon made their first commit to this issue’s fork.

xurizaemon’s picture

Updated 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.

amanp’s picture

MR !3 patch successfully applied and tested in Drupal 8.9.16 and google_analytics 3.1.0

William Aubert’s picture

Patch #24 is ok with Drupal 9.2

xurizaemon’s picture

@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.)

pere orga’s picture

For some reason, GitLab is broken at this moment (Unable to load the merge request widget. Try reloading the page.)

+    $this->assertRaw($this->t('A valid Google Analytics Web Property ID is case sensitive and formatted like: UA-XXXXXXX, G-XXXXXXX, DC-XXXXXXX, or AW-XXXXXXX.'));

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:

+    $this->assertRaw($this->t('A valid Property ID is case sensitive and formatted like: UA-XXXXXXX, G-XXXXXXX, DC-XXXXXXX, or AW-XXXXXXX.'));

Not changing the status (it is "Needs work" already).

xurizaemon’s picture

Status: Needs work » Needs review

Fair enough. In fact Google refer to it in https://support.google.com/analytics/answer/9310895?hl=en as

The property ID (or Measurement ID, for Google Analytics 4 properties) indicated by "controls" this gtag.js snippet.

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.

japerry’s picture

Version: 8.x-3.x-dev » 4.x-dev

There 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.

orodicio’s picture

Hi:
I've tried 4.x-dev version is not compatible with drupal 9.

xurizaemon’s picture

@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.

aneida’s picture

After running composer require 'drupal/google_analytics:4.x-dev@dev'
I get
- Syncing drupal/google_analytics (dev-4.x 5deae36) into cache

Issue #2977913 by pifagor: Return type does not match
pifagor authored 3 years ago

jukka792’s picture

Is 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...

japerry’s picture

"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.

japerry’s picture

I've tried 4.x-dev version is not compatible with drupal 9.

Can 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

xurizaemon’s picture

@japerry we could enable tests for 4.x / D9 to see? https://www.drupal.org/node/49388/qa

chike’s picture

On 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.

japerry’s picture

Fixed the issues raised in #68, it should work both on Drupal 8.9 and 9.2+

japerry’s picture

Status: Needs review » Fixed

With 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.

chike’s picture

Sure, 4.0.0-alpha1 is working on 8.9.19 and 9.2.6.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

damienmckenna’s picture

Could someone please update the project page to make it clear which version(s) support GA4? Thank you.

ronlee’s picture

UA is being depreciated in 2023. We still don't have a D7 fix.

vinmassaro’s picture

Webbeh’s picture

#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.

xem8vfdh’s picture

Can someone explain the current state of this issue? There is a (non-alpha, non-beta) 4.0.0 branch. The Release Notes say:

While this 4.0.0 release is 'stable', we will wait until Jan 2022 to make it recommended so site developers can test it more widely and give time for sites to migrate from 3.x to 4.x.

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?

rviner’s picture

I am also interested to know the answer to #77 as there appears to be a lot more focus towards GA4 now?

dustinyoder’s picture

I 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.