Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add useMatomo hook so we can use this in all apps moving forward #376

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"editor.codeActionsOnSave": {
"source.organizeImports": false,
"source.fixAll.eslint": true,
},
"source.organizeImports": "always",
"source.fixAll.eslint": "always"
},
"editor.formatOnSave": false,
"eslint.format.enable": false,
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
Expand Down
75 changes: 75 additions & 0 deletions packages/react-components-lab/src/hooks/useMatomo/useMatomo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint-disable no-underscore-dangle */

Check failure on line 1 in packages/react-components-lab/src/hooks/useMatomo/useMatomo.ts

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Requires 'eslint-enable' directive for 'no-underscore-dangle'
/* eslint-disable @typescript-eslint/naming-convention */

Check failure on line 2 in packages/react-components-lab/src/hooks/useMatomo/useMatomo.ts

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Requires 'eslint-enable' directive for '@typescript-eslint/naming-convention'
/* eslint-disable import/prefer-default-export */

Check failure on line 3 in packages/react-components-lab/src/hooks/useMatomo/useMatomo.ts

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Requires 'eslint-enable' directive for 'import/prefer-default-export'
import { useEffect } from 'react';

/**
* Hook to use Matomo tracking, it will load the Matomo script and track events
*
* Requires the following environment variables:
* - REACT_APP_MATOMO_COOKIE_DOMAIN
* - REACT_APP_MATOMO_SITE_ID
*
* Reminder: Remember to update your build script to include the environment variables.
*
* Reminder: Add this to your App.tsx file for initialization.
*
* @returns {Object} trackEvent function
*/
export const useMatomo = () => {
const _window = window as any;

useEffect(() => {
// Matomo is already loaded, do nothing
if (_window._paq) {

Check failure on line 24 in packages/react-components-lab/src/hooks/useMatomo/useMatomo.ts

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Unsafe member access ._paq on an `any` value
return;
}

// eslint-disable-next-line no-multi-assign
const _paq = (_window._paq = _window._paq || []);

Check failure on line 29 in packages/react-components-lab/src/hooks/useMatomo/useMatomo.ts

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Unsafe member access ._paq on an `any` value

Check failure on line 29 in packages/react-components-lab/src/hooks/useMatomo/useMatomo.ts

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Unsafe member access ._paq on an `any` value

const siteId = process.env.REACT_APP_MATOMO_SITE_ID;
const cookieDomain = process.env.REACT_APP_MATOMO_COOKIE_DOMAIN;

if (!cookieDomain || !siteId) {
throw new Error('Missing Matomo configuration');
}

// Matomo configuration
_paq.push(['setCookieDomain', cookieDomain]);

Check failure on line 39 in packages/react-components-lab/src/hooks/useMatomo/useMatomo.ts

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Unsafe member access .push on an `any` value

Check failure on line 39 in packages/react-components-lab/src/hooks/useMatomo/useMatomo.ts

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Unsafe call of an `any` typed value
_paq.push(['trackPageView']);

Check failure on line 40 in packages/react-components-lab/src/hooks/useMatomo/useMatomo.ts

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Unsafe member access .push on an `any` value

Check failure on line 40 in packages/react-components-lab/src/hooks/useMatomo/useMatomo.ts

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Unsafe call of an `any` typed value
_paq.push(['enableLinkTracking']);
_paq.push(['setTrackerUrl', 'https://dhigroup.matomo.cloud/matomo.php']);
_paq.push(['setSiteId', siteId]);

const g = document.createElement('script');
g.async = true;
g.src = 'https://cdn.matomo.cloud/dhigroup.matomo.cloud/matomo.js';

const h = document.getElementsByTagName('head')[0];
h.appendChild(g);
}, []);

const trackEvent = (
category: string,
action: string,
name?: string,
value?: string
) => {
const { _paq } = _window;

if (!_paq) {
return;
}

if (name && value) {
_paq.push(['trackEvent', category, action, name, value]);
} else if (name) {
_paq.push(['trackEvent', category, action, name]);
} else {
_paq.push(['trackEvent', category, action]);
}
};

return { trackEvent };
};
Loading