Skip to content

Commit

Permalink
Merge pull request #318 from poap-xyz/release/v1.13.24
Browse files Browse the repository at this point in the history
Release v1.13.24
  • Loading branch information
jm42 authored May 28, 2024
2 parents 40cbd97 + ec6bb92 commit f73721a
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 32 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/poap-family",
"version": "1.13.23",
"version": "1.13.24",
"author": {
"name": "POAP",
"url": "https://poap.xyz"
Expand All @@ -24,7 +24,6 @@
"dayjs": "^1.11.11",
"downloadjs": "^1.4.7",
"iconoir-react": "^7.7.0",
"matomo-react": "^1.0.7",
"numbro": "^2.5.0",
"prop-types": "^15.8.1",
"react": "^18.3.1",
Expand Down
5 changes: 2 additions & 3 deletions src/app/layout/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ export default function Root() {
href: window.location.href,
documentTitle: title,
})
enableLinkTracking()
},
[title, trackPageView]
[title, trackPageView, enableLinkTracking]
)

enableLinkTracking()

if (fetcher.state === 'loading' || navigation.state === 'loading') {
return (
<CenterPage>
Expand Down
159 changes: 137 additions & 22 deletions src/stores/analytics.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,156 @@
import PropTypes from 'prop-types'
import { createInstance, MatomoProvider, useMatomo } from 'matomo-react'
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
} from 'react'

const matomoHost = process.env.REACT_APP_MATOMO_HOST
const matomoSiteId = process.env.REACT_APP_MATOMO_SITE_ID
const MATOMO_HOST = process.env.REACT_APP_MATOMO_HOST
const MATOMO_SITE_ID = process.env.REACT_APP_MATOMO_SITE_ID
? parseInt(process.env.REACT_APP_MATOMO_SITE_ID)
: undefined

const matomo = matomoHost && matomoSiteId
? createInstance({
siteId: matomoSiteId,
urlBase: `https://${matomoHost}`,
srcUrl: `https://cdn.matomo.cloud/${matomoHost}/matomo.js`,
disabled: false,
linkTracking: false,
configurations: {
disableCookies: true,
},
})
: undefined
/**
* @param {ReadonlyArray<string | number | boolean>} values
*/
function push(values) {
if ('_paq' in window) {
console.debug(values)
// @ts-ignore
window._paq.push(values)
}
}

/**
* @param {string} host
* @param {number} siteId
*/
function load(host, siteId) {
if ('_paq' in window) {
return
}

console.debug('loading analytics')

let tracker = document.createElement('script')
tracker.type = 'text/javascript'
tracker.src = `https://cdn.matomo.cloud/${host}/matomo.js`
tracker.async = true
tracker.defer = true

// @ts-ignore
window._paq = []

push(['setTrackerUrl', `https://${host}/matomo.php`])
push(['setSiteId', siteId])

const firstScript = document.getElementsByTagName('script')[0]
firstScript.parentNode.insertBefore(tracker, firstScript)
}

const AnalyticsContext = createContext({
trackLink:
/**
* @param {{ href: string; linkType: 'download' | 'link' }} params
*/
(params) => {},
trackSiteSearch:
/**
* @param {{ category: string; keyword: string; count: number }} params
*/
(params) => {},
trackPageView:
/**
* @param {{ href: string; documentTitle: string }} params
*/
(params) => {},
enableLinkTracking: () => {},
})

export const useAnalytics = () => useMatomo()
export const useAnalytics = () => useContext(AnalyticsContext)

/**
* @param {PropTypes.InferProps<AnalyticsProvider.propTypes>} props
*/
export function AnalyticsProvider({ children }) {
if (!matomo) {
return children
}
export function AnalyticsProvider({
children,
matomoHost = MATOMO_HOST,
matomoSiteId = MATOMO_SITE_ID,
}) {
useEffect(
() => {
if (matomoHost && matomoSiteId) {
load(matomoHost, matomoSiteId)
}

push(['disableCookies', true])
push(['enableHeartBeatTimer', 15])
},
[] // eslint-disable-line react-hooks/exhaustive-deps
)

const trackLink = useCallback(
/**
* @param {{ href: string; linkType: 'download' | 'link' }} params
*/
(params) => {
push(['trackLink', params.href, params.linkType])
},
[]
)

const trackSiteSearch = useCallback(
/**
* @param {{ keyword: string; category: string; count: number }} params
*/
(params) => {
push(['trackSiteSearch', params.keyword, params.category, params.count])
},
[]
)

const trackPageView = useCallback(
/**
* @param {{ href: string; documentTitle: string }} params
*/
(params) => {
push(['setCustomUrl', params.href])
push(['setDocumentTitle', params.documentTitle])
push(['trackPageView'])
},
[]
)

const enableLinkTracking = useCallback(
() => {
push(['enableLinkTracking', true])
},
[]
)

const value = useMemo(() => ({
trackLink,
trackSiteSearch,
trackPageView,
enableLinkTracking,
}), [
trackLink,
trackSiteSearch,
trackPageView,
enableLinkTracking,
])

return (
<MatomoProvider value={matomo}>
<AnalyticsContext.Provider value={value}>
{children}
</MatomoProvider>
</AnalyticsContext.Provider>
)
}

AnalyticsProvider.propTypes = {
children: PropTypes.node.isRequired,
matomoHost: PropTypes.string,
matomoSiteId: PropTypes.number,
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6449,11 +6449,6 @@ [email protected]:
dependencies:
tmpl "1.0.5"

matomo-react@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/matomo-react/-/matomo-react-1.0.7.tgz#454e1457ee306bee4f5a25173ab2fc95c55e4e21"
integrity sha512-R7K0AxemueBMrjNf8JEcx0HD1rFvRP1AyEunRcjodlm7xCNwBmZzsPGmT3L+30XTZSrk3o58q2akAFgdoMXYmw==

[email protected]:
version "2.0.14"
resolved "https://registry.yarnpkg.com/mdn-data/-/mdn-data-2.0.14.tgz#7113fc4281917d63ce29b43446f701e68c25ba50"
Expand Down

0 comments on commit f73721a

Please sign in to comment.