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

Adding ping=1 tracker query param to allow JS tracker to update the end of visit time for more accurate visit length #8069

Merged
merged 16 commits into from Jun 25, 2015

Conversation

diosmosis
Copy link
Member

Includes JavaScript tracker addition that sends a 'ping=1' request to Piwik every N seconds for a more accurate visit time, and the Piwik tracker code to handle those requests.

Changes to the JavaScript Tracker

Modified existing 'heart beat' code so it would be more efficient and more accurate. The system as it functions now is as follows:

  • the setHeartBeatTimer() method is used to configure the heart beat interval.
  • a page is tracked, which sets the first heart beat timeout
  • after N seconds pass, the timeout executes. It checks if there has been non-ping tracker activity in the last N seconds, and if not, sends a ping request and schedules a timeout for the next N seconds. If there has been activity, the next timeout is scheduled to be executed N seconds from the last request.

If a window loses focus, the timeout is cleared. When the window gains focus, the timeout is reinstated.

Changes to Piwik tracking code

The change to the tracker is simple, if a ping request is sent and the visit is not new, the action is set to null and only the visit is updated.

Other Changes

  • I added the ability to issue ping requests to the PHP tracker mainly for testing here: https://github.com/piwik/piwik-php-tracker/tree/ping_addition Must be merged before/after this is merged.
  • I added the Q promise library for the JS tracker tests. It allowed me to cleanly test the heartbeat code. Without it, the timeout could not be tested, since the JS tests used a wait() function that spun (so the event queue never advanced).

Manual Testing Strategy

For each browser:

  1. start watching visitor log
  2. visit page (assert visitor log has 1 action)
  3. do nothing + wait for 10 secs (assert visitor log has one action, but longer visit length)
  4. do nothing + wait for 10 secs (same assert)
  5. switch tab, wait for 10 secs (assert no change)
  6. wait 10 secs, switch back to tab (assert visitor log has one action, but longer visit length)
  7. do nothing + wait for 10 secs (assert visitor log has one action, but longer visit length)

TODO:

  • piwik.js code that activates ping if there is activity on the page and N secs have passed since the last tracker request
  • comprehensive piwik.js tests for ping
  • modify changelog
  • manual tests on as many browsers as possible
    • Chrome
    • Firefox
    • Opera
    • Safari (Mac)
    • Safari (iPhone)
    • IE 7/8/9/??

Refs #2041

@diosmosis diosmosis added Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc. c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. Pull Request WIP Indicates the current pull request is still work in progress and not ready yet for a review. labels Jun 9, 2015
@diosmosis diosmosis self-assigned this Jun 9, 2015
@diosmosis diosmosis added this to the 2.14.0 milestone Jun 9, 2015
@diosmosis diosmosis force-pushed the 2041_visitor_ping branch 6 times, most recently from 7f1ef90 to 261847e Compare June 13, 2015 05:06
@diosmosis diosmosis added Needs Review PRs that need a code review and removed Pull Request WIP Indicates the current pull request is still work in progress and not ready yet for a review. labels Jun 16, 2015
@diosmosis
Copy link
Member Author

Still have to do manual tests & get JSLint test to pass, but this is ready to be reviewed. Will update the description w/ more info soon, but if you want to review now, going commit by commit will help, as I tried to make the commit messages descriptive.

*/
setHeartBeatTimer: function (minimumVisitLength, heartBeatDelay) {
var now = new Date();
setHeartBeatTimer: function (heartBeatDelay) {
Copy link
Member

Choose a reason for hiding this comment

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

Does this break API? Removing the first parameter? I reckon it was not much used before? Should we mention it in changelog? Or maybe we do already? :)

Copy link
Member Author

Choose a reason for hiding this comment

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

It might break code that uses it, however, it didn't actually do anything before. It was incomplete, just half the feature. It sent useless requests, probably creating new actions, no one should be using it.

Copy link
Member

Choose a reason for hiding this comment

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

Probably not everyone likes it, I'd personally name it heartBeatDelayInSeconds and also mention it in doc block that it is seconds since one is used to deal with ms eg in setTimeout etc.

@tsteur
Copy link
Member

tsteur commented Jun 23, 2015

I'll have another look tomorrow and not only have a look at the diff... so far looks like a nice, small addition

heartBeatDown();
});

heartBeatUp();
Copy link
Member

Choose a reason for hiding this comment

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

When looking at the code I found it a bit confusing that setUpHeartBeat() does also directly sends or sets a timeout for heartBeatUp although it's convenient. I'd maybe remove it from there and call it separately but up to you

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't really understand this, setUpHeartBeat() sets up events and the first timeout, but the timeout is set via heartBeatUp(). heartBeatUp() is called in many places, including in heartBeatUp itself, because the timeout does not always exist even if the heart beat is enabled.

I will probably merge w/o this change, but if you want to clarify we can talk on slack.

@tsteur
Copy link
Member

tsteur commented Jun 23, 2015

FYI: The minified piwik.js version needs to be created

diosmosis added 12 commits June 25, 2015 04:13
…s misleading. Though it is initially set to the start time of a visit's last action, it is supposed to represent the visit's actual end time.
… is more performant and executes much less code on user websites.

New piwik.js tests included as well as an additional utility to make writing future tests easier (use jquery to parse tracker requests that are returned from fetchTrackedRequests). However tests don't currently pass since qunit is not super helpful for testing async code.
…me, since we should ping N seconds after last ping also. And if tracking request is sent before heart beat timer is configured, setup heart beat when configuring heart beat timer.
…nableHeartBeatTimer, rename clearHeartBeat to disableHeartBeatTimer, make sure the timer will be set up after any tracker request, not just page view.
…doesn't appear in minified piwik.js. And set last tracker request time in makeSureThereIsAGapAfterFirstTrackingRequestToPreventMultipleVisitorCreation for increased code clarity + so it will be set when bulk requests sent.
diosmosis added a commit that referenced this pull request Jun 25, 2015
Adding ping=1 tracker query param to allow JS tracker to update the end of visit time for more accurate visit length.
@diosmosis diosmosis merged commit ea8935d into master Jun 25, 2015
@diosmosis diosmosis deleted the 2041_visitor_ping branch June 25, 2015 12:12
@diosmosis
Copy link
Member Author

Moved the interval changing code to here: #8213

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc. Needs Review PRs that need a code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants