Skip to content

Commit

Permalink
If a child element is navigated to, observe whether the user scrolls …
Browse files Browse the repository at this point in the history
…to the page top
  • Loading branch information
thekid committed Dec 14, 2024
1 parent 06e7d23 commit de38c3c
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/main/handlebars/journey.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,17 @@ parent: feed
mapping.project(document.querySelector('#map'));
// Update statistics for journey after a little while. To determine which entries the
// user has spent time viewing, use intersection observer
// To determine which entries the user has spent time viewing, use intersection observer
{{&use 'statistics'}}
const statistics = new Statistics();
if (!document.location.hash) {
statistics.schedule(statistics.add('{{journey.slug}}', '{{sign journey.slug}}', 10000));
}
const observer = new IntersectionObserver(
(entries) => {
for (const entry of entries) {
const id = '{{journey.slug}}' + (entry.target.id ? '/' + entry.target.id : '');
if (entry.isIntersecting) {
statistics.schedule('{{journey.slug}}/' + entry.target.id);
statistics.schedule(id);
} else {
statistics.withdraw('{{journey.slug}}/' + entry.target.id);
statistics.withdraw(id);
}
}
},
Expand All @@ -119,6 +115,14 @@ parent: feed
observer.observe(document.querySelector('#{{scroll slug}}'));
statistics.add('{{slug}}', '{{sign slug}}', Math.min({{size images}} * 1500, 5000));
{{/each}}
// If a child element is navigated to, observe whether the user scrolls to the page top
if (document.location.hash) {
statistics.add('{{journey.slug}}', '{{sign journey.slug}}', 500)
observer.observe(document.querySelector('h1'));
} else {
statistics.schedule(statistics.add('{{journey.slug}}', '{{sign journey.slug}}', 10000));
}
</script>
{{/inline}}
{{/layout}}

0 comments on commit de38c3c

Please sign in to comment.