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

feat: respect astros config.site configuration #7

Merged
merged 2 commits into from
Oct 2, 2022
Merged
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import matomo from '@jop-software/astro-matomo';

export default defineConfig({
integrations: [
// We only track site views when this matches the host the user is on.
// If the configuration is empty, every page view gets tracked.
site: "https://example.com",
matomo({
baseUrl: "https://analytics.example.com/",
siteId: 1
Expand All @@ -21,4 +24,4 @@ export default defineConfig({
});
```

<div align=center>&copy 2022, <a href="https://jop-software.de">jop-software Inh. Johannes Przymusinski</a></div>
<div align=center>&copy 2022, <a href="https://jop-software.de">jop-software Inh. Johannes Przymusinski</a></div>
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@ const createPlugin = (options) => {
name: "@jop-software/astro-matomo",
hooks: {
"astro:config:setup": async ({ config, injectScript }) => {
/**
* @prop {string} host - The URL to compare window.location.host to, to determine weather we should track the user or not.
*/
const matomo = {
host: "",
};

if (config.site) {
let url = new URL(config.site);
matomo.host = url.host;
}

injectScript(
"page",
`import { init } from '@jop-software/astro-matomo/matomo.js'; init('${options.baseUrl}', ${options.siteId});`
`import { init } from '@jop-software/astro-matomo/matomo.js'; window.matomo = ${JSON.stringify(
matomo
)}; init('${options.baseUrl}', ${options.siteId});`
);
},
},
Expand Down
11 changes: 11 additions & 0 deletions src/matomo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
export function init(baseUrl, siteId) {
// Skip tracking when either window.matomo.host is emptry or it does not match the current host.
if (
window.matomo.host !== "" &&
window.location.host !== window.matomo.host
) {
console.info(
`[@jop-software/astro-matomo]: Skip tracking because the host does not match.`
);
return;
}

var _paq = (window._paq = window._paq || []);
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["trackPageView"]);
Expand Down