Skip to content

Commit c085db4

Browse files
Mark JessopMark Jessop
authored andcommitted
Add tile load analytics
1 parent befa9f0 commit c085db4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

js/tracker.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,59 @@ var baseMaps = {
616616

617617
var selectedLayer = "Mapnik";
618618

619+
// Tile load analytics - 2023-01-09
620+
// This code allows us to get some understanding of total tile loads across all users.
621+
var tile_loads = {
622+
"Mapnik": 0,
623+
"DarkMatter": 0,
624+
"WorldImagery": 0,
625+
"Terrain": 0,
626+
"Voyager": 0,
627+
"OpenTopoMap": 0,
628+
}
629+
630+
// Add handlers to eadh tileload event to simply increment a counter.
631+
// We don't need any more data than this.
632+
osm.on('tileload', function() { tile_loads["Mapnik"]++ });
633+
dark_matter.on('tileload', function() { tile_loads["DarkMatter"]++ });
634+
worldimagery.on('tileload', function() { tile_loads["WorldImagery"]++ });
635+
stamen_terrain.on('tileload', function() { tile_loads["Terrain"]++ });
636+
cartodb_voyager.on('tileload', function() { tile_loads["Voyager"]++ });
637+
opentopomap.on('tileload', function() { tile_loads["OpenTopoMap"]++ });
638+
639+
640+
var last_sent_tile_loads = {}
641+
642+
setInterval(function(){
643+
644+
temp_tile_loads = Object.assign({},tile_loads);
645+
646+
// Check if the tile load count has changed.
647+
// Using JSON stringify is a bit of a hack, but appropriate for this kind of job.
648+
if(JSON.stringify(last_sent_tile_loads) == JSON.stringify(temp_tile_loads)){
649+
// Tile loads havent changed, do nothing,
650+
} else {
651+
// Tile loads have changed. Update the store, and send the data.
652+
last_sent_tile_loads = Object.assign({},tile_loads);
653+
654+
// Send!
655+
656+
$.ajax({
657+
type: "PUT",
658+
url: "https://api.v2.sondehub.org/tiles/count",
659+
contentType: "application/json; charset=utf-8",
660+
dataType: "json",
661+
data: JSON.stringify({'client': clientID, 'tile_loads': last_sent_tile_loads}),
662+
});
663+
}
664+
665+
}, 60000)
666+
667+
668+
669+
670+
671+
619672
// set map if in memory
620673
var maplayer = offline.get("map")
621674
if (maplayer !== null) {

0 commit comments

Comments
 (0)