[IMP] website_event: improve web tracking

PURPOSE

When opening the event page on the website, there is a service worker in place
that pre-fetches all links in order to be able to work offline.
This commit aims to disable the page tracking for those prefetch requests to
avoid polluting the statistics with "fake" page views.

SPECS

- Add a preparation commit to introduce a "X-Disable-Tracking" header
- Put the header on all requests in the event module that prefetch content

See underlying commit for details.

Task-2476513

closes odoo/odoo#86031

Signed-off-by: Warnon Aurélien (awa) <awa@odoo.com>
This commit is contained in:
Odoo's Mergebot
2022-04-29 08:09:59 +02:00
committed by GitHub
4 changed files with 15 additions and 9 deletions
@@ -444,7 +444,7 @@ class TestOnlineEventPerformance(EventPerformanceCase, UtilPerf):
# website customer data
with freeze_time(self.reference_now):
self.authenticate('user_eventmanager', 'user_eventmanager')
with self.assertQueryCount(default=50): # tef only: 49
with self.assertQueryCount(default=60): # com runbot: 59 - ent runbot:60
self._test_url_open('/event')
@warmup
@@ -452,7 +452,7 @@ class TestOnlineEventPerformance(EventPerformanceCase, UtilPerf):
# website customer data
with freeze_time(self.reference_now):
self.authenticate(None, None)
with self.assertQueryCount(default=30):
with self.assertQueryCount(default=39): # com runbot: 39 - ent runbot: 39
self._test_url_open('/event')
# @warmup
+1 -1
View File
@@ -133,7 +133,7 @@ class Http(models.AbstractModel):
@classmethod
def _register_website_track(cls, response):
if getattr(response, 'status_code', 0) != 200:
if getattr(response, 'status_code', 0) != 200 or request.httprequest.headers.get('X-Disable-Tracking') == '1':
return False
template = False
@@ -2,7 +2,7 @@
<odoo>
<!-- Index -->
<template id="index" name="Events">
<template id="index" name="Events" track="1">
<t t-call="website.layout">
<div id="wrap" class="o_wevent_index">
<!-- Options -->
@@ -236,14 +236,20 @@ const matchCache = async (request) => {
/**
*
* @param {FetchEvent} param0
* @param {Request} request
* @param {object} [options]
* @param {Boolean} [options.disableTracking] whether adding a header preventing the server to track the request
* @returns {Promise<Response>}
*/
const processFetchRequest = async ({ request }) => {
const processFetchRequest = async (request, options) => {
const requestCopy = request.clone();
let response;
try {
response = await fetch(request);
if (options && options.disableTracking) {
response = await fetch(request, { headers: { 'X-Disable-Tracking': '1' } });
} else {
response = await fetch(request);
}
await cacheRequest(request, response);
} catch (requestError) {
if (isCachableRequest(requestCopy)) {
@@ -308,7 +314,7 @@ const prefetchUrls = async (urls = []) => {
continue;
}
try {
await processFetchRequest({ request: new Request(url) });
await processFetchRequest(new Request(url), { disableTracking: true });
} catch (error) {
console.error(`fail to prefetch ${url} : ${error}`);
}
@@ -347,7 +353,7 @@ const processMessage = (data) => {
};
self.addEventListener("fetch", (event) => {
event.respondWith(processFetchRequest(event));
event.respondWith(processFetchRequest(event.request));
});
self.addEventListener("sync", (event) => {