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

More efficient heart beat timer #15058

Merged
merged 6 commits into from Nov 5, 2019
Merged

More efficient heart beat timer #15058

merged 6 commits into from Nov 5, 2019

Conversation

tsteur
Copy link
Member

@tsteur tsteur commented Oct 25, 2019

fix #15049

Currently, when the heart beat timer is enabled, we ping every 15s the Matomo server. If you have 1000 visitors on your page that makes 66req/s. It also sends a request every time you focus the window after you were on another tab.

This PR implements pretty much the opposite behaviour:

  • Ping only on window blur (never on focus)
  • On window blur send only a request if the user was on the page for at least 15 seconds (or whatever is configured as heart beat timeout). Otherwise user maybe was just quickly switching between tabs but not actively on the page
  • Ping on unload of browser window but only when user was on page > 15 seconds (or configured heart beat timeout)
  • Stop sending ping requests 30 minutes after the first request (as it would not have any effect anyway)
  • Now requiring at least 5 seconds to be configured as heart beat timer as every second be quite extreme and we want to prevent this.

This means instead of possibly sending heaps of requests every few seconds we typically would maybe send one or two ping requests per pageview. Most of the times it be one (on page unload) or even none (when user was only a few seconds on a page).

Behaviour is otherwise the same, it's just more efficient by not sending as many requests. Instead we only send requests when needed. Which is either when the user is leaving the window, or closes the window.

@tsteur tsteur added not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org. Needs Review For pull requests that need a code review. labels Oct 25, 2019
@tsteur tsteur added this to the 3.13.0 milestone Oct 25, 2019

tracker.disableHeartBeatTimer(); // flatline

return Q.delay(1000); // for the ping request to get sent
return Q.delay(1000); // for a ping request to get sent if there was one
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a test here for when the time goes past setVisitStandardLength()? For example, we set setVisitStandardLength() to 4 or something, then wait 5 seconds and make sure a ping isn't sent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 👍 Added that test and also resolved the merge conflicts.

@diosmosis diosmosis merged commit 73949e5 into 3.x-dev Nov 5, 2019
@diosmosis diosmosis deleted the betterheatbeat branch November 5, 2019 21:58
diosmosis pushed a commit that referenced this pull request Nov 6, 2019
* More efficient heart beat timer

* add changelog

* make sure the event is added for each tracker

* add test

* fix test
@ZupZ3r0
Copy link

ZupZ3r0 commented Jun 26, 2020

Hi,
First of all I appreciate this very effective change!
But did you consider Single Page Applications? Blur or unload may never happen in these cases. What I see currently:

  • open the page initially: heartbeat ping gets fired when the page loses focus (I go to another tab or whatever) -> this works as described here and expected
  • but any pagechanges inside this app (via its navigation) does not trigger pings

Maybe you can ping on hashchange event or even better: provide a method to force ping. This would be helpful for matomo integrations in different frameworks or if we have own (custom)events that we are listening to.

@tsteur
Copy link
Member Author

tsteur commented Jun 26, 2020

@ZupZ3r0 AFAIK this should still work because all it does is update the overall visit time but not anything else.

@tsteur
Copy link
Member Author

tsteur commented Jun 27, 2020

@ZupZ3r0 btw there is also a tracker method named _paq.push(['ping'])

@ZupZ3r0
Copy link

ZupZ3r0 commented Jun 29, 2020

@tsteur apperently it doesn't work out of the box, because the heartbeat sends only the first ping on pageload but never again (or only if you leave the active tab). This doesn't really break something, but results in a visit time of 1s for every action/event.
Anyway thanks for the hint with _paq.push(['ping']), thats what I was looking for and helps in this situation :-)

@tsteur
Copy link
Member Author

tsteur commented Jun 29, 2020

@ZupZ3r0 be great to let me know if using ping changes anything for you. Technically it shouldn't change anything from my understanding because no action/event will be updated in a ping request. Be great to give it a try though.

@miiimooo
Copy link

miiimooo commented Oct 3, 2022

I have been testing for a few days now with sending a ping (_paq.push(['ping'])) but I don't notice any difference in Avg Time on Page measurements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review For pull requests that need a code review. not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

More efficient heart beat timer
4 participants