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

Documentation for next.js 13 app directory? #112

Open
philippkeller opened this issue Sep 16, 2023 · 7 comments
Open

Documentation for next.js 13 app directory? #112

philippkeller opened this issue Sep 16, 2023 · 7 comments

Comments

@philippkeller
Copy link

The docs only cover the pages directory (using _app.jsx).
I want to use it with the new app directory, but I'm lost.

The best I could find was this howto which covers PostHog.

Could anyone help out here?

@philippkeller philippkeller changed the title Documentation for using matomo with next.js 13 app directory? Documentation for next.js 13 app directory? Sep 16, 2023
@eivindml
Copy link

@philippkeller Did you figure this out?

@julian-leahy
Copy link

Also looking for this solution

@revolunet
Copy link
Member

revolunet commented Dec 9, 2023

does this issue help ? #104

This one also #99

@rihards-simanovics
Copy link

need a solution as well 😕

@philippkeller
Copy link
Author

@revolunet yes the code snippet in #104 looks good - where do I add it though? into src/app/layout.jsx ?

@philippkeller
Copy link
Author

philippkeller commented Jan 3, 2024

I got something working, but it's probably far from perfect:

In layout.jsx:

import Matomo from '@/utils/matomo'

then, above the tag add <Matomo />

In /utils.matomo.js:

import Script from "next/script";

const MATOMO_URL = process.env.NEXT_PUBLIC_MATOMO_URL;
const MATOMO_SITE_ID = process.env.NEXT_PUBLIC_MATOMO_SITE_ID;

export default function Matomo() {
    return (
        <Script
            id="matomo"
            strategy="afterInteractive"
            dangerouslySetInnerHTML={{
                __html: `
                var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//${MATOMO_URL}/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '${MATOMO_SITE_ID}']);
    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);
  })();
            `,
            }}
        />
    )
}

And in your .env file:

NEXT_PUBLIC_MATOMO_URL=matomo.mydomain.com
NEXT_PUBLIC_MATOMO_SITE_ID=1

@wscourge
Copy link

wscourge commented Jan 20, 2024

@philippkeller FYI you don't need dangerouslySetInnerHTML={{ __html: ""}} syntax to use the next/script tag.

<Script id="matomo" strategy="afterInteractive">
{`
    var _paq = window._paq = window._paq || [];
    /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
    _paq.push(['trackPageView']);
    _paq.push(['enableLinkTracking']);
    (function() {
    var u="//${MATOMO_URL}/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '${MATOMO_SITE_ID}']);
    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);
    })();
`}
</Script>

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants