-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #694 from Sifchain/staging
hotfix/2.7.7 - add matomo analytics loader
- Loading branch information
Showing
4 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const createMatomoScript = ({ siteId = "", baseUrl = "", urlSuffix = "" }) => ` | ||
var _paq = window._paq = window._paq || []; | ||
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */ | ||
_paq.push(['trackPageView']); | ||
_paq.push(['enableLinkTracking']); | ||
(function() { | ||
var u="${baseUrl.replace(/\/$/, "")}"; | ||
_paq.push(['setTrackerUrl', u+'/${urlSuffix.replace(/^\//, "")}']); | ||
_paq.push(['setSiteId', '${siteId}']); | ||
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; | ||
g.async=true; g.src=u+'/matomo.js'; s.parentNode.insertBefore(g,s); | ||
})(); | ||
`; | ||
|
||
type MinimumProviderConfig = { | ||
url: string; | ||
analytics: { | ||
siteId: string; | ||
baseUrl: string; | ||
urlSuffix: string; | ||
enabled?: boolean; | ||
}; | ||
}; | ||
|
||
const ANALYTICS_SCRIPT_ID = "matomo-script"; | ||
|
||
export default async function loadAnalytics() { | ||
try { | ||
const providers: MinimumProviderConfig[] = await fetch( | ||
`https://registry.sifchain.network/api/providers`, | ||
).then((x) => x.json()); | ||
|
||
const { origin } = window.location; | ||
|
||
const providerConfig = providers.find((x) => x?.url === origin); | ||
|
||
if ( | ||
providerConfig && | ||
(providerConfig.analytics.enabled ?? true) && | ||
!document.getElementById(ANALYTICS_SCRIPT_ID) | ||
) { | ||
const scriptSrc = createMatomoScript(providerConfig.analytics); | ||
const script = document.createElement("script"); | ||
script.id = ANALYTICS_SCRIPT_ID; | ||
script.innerHTML = scriptSrc; | ||
document.head.appendChild(script); | ||
return true; | ||
} | ||
return false; | ||
} catch (error) { | ||
console.warn("failed to load analytics:", error); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "sifchain-ui", | ||
"version": "2.7.6", | ||
"version": "2.7.7", | ||
"private": true, | ||
"license": "UNLICENSED", | ||
"packageManager": "[email protected]", | ||
|
19fd486
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
sifchain-ui – ./
sifchain-ui-git-master-sifchain.vercel.app
dex.sifchain.finance
sifchain-ui-sifchain.vercel.app
sifchain-dex.redstarling.com