From 78441472b31099b614d13dd249541efa5f09a44d Mon Sep 17 00:00:00 2001 From: Sam Richards Date: Mon, 27 Jul 2020 15:16:31 -0700 Subject: [PATCH] Add trackCustomEvent [Fixes #34] --- src/index.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 172f1ae..dc4068a 100644 --- a/src/index.js +++ b/src/index.js @@ -1 +1,25 @@ -// noop +/** + * Create custom Matomo event. + * + * @see https://matomo.org/docs/event-tracking/ + */ +export const trackCustomEvent = ({ + eventCategory, + eventAction, + eventName, + eventValue +}) => { + if (process.env.NODE_ENV === 'production' || window.dev === true) { + if (!window._paq) return + + const { _paq, dev } = window + + _paq.push([`trackEvent`, eventCategory, eventAction, eventName, eventValue]) + + if (dev) { + console.debug( + `[Matomo] event tracked, category: ${eventCategory}, action: ${eventAction}, name: ${eventName}, value: ${eventValue}` + ) + } + } +}