Skip to content

Commit

Permalink
Merge branch 'staging' of https://github.com/huridocs/uwazi into deve…
Browse files Browse the repository at this point in the history
…lopment
  • Loading branch information
Zasa-san committed May 22, 2024
2 parents ba29ed3 + 380b235 commit 9ae21f3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
62 changes: 35 additions & 27 deletions app/react/V2/Components/Analitycs/Matomo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect } from 'react';
/* eslint-disable max-statements */
import { useEffect, useRef } from 'react';
import { useAtomValue } from 'jotai';
import { globalMatomoAtom, settingsAtom } from 'V2/atoms';

Expand Down Expand Up @@ -28,7 +29,7 @@ const buildScript = ({
var url = "${mainUrl}";
_paq.push(["setTrackerUrl", url + "${filename}.php"]);
_paq.push(["setSiteId", "${mainId}"]);
${userMatomoUrl && globalMatomoUrl && `_paq.push(["addTracker", "${userMatomoUrl}", "${userId}"]);`}
${userMatomoUrl && globalMatomoUrl ? `_paq.push(["addTracker", "${userMatomoUrl}matomo.php", "${userId}"]);` : ''}
var d = document,
g = d.createElement("script"),
s = d.getElementsByTagName("script")[0];
Expand All @@ -41,6 +42,7 @@ const buildScript = ({
};

const Matomo = () => {
const scriptIsPresent = useRef(false);
const { matomoConfig } = useAtomValue(settingsAtom);
const globalMatomo = useAtomValue(globalMatomoAtom);
const { id: globalId, url: globalUrl } = globalMatomo || {};
Expand All @@ -54,38 +56,44 @@ const Matomo = () => {
} catch (e) {}

useEffect(() => {
const script = document.createElement('script');
const hasUserMatomo = Boolean(id && url);
const hasGlobalMatomo = Boolean(globalUrl && globalId);
if (!scriptIsPresent.current) {
const script = document.createElement('script');
const hasUserMatomo = Boolean(id && url);
const hasGlobalMatomo = Boolean(globalUrl && globalId);

switch (true) {
case hasGlobalMatomo && !hasUserMatomo:
script.innerHTML = buildScript({ globalUrl, globalId });
break;
let scriptContent = '';

case !hasGlobalMatomo && hasUserMatomo:
script.innerHTML = buildScript({ userUrl: url, userId: id });
break;
switch (true) {
case hasGlobalMatomo && !hasUserMatomo:
scriptContent = buildScript({ globalUrl, globalId });
break;

case hasGlobalMatomo && hasUserMatomo:
script.innerHTML = buildScript({
globalUrl,
globalId,
userUrl: url,
userId: id,
});
break;
case !hasGlobalMatomo && hasUserMatomo:
scriptContent = buildScript({ userUrl: url, userId: id });
break;

default:
break;
}
case hasGlobalMatomo && hasUserMatomo:
scriptContent = buildScript({
globalUrl,
globalId,
userUrl: url,
userId: id,
});
break;

default:
break;
}

if (hasUserMatomo || hasGlobalMatomo) {
document.body.appendChild(script);
if (scriptContent) {
script.innerHTML = scriptContent;
document.body.appendChild(script);
scriptIsPresent.current = true;
}
}
}, [globalId, globalUrl, id, url]);
}, []);

return <div className="hidden" />;
return null;
};

export { Matomo };
4 changes: 2 additions & 2 deletions app/react/V2/Components/Analitycs/specs/Matomo.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('Matomo', () => {
['enableLinkTracking'],
['setTrackerUrl', 'https://global.org/tenant.php'],
['setSiteId', '2'],
['addTracker', 'https://url.org/', '1'],
['addTracker', 'https://url.org/matomo.php', '1'],
]);
});

Expand Down Expand Up @@ -117,7 +117,7 @@ describe('Matomo', () => {
['enableLinkTracking'],
['setTrackerUrl', 'https://global.org/tenant.php'],
['setSiteId', '5'],
['addTracker', 'https://url.org/', '10'],
['addTracker', 'https://url.org/matomo.php', '10'],
]);
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uwazi",
"version": "1.168.0-rc1",
"version": "1.168.0-rc3",
"description": "Uwazi is a free, open-source solution for organising, analysing and publishing your documents.",
"keywords": [
"react"
Expand Down

0 comments on commit 9ae21f3

Please sign in to comment.