From 15de8c0020524d9482e31b66e398678ad1c2115e Mon Sep 17 00:00:00 2001
From: Dominik Hebeler <dominik@suma-ev.de>
Date: Mon, 15 Jul 2024 16:25:24 +0200
Subject: [PATCH] disable statistics for integration tests

---
 .gitlab/ci/integrationtest.yml     |  1 +
 metager/resources/js/statistics.js | 29 +++++++++++++++++------------
 2 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/.gitlab/ci/integrationtest.yml b/.gitlab/ci/integrationtest.yml
index 95a57233d..9c1cc9887 100644
--- a/.gitlab/ci/integrationtest.yml
+++ b/.gitlab/ci/integrationtest.yml
@@ -10,6 +10,7 @@ integrationtest:
     BRANCH_NAME: $CI_COMMIT_REF_NAME
     COMMIT_NAME: $CI_COMMIT_REF_SLUG
     SELENIUM_HOST: selenium-standalone-firefox
+    MATOMO_ENABLED: false
   dependencies: [] # No artifacts required
   before_script:
     - export COMPOSER_HOME=$(pwd)/metager/.composer
diff --git a/metager/resources/js/statistics.js b/metager/resources/js/statistics.js
index 1c6ae1eb1..308957a27 100644
--- a/metager/resources/js/statistics.js
+++ b/metager/resources/js/statistics.js
@@ -4,6 +4,7 @@
  */
 class Statistics {
     #load_complete = false;
+    #load_time = new Date();
 
     constructor() {
         let performance = window.performance.getEntriesByType('navigation')[0];
@@ -14,7 +15,6 @@ class Statistics {
                 let readyStateCheckInterval = setInterval(() => {
                     performance = window.performance.getEntriesByType('navigation')[0];
                     if (performance.loadEventEnd == 0) return;
-                    console.log("load end");
                     clearInterval(readyStateCheckInterval);
                     this.#init();
                 }, 100);
@@ -23,29 +23,34 @@ class Statistics {
     }
 
     #init() {
-        this.pageLoad();
+        setTimeout(this.pageLoad.bind(this), 60000);
         document.querySelectorAll("a").forEach(anchor => {
-            anchor.addEventListener("click", e => this.pageLeave(e.target.href).bind(this));
+            anchor.addEventListener("click", e => this.pageLeave(e.target.closest("a").href));
         });
     }
 
     pageLeave(target) {
         let params = {};
-        params.url = target;
-        params.link = target;
+
         try {
+            this.pageLoad();    // Make sure to track the initial page load
             let url = new URL(target);
-            if (url.host == document.location.host) return;
-            this.pageLoad(params);
-            navigator.sendBeacon("/stats/pl", new URLSearchParams(params));
-        } catch (error) { }
+            if (url.host != document.location.host) {
+                params.link = target;
+                params.url = target;
+                this.pageLoad(params);
+            }
+
+        } catch (error) { console.error(error) }
     }
 
     pageLoad(overwrite_params = {}) {
-        if (this.#load_complete) return;
-        this.#load_complete = true;
-
         let params = {};
+        if (this.#load_complete && !overwrite_params.hasOwnProperty("link")) return;
+        if (!this.#load_complete) {
+            params.cdt = this.#load_time.getTime();
+            this.#load_complete = true;
+        }
 
         // Page performance
         try {
-- 
GitLab