Google Analytics 4 Measurement Protocol Wordpress Plugin settings

Google Analytics 4 Measurement Protocol Plugin for WordPress

May 2024: Measuring visits works well

The plugin works as expected. Attribution seems to be working well for all channels (compared to server log records for certain visits etc.).

Some very long sessions still appear, but not much. This can be easily filtered out by excluding session length longer than e.g. 10 minutes, if such cleaned up reporting is required.

Funny thing it the plugin logs all URLs hit (or tried to be served from WP), so even 404s appear, brute force hacking bots hitting weird admin, malware-like and rootkit URLs.

You can have Google Analytics 4 stats without cookies, not losing 50+% data. Anonymized, but still well attributed. Better than some front-end replacements.

Mar 15, 2024: Some issues with very long session lengths

I have identified some issues with very long session lengths reported in Google Analytics 4 for events coming from this server tracking plugin. The session length is set to 30 minutes similar to GA4 length set via admin.

I suspect this might has to do something with a periodicity of WordPress transient options removal. If this removal is not run via cron and it’s run on page view, there might be some old transients hanging around. I am going to monitor and diagnose this further.

Feb 17, 2024: Attribution and reserved events working

Improved GA4 plugin has been running for around 10 days now and attribution seems to be working well. Unassigned channel has been almost eliminated in favor of other channels. What do you think – is it stable enough to be released to WordPress analytics plugins community?

Attribution working - Analytics WordPress plugin for server tracking using Measurement Protocol
Attribution working – Analytics WordPress plugin for server tracking using Measurement Protocol

Since I have reverse engineered the GTAG reporting URL, plugin now successfully sends session_start and first_visit events to Google Analytics 4, even so that Measurement Protocol API disallows you from sending these and reporting errors such as “reserved event”. Well, I have worked around (nonsensical) Measurement Protocol limitations and got it working.

GA4 Measurement Protocol - using reverse engineered GTAG URL to submit session_start and first_visit events
GA4 Measurement Protocol – using reverse engineered GTAG URL to submit session_start and first_visit events

Feb 06, 2024: Hybrid and debug modes (cookie and cookieless tracking)

Second iteration that has been improved and tested now works. It seems to be working well. I have improved attribution even more, normalized some data that are sent into Google Analytics 4.

Since Measurement Protocol does not allow for sending session_start and first_visit events, I had to find a different way. Sending page_view events with campaign_details attribution events wouldn’t work properly as session_start would not exist in GA4. Thus attribution wouldn’t work either.

Google Analytics 4 Measurement Protocol WordPress Plugin settings
Google Analytics 4 Measurement Protocol WordPress Plugin settings

I had to reverse engineer GA4 collect URL protocol. Data from tracking scripts (GTM or GTAG) are usually sent using this URL:

https://region1.google-analytics.com/g/collect?v=2& ...

After reverse engineering a couple of URL parameters (query strings), I am now able to send both session_start and first_visit event to GA4 properties sidestepping Measurement Protocol. Page views are still reported via Measurement Protocol, although at this point the whole Measurement Protocol with its limitations sounds more like a joke. Specially as it can be easily replaced by the /collect URL.

Another interesting thing is that this URL does not require any secrets (as opposed to MP), so it’s possible to spam GA4 properties in a similar fashion as in Universal Analytics Measurement Protocol. Weird.

As you can see from the screenshot, I have added a Hybrid mode tracking where the plugin exits as soon as it detects GA4 cookie as to allow GTM/GTAG to report from frontend. If the cookie does not exist, server tracking is performed.

I have also added a Debug mode to report and possibly debug all requests, although I am quite content with the reporting at the moment.

Reporting from backend now includes emulated operating system named “Server” along with a plugin version (OS version). It might be useful to differentiate reports sent from frontend and backend. You can also manage similar separation by creating separate streams, but that’s probably an overkill.

Let’s do some more testing and debugging before I release it.

Jan 29, 2024: Measurement protocol plugin proof of concept

Very experimental, but first version of the plugin is right here. I have started testing it on my blog.

It’s a very basic plugin to report page_view events to Google Analytics 4 from backend, i.e. directly from a WordPress without any need for Javascript or cookies. It uses Measurement Protocol that is just a simple API to directly send events to GA4 from a server.

It juggles some variables to improve user and session attribution and properly anonymizes both client ID and session ID.

I’ll report later how (well) it works. And once content I’ll release it to WordPress community using Analytics 4.


Have a comment? Join discussion on Mastodon as a reply to: @dusoft@fosstodon.org