feat: use tracking pixel for article view count (#11559)

This commit is contained in:
Shivam Mishra
2025-05-29 17:01:38 +05:30
committed by GitHub
parent 873cfa08d8
commit b3a76289cc
5 changed files with 80 additions and 3 deletions

View File

@@ -73,6 +73,30 @@ By default, it renders:
<% end %>
</main>
</div>
<% if @article.present? %>
<script>
(function() {
let viewTracked = false;
const trackView = function() {
if (!viewTracked) {
viewTracked = true;
const img = new Image();
img.src = '<%= request.base_url %>/hc/<%= @portal.slug %>/articles/<%= @article.slug %>.png';
}
};
const addTrackingListeners = function() {
const events = ['mouseenter', 'touchstart', 'focus'];
events.forEach(event => {
document.body.addEventListener(event, function() {
setTimeout(trackView, 5000);
}, { once: true });
});
};
addTrackingListeners();
})();
</script>
<% end %>
</body>
<style>
html.dark {