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

Added 404 support #619

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Noam-Alum
Copy link
Contributor

@Noam-Alum Noam-Alum commented Aug 23, 2024

Unlike this issue AlmaLinux/wiki#467 for the wiki, it was much simpler to implement this since the script tag for matomo tracking was present in the footer layout partial and it was not used on the 404 page layout.

image

As you can see it was as simple as inserting the tracking script tag and adding this line:

_paq.push(['setDocumentTitle',  '404/URL = ' +  encodeURIComponent(document.location.pathname+document.location.search) + ' /From = ' + encodeURIComponent(document.referrer)]);

image

As said in this guide.


Let me know if this works.

Referring to this issue - #603.

@codyro
Copy link
Member

codyro commented Aug 23, 2024

The footer partial is still loaded when the layouts/404.html file is loaded. This results in two instances of the tracking code being on the page (with one modifying the title):

out

I wonder if this creates two tracking events in Matomo? Looking at the documentation[1], it seems like you can push the setDocumentTitle setting to the _paq array without re-declaring everything (I did not test this).

[1] https://developer.matomo.org/guides/tracking-javascript-guide

@Noam-Alum
Copy link
Contributor Author

Noam-Alum commented Aug 23, 2024

The footer partial is still loaded when the layouts/404.html file is loaded. This results in two instances of the tracking code being on the page (with one modifying the title):

out

I wonder if this creates two tracking events in Matomo? Looking at the documentation[1], it seems like you can push the setDocumentTitle setting to the _paq array without re-declaring everything (I did not test this).

[1] https://developer.matomo.org/guides/tracking-javascript-guide

From what I'm getting you'd have to use a separate tracking event for the 404 page (Since you can't really check if the page you're on is 404 from JavaScript unless you use something like fetch), I didn't realize the footer partial is loaded 🤔 I don't see any reference for it in the 404 page layout.

I've sent a PR for the wikis 404 tracking it has the same boundaries regarding a separate tracking instance, I've written there in more detail.

I'll check how I can bypass that on Sunday.

@codyro
Copy link
Member

codyro commented Aug 25, 2024

From what I'm getting you'd have to use a separate tracking event for the 404 page (Since you can't really check if the page you're on is 404 from JavaScript unless you use something like fetch), I didn't realize the footer partial is loaded 🤔 I don't see any reference for it in the 404 page layout.

You wouldn't need a separate tracking event. They're leveraging setDocumentTitle to make the title of the 404 page in a standard format so you can easily find and identify it within Matomo.

Pseudo-Example of what the snippet does to the title when reporting it to Matomo:

404/URL = /non-existent-page/From = https%3A%2F%2Falmalinux.org%2Fsome-page

Then, you'd be able to quickly view, filter, and identify the page causing a 404 and where it's coming from inside Matomo (Behavior > Pages Titles).

I think you can push the _paq changes on the 404 page (without another tracking snippet). This would be in line with the documentation referenced above:

You can push to the _paq array even after the matomo.js file has been loaded and run.

I haven't tested this, but if that works, it should be as easy as removing the tracking portion you added but keeping the setDocumentTitle part.

@Noam-Alum
Copy link
Contributor Author

From what I'm getting you'd have to use a separate tracking event for the 404 page (Since you can't really check if the page you're on is 404 from JavaScript unless you use something like fetch), I didn't realize the footer partial is loaded 🤔 I don't see any reference for it in the 404 page layout.

You wouldn't need a separate tracking event. They're leveraging setDocumentTitle to make the title of the 404 page in a standard format so you can easily find and identify it within Matomo.

Pseudo-Example of what the snippet does to the title when reporting it to Matomo:

404/URL = /non-existent-page/From = https%3A%2F%2Falmalinux.org%2Fsome-page

Then, you'd be able to quickly view, filter, and identify the page causing a 404 and where it's coming from inside Matomo (Behavior > Pages Titles).

I think you can push the _paq changes on the 404 page (without another tracking snippet). This would be in line with the documentation referenced above:

You can push to the _paq array even after the matomo.js file has been loaded and run.

I haven't tested this, but if that works, it should be as easy as removing the tracking portion you added but keeping the setDocumentTitle part.

So I just insert a script tag with this line?

_paq.push(['setDocumentTitle',  '404/URL = ' +  encodeURIComponent(document.location.pathname+document.location.search) + ' /From = ' + encodeURIComponent(document.referrer)]);

From my understanding this would have to be located before _paq.push(['trackPageView']);.

image

And it is already called in the footer layout partial, that's why I created a new tracking event with the new document title in the first place (When I thought it wasn't tracking at all).

I think I'm missing something... 🙃

At first, I wanted to detect a 404 status code and then change the document title in the tracking event dynamically (this proved to be not the best idea) but it seems better to just use the 404 page template with a modified tracking event (or script tag) 🤷‍♂️ let me know what you think we should do.

@codyro
Copy link
Member

codyro commented Sep 8, 2024

Give something like this a try. You can see what's being sent to Matomo by viewing your network inspector.

master...codyro:almalinux.org:matomo-404-test

image

References

@bennyvasquez bennyvasquez added the enhancement New feature or request label Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants