Don't cache javascript snippet if track.userId is enabled

Created on 16 February 2020, about 5 years ago
Updated 6 July 2023, almost 2 years ago

We recently realized after noticing high differences between analytics reports with and without user id enabled, that the id hash is being cached in the page content.

The result is a high number of sessions in the analytics report (ie one session per page viewed), and wrong user counts. This could have privacy implications too as the anonymized id is being leaked to unconcerned users.

Below is the current workaround I'm using, adding user to the cache contexts. It is probably not ideal because afaik it kind of defeats the whole purpose of the cache. I think it should be possible to use placeholder rendering to invalidate only the actual snippet, but it would have to be confirmed by someone with a better understanding of the d8 render array...

piwik / matomo are potentially affected too

--- a/google_analytics.module
+++ b/google_analytics.module
@@ -64,6 +64,7 @@ function google_analytics_page_attachments(array &$page) {
 
   // Add module cache tags.
   $page['#cache']['tags'] = Cache::mergeTags(isset($page['#cache']['tags']) ? $page['#cache']['tags'] : [], $config->getCacheTags());
+  $page['#cache']['contexts'] = Cache::mergeContexts(isset($page['#cache']['contexts']) ? $page['#cache']['contexts'] : [], ['user']);
 
   // Get page http status code for visibility filtering.
   $status = NULL;
πŸ› Bug report
Status

Closed: outdated

Version

2.4

Component

Code

Created by

πŸ‡«πŸ‡·France bendenoz

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States japerry KVUO

    With the sunset of legacy google analytics, the 8.x-2.x module is now unsupported. If this is still an issue with the 4.x version, please file a new issue in the queue.

Production build 0.71.5 2024