Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

fix: medium-severity bugs from the October bug sweep (#1329)

Goldflagcaf1cf7e3583
diff --git a/client/public/countries.json b/client/public/countries.json
index 0c2c556e5..3c53b27ec 100644
--- a/client/public/countries.json
+++ b/client/public/countries.json
@@ -173,7 +173,7 @@
{"type":"Feature","properties":{"ISO_A2":"AW","ADMIN":"Aruba","ISO_A3":"ABW","BORDER":3},"geometry":{"type":"Polygon","coordinates":[[[-69.99693762899994,12.57758209800005],[-70.04873613199993,12.632147528000075],[-70.06110592399995,12.625392971000053],[-70.04873613199993,12.583726304000038],[-70.06037350199995,12.55695221600007],[-70.05809485599991,12.537176825000074],[-70.02765865799995,12.522935289000088],[-69.95856686099995,12.46320221600007],[-69.92467200399994,12.447211005000042],[-69.94513912699995,12.440375067000048],[-69.93053137899994,12.425970770000049],[-69.90880286399994,12.417792059000078],[-69.88809160099993,12.417669989000046],[-69.87682044199994,12.427394924000055],[-69.8801977199999,12.453558661000045],[-69.91576087099992,12.497015692000048],[-69.93639075399994,12.531724351000037],[-69.99693762899994,12.57758209800005]]]}},
{"type":"Feature","properties":{"ISO_A2":"BS","ADMIN":"The Bahamas","ISO_A3":"BHS","BORDER":2},"geometry":{"type":"MultiPolygon","coordinates":[[[[-72.99957214499995,21.45170771100004],[-72.97076639099993,21.469428091000054],[-72.93342899699991,21.473451972000078],[-72.91464979799991,21.485062200000073],[-72.91043831699994,21.504850291000082],[-72.91974850199995,21.514797268000052],[-72.95945733199994,21.533127982000053],[-72.9915258449999,21.56199778900009],[-73.02403723899994,21.532171942000048],[-73.07623436799992,21.506795732000057],[-73.07243081799993,21.486336651000045],[-73.0521743239999,21.447796365000045],[-73.05304575199995,21.42490354000006],[-72.99957214499995,21.45170771100004]]],[[[-73.13914954299992,22.363348700000074],[-73.10130774599992,22.355902411000045],[-73.07371985599991,22.342230536000045],[-73.04954993399991,22.346625067000048],[-73.00991777299993,22.36595286700009],[-72.9883520169999,22.37018463700008],[-72.95702063699991,22.355902411000045],[-72.92292232999995,22.363348700000074],[-72.87999426999994,22.358221747000073],[-72.84373938699991,22.336004950000074],[-72.79723059799994,22.29364655200004],[-72.77944902299993,22.288234768000052],[-72.75088456899994,22.31122467700004],[-72.7461645169999,22.331284898000035],[-72.75499426999994,22.349676825000074],[-72.78331458199995,22.353216864000046],[-72.82046464799993,22.38377513200004],[-72.86168372299994,22.386867580000057],[-72.89281165299991,22.40428294500009],[-72.93993079299992,22.396877346000053],[-72.96373450399994,22.397650458000044],[-73.01520748599995,22.430487372000073],[-73.06314042899993,22.44472890800006],[-73.09089107999995,22.433661200000074],[-73.1145727199999,22.43854401200008],[-73.13170325399994,22.459540106000077],[-73.14224199099993,22.45136139500005],[-73.1406957669999,22.43154531500005],[-73.15151933499993,22.408596096000053],[-73.14537512899994,22.396877346000053],[-73.16600501199991,22.374212958000044],[-73.13914954299992,22.363348700000074]]],[[[-73.46873625299992,22.58766229300005],[-73.47846199399993,22.601163606000057],[-73.52392126799992,22.61619629900008],[-73.53611469799995,22.603462445000048],[-73.4752329449999,22.586170179000078],[-73.46873625299992,22.58766229300005]]],[[[-73.64707081799992,21.061555161000058],[-73.65333525799991,21.039886040000056],[-73.70320851199995,21.019765722000045],[-73.68226118299992,20.98578499100006],[-73.67614103599993,20.93516228200008],[-73.64511012499992,20.912398909000046],[-73.63142089299993,20.936681229000044],[-73.60081946499992,20.946519273000035],[-73.53681058799992,20.94123591400006],[-73.4926142509999,20.924457277000045],[-73.43342037699995,20.937648830000057],[-73.3989574819999,20.935761355000068],[-73.34692155599993,20.96382728000009],[-73.32543493799994,20.961337770000057],[-73.31065833199995,20.950506903000075],[-73.26891028599994,20.958726304000038],[-73.22556430799995,20.952463097000077],[-73.15119381399995,20.982123114000046],[-73.12982146599995,20.993662571000073],[-73.09759423299994,21.081400047000045],[-73.03392986299991,21.156731490000084],[-73.02554277299993,21.18455638200004],[-73.00572669199994,21.291449286000045],[-73.00462805899991,21.31443919500009],[-73.01219641799992,21.33201732000009],[-73.03376217399995,21.340399481000077],[-73.05304928299995,21.329291083000044],[-73.11738404399995,21.23670132300009],[-73.13773470399991,21.19767059800006],[-73.1609611959999,21.16986297300008],[-73.21102369099992,21.158073590000072],[-73.2551523969999,21.135060132000035],[-73.31794350199993,21.154067681000072],[-73.34383419599993,21.16541318500009],[-73.37979081899994,21.208197333000044],[-73.39818274599992,21.22483958500004],[-73.41291256399995,21.205145575000074],[-73.43333899599992,21.19326406500005],[-73.47276770699995,21.192694403000075],[-73.49262555699994,21.18208591700005],[-73.51460433599993,21.18686665300004],[-73.52369848299992,21.168271383000047],[-73.54579357499995,21.149742968000055],[-73.55288739499991,21.13020428300007],[-73.63344490599991,21.099701976000063],[-73.66786106699993,21.104998324000064],[-73.64517348299995,21.080532573000085],[-73.64707081799992,21.061555161000058]]],[[[-74.28376217399995,22.671820380000042],[-74.31200110599991,22.638332424000055],[-74.34764563699991,22.612941799000055],[-74.37669837099992,22.584377346000053],[-74.38556881399995,22.541449286000045],[-74.35472571499992,22.563299872000073],[-74.31753495999993,22.602484442000048],[-74.28892981699994,22.643744208000044],[-74.28376217399995,22.671820380000042]]],[[[-73.99571692599994,22.657538153000075],[-74.01744544199994,22.64549388200004],[-74.03123938699991,22.62368398600006],[-74.01622473899994,22.61033763200004],[-73.98452714799993,22.59560781500005],[-73.91344153599994,22.59446849200009],[-73.88589433499993,22.57558828300006],[-73.87083899599992,22.535305080000057],[-73.8801977199999,22.50291575700004],[-73.90770423099991,22.479437567000048],[-73.9473363919999,22.465765692000048],[-73.98692786399994,22.457505601000037],[-73.99840247299994,22.445868231000077],[-74.00255286399994,22.421698309000078],[-74.01333574099993,22.40582916900007],[-74.06541907499991,22.39752838700008],[-74.08385169199994,22.39061107000009],[-74.07917232999995,22.355169989000046],[-74.1087133449999,22.32916901200008],[-74.13182532499991,22.325913804000038],[-74.18130449099993,22.301906643000052],[-74.17414303299995,22.285549221000053],[-74.19371497299994,22.273911851000037],[-74.18390865799995,22.25409577000005],[-74.22252356699994,22.225531317000048],[-74.26195227799991,22.22850169500009],[-74.27696692599994,22.21588776200008],[-74.27696692599994,22.171535549000055],[-74.24474036399994,22.200384833000044],[-74.19416256399995,22.216864325000074],[-74.15965735599991,22.246649481000077],[-74.13467363199993,22.28388092700004],[-74.11937415299991,22.29686107000009],[-74.09439042899993,22.301906643000052],[-74.01378333199995,22.33006419500009],[-73.96157792899993,22.37018463700008],[-73.92837480399993,22.430121161000045],[-73.88955644399994,22.455023505000042],[-73.86538652299993,22.48004791900007],[-73.84837805899991,22.537583726000037],[-73.83120683499993,22.541449286000045],[-73.84947669199994,22.582464911000045],[-73.85863196499992,22.657171942000048],[-73.87694251199991,22.66547272300005],[-73.84487870999993,22.72020091400009],[-73.85171464799993,22.733303127000056],[-73.90314693899995,22.719671942000048],[-73.95474199099993,22.72020091400009],[-73.93431555899991,22.698472398000035],[-73.95555579299992,22.695379950000074],[-73.98314368399991,22.67597077000005],[-74.01622473899994,22.684881903000075],[-73.99571692599994,22.657538153000075]]],[[[-74.28685462099992,22.733303127000056],[-74.27814693899995,22.72842031500005],[-74.28376217399995,22.695379950000074],[-74.26642818899995,22.698553778000075],[-74.24893144399994,22.72020091400009],[-74.22227942599994,22.69562409100007],[-74.14842688699991,22.677801825000074],[-74.10407467399995,22.688299872000073],[-74.10496985599991,22.671820380000042],[-74.09068762899994,22.657538153000075],[-74.01622473899994,22.72020091400009],[-74.07591712099992,22.741888739000046],[-74.09813391799992,22.746893622000073],[-74.11164303299995,22.738348700000074],[-74.14753170499995,22.740668036000045],[-74.16706295499995,22.746893622000073],[-74.17796790299991,22.76170482000009],[-74.19371497299994,22.76117584800005],[-74.20055091099994,22.794663804000038],[-74.22850501199991,22.81582265800006],[-74.27009029899995,22.822658596000053],[-74.31041419199994,22.84316640800006],[-74.34455318899995,22.83624909100007],[-74.35195878799993,22.822658596000053],[-74.28685462099992,22.733303127000056]]],[[[-73.66616777299993,23.089544989000046],[-73.68569902299993,23.102484442000048],[-73.74185136599993,23.096991278000075],[-73.81696529899995,23.10382721600007],[-73.79735266799992,23.07379791900007],[-73.75523841099994,23.07298411700009],[-73.66616777299993,23.089544989000046]]],[[[-75.65355383999992,23.459458726000037],[-75.63731848899994,23.435492255000042],[-75.60187740799995,23.42340729400007],[-75.52383378799993,23.404852606000077],[-75.53978430899991,23.427679755000042],[-75.57697506399995,23.445868231000077],[-75.61998450399994,23.457098700000074],[-75.65355383999992,23.459458726000037]]],[[[-75.79820716099994,23.53864166900007],[-75.85631262899994,23.567287502000056],[-75.92174231699994,23.631415106000077],[-75.90807044199994,23.645005601000037],[-75.95718339799993,23.654852606000077],[-75.98224850199995,23.68268463700008],[-76.02354895699995,23.67918528900009],[-76.02944902299993,23.65566640800006],[-76.00304114499994,23.61399974200009],[-75.98770097599993,23.600816148000035],[-75.94546464799993,23.588039455000057],[-75.90807044199994,23.56240469000005],[-75.87336178299995,23.52838776200008],[-75.85924231699994,23.537543036000045],[-75.80443274599992,23.500433661000045],[-75.81273352799991,23.47955963700008],[-75.79377193899995,23.470526434000078],[-75.74299068899995,23.466945705000057],[-75.69823157499991,23.444281317000048],[-75.67536373599995,23.43854401200008],[-75.66104081899994,23.45262278900009],[-75.74494381399995,23.48273346600007],[-75.7825414699999,23.507554429000038],[-75.79820716099994,23.53864166900007]]],[[[-74.85912024599992,22.980292059000078],[-74.84483801999994,23.000148830000057],[-74.90965735599991,23.067328192000048],[-74.94635982999995,23.09373607000009],[-75.03160559799994,23.12335846600007],[-75.05996660099993,23.14166901200008],[-75.07689368399991,23.171698309000078],[-75.08503170499995,23.219875393000052],[-75.08511308499993,23.264064846000053],[-75.09406490799995,23.30532461100006],[-75.15322831899994,23.41120026200008],[-75.18272864499994,23.446437893000052],[-75.19489498599995,23.514105536000045],[-75.20457923099991,23.533351955000057],[-75.23932857999995,23.575100002000056],[-75.25291907499991,23.582953192000048],[-75.31159420499995,23.672349351000037],[-75.33141028599994,23.685980536000045],[-75.33458411399994,23.653143622000073],[-75.31159420499995,23.645005601000037],[-75.3047582669999,23.624945380000042],[-75.31159420499995,23.57680898600006],[-75.28648841099994,23.587632554000038],[-75.27684485599991,23.582953192000048],[-75.25633704299992,23.53514232000009],[-75.2500707669999,23.490179755000042],[-75.24250240799995,23.474310614000046],[-75.22223873599995,23.459458726000037],[-75.22057044199994,23.43976471600007],[-75.19489498599995,23.432114976000037],[-75.19688880099994,23.41014232000009],[-75.13166256399995,23.35578034100007],[-75.12035071499992,23.33193594000005],[-75.11294511599993,23.281317450000074],[-75.13585364499994,23.229803778000075],[-75.13963782499991,23.202826239000046],[-75.13219153599994,23.185980536000045],[-75.07510331899994,23.127386786000045],[-75.07868404899995,23.113674221000053],[-75.10610917899993,23.11749909100007],[-75.14647376199991,23.14476146000004],[-75.15831458199995,23.144191799000055],[-75.18057206899994,23.159857489000046],[-75.16356360599991,23.179022528000075],[-75.19172115799995,23.191799221000053],[-75.20791581899994,23.206244208000044],[-75.22964433499993,23.17145416900007],[-75.21397864499994,23.16547272300005],[-75.10488847599993,23.099554755000042],[-74.99054928299995,23.078558661000045],[-74.96764075399994,23.06386953300006],[-74.92442786399994,23.01170482000009],[-74.91405188699991,22.970851955000057],[-74.88548743399991,22.92381419500009],[-74.8789770169999,22.897772528000075],[-74.86534583199995,22.90452708500004],[-74.85806230399993,22.87055084800005],[-74.83995520699995,22.859930731000077],[-74.83202063699991,22.874945380000042],[-74.83340410099993,22.914129950000074],[-74.84703528599994,22.97011953300006],[-74.85912024599992,22.980292059000078]]],[[[-74.90005449099993,23.70648834800005],[-74.93862870999993,23.69432200700004],[-74.9453832669999,23.681586005000042],[-74.94163977799991,23.658677476000037],[-74.9238988919999,23.664455471000053],[-74.89085852799991,23.66474030200004],[-74.87279212099992,23.672349351000037],[-74.86074785099993,23.657904364000046],[-74.83674068899995,23.650824286000045],[-74.82494055899991,23.664943752000056],[-74.79369055899991,23.67133209800005],[-74.77721106699994,23.699652411000045],[-74.81236731699994,23.719671942000048],[-74.84972083199995,23.72011953300006],[-74.90005449099993,23.70648834800005]]],[[[-76.32518469999991,23.97402578300006],[-76.31895911399994,23.980210679000038],[-76.37677975199995,24.101385809000078],[-76.40147864499994,24.10293203300006],[-76.37498938699991,24.046576239000046],[-76.34019934799994,23.98818594000005],[-76.32518469999991,23.97402578300006]]],[[[-74.46865800699993,24.02179596600007],[-74.47850501199991,23.99168528900009],[-74.47549394399994,23.980210679000038],[-74.43455969999991,24.045396226000037],[-74.42711341099994,24.083238023000035],[-74.45815995999993,24.14130280200004],[-74.51870683499993,24.114935614000046],[-74.52969316299993,24.100409247000073],[-74.5254613919999,24.045721747000073],[-74.52965247299994,24.011053778000075],[-74.5410863919999,23.98004791900007],[-74.55809485599991,23.953558661000045],[-74.49229895699995,23.96039459800005],[-74.47691809799994,24.00999583500004],[-74.46865800699993,24.02179596600007]]],[[[-77.88068600199995,24.18626536700009],[-77.86082923099991,24.22524648600006],[-77.80842037699995,24.239447333000044],[-77.79869544199994,24.25458405200004],[-77.83967037699995,24.25458405200004],[-77.87100175699993,24.24091217700004],[-77.92593339799993,24.20644765800006],[-77.94058183499993,24.19358958500004],[-77.97337805899991,24.196193752000056],[-77.96906490799995,24.15957265800006],[-77.94456946499992,24.154242255000042],[-77.88068600199995,24.18626536700009]]],[[[-77.92162024599992,24.26203034100007],[-77.93219967399995,24.27570221600007],[-77.97874915299991,24.25922272300005],[-78.00475012899994,24.25458405200004],[-78.01292883999992,24.23965078300006],[-77.99046790299991,24.22724030200004],[-77.95026607999995,24.230536200000074],[-77.93639075399994,24.261542059000078],[-77.92162024599992,24.26203034100007]]],[[[-76.46300208199995,24.21360911700009],[-76.47166907499991,24.22760651200008],[-76.50389563699991,24.247748114000046],[-76.52953040299991,24.28742096600007],[-76.53526770699995,24.268377997000073],[-76.49315344999991,24.218817450000074],[-76.46300208199995,24.21360911700009]]],[[[-77.52497311099995,23.93305084800005],[-77.54503333199995,23.93659088700008],[-77.60069739499994,23.905096747000073],[-77.59707597599993,23.93036530200004],[-77.55577551999994,23.938299872000073],[-77.52818762899994,23.95856354400007],[-77.52818762899994,23.97988515800006],[-77.54548092399995,24.004380601000037],[-77.53799394399994,24.045396226000037],[-77.54946855399993,24.125921942000048],[-77.59260006399995,24.19822825700004],[-77.60753333199995,24.21824778900009],[-77.62734941299993,24.221096096000053],[-77.63914954299992,24.20648834800005],[-77.64785722599993,24.137884833000044],[-77.67853756399995,24.10175202000005],[-77.70958411399994,24.04368724200009],[-77.72984778599994,24.042629299000055],[-77.70482337099992,24.094671942000048],[-77.70767167899993,24.12372467700004],[-77.72354081899994,24.11737702000005],[-77.7481176419999,24.131781317000048],[-77.73692786399994,24.14862702000005],[-77.73997962099992,24.164496161000045],[-77.7024633449999,24.190252997000073],[-77.64073645699995,24.23729075700004],[-77.63121497299994,24.24909088700008],[-77.65021725199995,24.260158596000053],[-77.67516028599994,24.30296458500004],[-77.70311438699991,24.288723049000055],[-77.71442623599995,24.275091864000046],[-77.75572669199994,24.25926341400009],[-77.77074133999992,24.247748114000046],[-77.78237870999993,24.221584377000056],[-77.80540930899991,24.208929755000042],[-77.83967037699995,24.206732489000046],[-77.85391191299993,24.165838934000078],[-77.89216061099995,24.149644273000035],[-77.91100012899994,24.13580963700008],[-77.90908769399994,24.105292059000078],[-77.92906653599994,24.11123281500005],[-77.94204667899993,24.096869208000044],[-77.87962805899991,24.073960679000038],[-77.86009680899991,24.06273021000004],[-77.8665258449999,24.04755280200004],[-77.83348548099991,24.02179596600007],[-77.8372289699999,23.984808661000045],[-77.82876542899993,23.969183661000045],[-77.8274633449999,23.943915106000077],[-77.81285559799994,23.876654364000046],[-77.79869544199994,23.856675523000035],[-77.78587805899991,23.85370514500005],[-77.74779212099992,23.864162502000056],[-77.66559811099995,23.86347077000005],[-77.69884192599994,23.84511953300006],[-77.76455644399994,23.823187567000048],[-77.74429277299993,23.81639232000009],[-77.72020423099991,23.784816799000055],[-77.70311438699991,23.788397528000075],[-77.59406490799995,23.853461005000042],[-77.57616126199991,23.84711334800005],[-77.59129798099991,23.82489655200004],[-77.64500891799992,23.80145905200004],[-77.66905676999994,23.779242255000042],[-77.67516028599994,23.76113515800006],[-77.72769120999993,23.777736721000053],[-77.74404863199993,23.76113515800006],[-77.77818762899994,23.74681224200009],[-77.71458899599992,23.74872467700004],[-77.68492591099994,23.741441148000035],[-77.66087805899991,23.72016022300005],[-77.64297441299993,23.75486888200004],[-77.59732011599993,23.730617580000057],[-77.56590735599991,23.740668036000045],[-77.55260169199994,23.760239976000037],[-77.54548092399995,23.788397528000075],[-77.55134029899995,23.803127346000053],[-77.56944739499994,23.816839911000045],[-77.53457597599993,23.828558661000045],[-77.51748613199993,23.864162502000056],[-77.52261308499993,23.881008205000057],[-77.51650956899994,23.918850002000056],[-77.52497311099995,23.93305084800005]]],[[[-77.76768958199995,24.326605536000045],[-77.74803626199991,24.34593333500004],[-77.71983801999994,24.350775458000044],[-77.69033769399994,24.338446356000077],[-77.68809973899994,24.36078522300005],[-77.71922766799992,24.37173086100006],[-77.75804602799991,24.35936107000009],[-77.79869544199994,24.337103583000044],[-77.78685462099992,24.314601955000057],[-77.76768958199995,24.326605536000045]]],[[[-75.31468665299991,24.217352606000077],[-75.37559973899994,24.275946356000077],[-75.40038001199991,24.323472398000035],[-75.41901607999995,24.337836005000042],[-75.47606360599991,24.37059153900009],[-75.53221594999991,24.44489166900007],[-75.55996660099993,24.491441148000035],[-75.57201087099992,24.522691148000035],[-75.6126195949999,24.562445380000042],[-75.61856035099993,24.581244208000044],[-75.62633216099994,24.645005601000037],[-75.63731848899994,24.65696849200009],[-75.67536373599995,24.672308661000045],[-75.70885169199994,24.70018138200004],[-75.73216712099992,24.69863515800006],[-75.75300045499995,24.67601146000004],[-75.75658118399991,24.660305080000057],[-75.74107825399994,24.646144924000055],[-75.70885169199994,24.631333726000037],[-75.65274003799993,24.588364976000037],[-75.6426488919999,24.55149974200009],[-75.64736894399994,24.528957424000055],[-75.61436926999994,24.526597398000035],[-75.58849036399994,24.512640692000048],[-75.57152258999992,24.488755601000037],[-75.56110592399995,24.443426825000074],[-75.54190019399994,24.41746653900009],[-75.53400631399995,24.389227606000077],[-75.48058020699995,24.309393622000073],[-75.45995032499991,24.294582424000055],[-75.43073482999995,24.288723049000055],[-75.41006425699993,24.27289459800005],[-75.40575110599991,24.240301825000074],[-75.41466223899994,24.21312083500004],[-75.43390865799995,24.21360911700009],[-75.46361243399991,24.185614325000074],[-75.52131100199995,24.162583726000037],[-75.50991777299993,24.14207591400009],[-75.48314368399991,24.12571849200009],[-75.45840410099993,24.12791575700004],[-75.4251195949999,24.14996979400007],[-75.37987219999991,24.15839264500005],[-75.33580481699994,24.15851471600007],[-75.29043535099993,24.14472077000005],[-75.30699622299994,24.20254140800006],[-75.31468665299991,24.217352606000077]]],[[[-77.47996985599991,24.991359768000052],[-77.45287024599992,24.994614976000037],[-77.4356176419999,25.014878648000035],[-77.40367591099994,25.00214264500005],[-77.37950598899994,25.00153229400007],[-77.2747289699999,25.046087958000044],[-77.26382402299993,25.05809153900009],[-77.3020727199999,25.066107489000046],[-77.32607988199993,25.079331773000035],[-77.35562089799993,25.084051825000074],[-77.39806067599994,25.076971747000073],[-77.45604407499991,25.076971747000073],[-77.52717037699995,25.054388739000046],[-77.55101477799991,25.038031317000048],[-77.56187903599994,25.018866278000075],[-77.54548092399995,25.001898505000042],[-77.49510657499991,24.999335028000075],[-77.47996985599991,24.991359768000052]]],[[[-78.3535863919999,24.65924713700008],[-78.37840735599991,24.66941966400009],[-78.38149980399993,24.655503648000035],[-78.40884355399993,24.63617584800005],[-78.44298255099994,24.62449778900009],[-78.41148841099994,24.58104075700004],[-78.3516332669999,24.530462958000044],[-78.22642981699994,24.46548086100006],[-78.20645097599993,24.460638739000046],[-78.18004309799994,24.464911200000074],[-78.13442949099993,24.501613674000055],[-78.14085852799991,24.43349844000005],[-78.13414466099994,24.403957424000055],[-78.0801488919999,24.379461981000077],[-78.06330318899995,24.316229559000078],[-78.05190995999993,24.288723049000055],[-78.03697669199994,24.275946356000077],[-78.01170813699991,24.28188711100006],[-78.01276607999995,24.314154364000046],[-77.96141516799992,24.345526434000078],[-77.90855872299994,24.350775458000044],[-77.86009680899991,24.384914455000057],[-77.87169348899994,24.406805731000077],[-77.86384029899995,24.41543203300006],[-77.83348548099991,24.419094143000052],[-77.80410722599993,24.44879791900007],[-77.75108801999994,24.45848216400009],[-77.72789466099994,24.47418854400007],[-77.71873938699991,24.499823309000078],[-77.72354081899994,24.535101630000042],[-77.74547278599994,24.57758209800005],[-77.75357011599993,24.602036851000037],[-77.73778235599991,24.65770091400009],[-77.73721269399994,24.70018138200004],[-77.74649003799993,24.714504299000055],[-77.78351803299995,24.74823639500005],[-77.8125707669999,24.76190827000005],[-77.82917232999995,24.778225002000056],[-77.87336178299995,24.83856842700004],[-77.93419348899994,24.976223049000055],[-77.94725501199991,24.991156317000048],[-77.98058020699995,25.001898505000042],[-77.98656165299991,25.012884833000044],[-77.96996008999992,25.076971747000073],[-77.99429277299993,25.105169989000046],[-77.99176998599995,25.122788804000038],[-77.97687740799995,25.138413804000038],[-77.98766028599994,25.15224844000005],[-78.04515540299991,25.18309153900009],[-78.09044348899994,25.159491278000075],[-78.10704505099994,25.16307200700004],[-78.12218176999994,25.18317291900007],[-78.14956620999993,25.19953034100007],[-78.20274817599994,25.206732489000046],[-78.21434485599991,25.18463776200008],[-78.20091712099992,25.146429755000042],[-78.18004309799994,25.10492584800005],[-78.1692602199999,25.07322825700004],[-78.15770423099991,25.00018952000005],[-78.17454993399991,24.981675523000035],[-78.18968665299991,24.94668203300006],[-78.17393958199995,24.919623114000046],[-78.18602454299992,24.904771226000037],[-78.20811926999994,24.861802476000037],[-78.24372311099995,24.85101959800005],[-78.23961341099994,24.82636139500005],[-78.25495357999995,24.806463934000078],[-78.29214433499993,24.775336005000042],[-78.30113684799994,24.745347398000035],[-78.30235755099994,24.72068919500009],[-78.28844153599994,24.714178778000075],[-78.27424068899995,24.682928778000075],[-78.2578832669999,24.72992584800005],[-78.25112870999993,24.70018138200004],[-78.22223873599995,24.666083075000074],[-78.22325598899994,24.645005601000037],[-78.19713294199994,24.618353583000044],[-78.20173092399995,24.598781643000052],[-78.23070227799991,24.61082591400009],[-78.22826087099992,24.569484768000052],[-78.23770097599993,24.55687083500004],[-78.25426184799994,24.566148179000038],[-78.26911373599995,24.609198309000078],[-78.31899980399993,24.581732489000046],[-78.33311926999994,24.603989976000037],[-78.31261145699995,24.617661851000037],[-78.29702714799993,24.643784898000035],[-78.29214433499993,24.67601146000004],[-78.29792232999995,24.69684479400007],[-78.31143144399994,24.710109768000052],[-78.3274633449999,24.710638739000046],[-78.34056555899991,24.693426825000074],[-78.33507239499994,24.67283763200004],[-78.3535863919999,24.65924713700008]]],[[[-76.16128495999993,25.165716864000046],[-76.22370357999995,25.20807526200008],[-76.28416907499991,25.26190827000005],[-76.33576412699995,25.327093817000048],[-76.36827551999994,25.342718817000048],[-76.41454016799992,25.34446849200009],[-76.46092688699991,25.35455963700008],[-76.49762936099995,25.373480536000045],[-76.62458248599995,25.46246979400007],[-76.64391028599994,25.467352606000077],[-76.69098873599995,25.525946356000077],[-76.67520097599993,25.556789455000057],[-76.68675696499992,25.56207916900007],[-76.72984778599994,25.563625393000052],[-76.74551347599993,25.48924388200004],[-76.76939856699994,25.42405833500004],[-76.73428300699993,25.441839911000045],[-76.71275794199994,25.446926174000055],[-76.63052324099993,25.433254299000055],[-76.59121660099993,25.42255280200004],[-76.53083248599995,25.370428778000075],[-76.48961341099994,25.351304429000038],[-76.40953528599994,25.335191148000035],[-76.37157141799992,25.32249583500004],[-76.34626217399995,25.303534247000073],[-76.2637426419999,25.19367096600007],[-76.23228919199994,25.17096588700008],[-76.19635982999995,25.15428294500009],[-76.16494706899994,25.129828192000048],[-76.14704342399995,25.083807684000078],[-76.14671790299991,25.062445380000042],[-76.15444902299993,25.00531647300005],[-76.18797766799992,24.973944403000075],[-76.20225989499994,24.94668203300006],[-76.21092688699991,24.90102773600006],[-76.20596269399994,24.864081122000073],[-76.18394934799994,24.88930898600006],[-76.17495683499993,24.871527411000045],[-76.18244381399995,24.848781643000052],[-76.21934973899994,24.81004466400009],[-76.31745357999995,24.82526276200008],[-76.33633378799993,24.814927476000037],[-76.31525631399995,24.792669989000046],[-76.25300045499995,24.765773830000057],[-76.23643958199995,24.754828192000048],[-76.22264563699991,24.726955471000053],[-76.21178137899994,24.690252997000073],[-76.19448808499993,24.65839264500005],[-76.16128495999993,24.645005601000037],[-76.17398027299993,24.67731354400007],[-76.17747962099992,24.71580638200004],[-76.17495683499993,24.78587474200009],[-76.14614824099993,24.83421458500004],[-76.13695227799991,24.902167059000078],[-76.15794837099992,24.962144273000035],[-76.15444902299993,24.981390692000048],[-76.13060462099992,24.99485911700009],[-76.12132727799991,25.030096747000073],[-76.11290442599994,25.114894924000055],[-76.13125566299993,25.147772528000075],[-76.16128495999993,25.165716864000046]]],[[[-77.81920325399994,25.707586981000077],[-77.82087154899995,25.716620184000078],[-77.85826575399994,25.777289130000042],[-77.86803137899994,25.78742096600007],[-77.88731848899994,25.78143952000005],[-77.83145097599993,25.708685614000046],[-77.81920325399994,25.707586981000077]]],[[[-77.53799394399994,26.306586005000042],[-77.5703832669999,26.33270905200004],[-77.57933508999992,26.318019924000055],[-77.56704667899993,26.29100169500009],[-77.53799394399994,26.306586005000042]]],[[[-78.59007727799991,26.53620026200008],[-78.57046464799993,26.541571356000077],[-78.54015051999994,26.565008856000077],[-78.29338578699992,26.621300259000066],[-78.23770620099992,26.621325470000045],[-78.10033118399991,26.64606354400007],[-78.04076087099992,26.65843333500004],[-77.96255449099993,26.65973541900007],[-77.92906653599994,26.639227606000077],[-77.91055253799993,26.646307684000078],[-77.90294348899994,26.681586005000042],[-77.92080644399994,26.716945705000057],[-77.91307532499991,26.757879950000074],[-77.93114173099991,26.77049388200004],[-77.95628821499992,26.73541901200008],[-77.97166907499991,26.72955963700008],[-78.05540930899991,26.72601959800005],[-78.11894820799995,26.704328287000067],[-78.16705098299991,26.710400108000044],[-78.20264129899994,26.697158680000086],[-78.25292968999992,26.712942463000047],[-78.27361518799995,26.703582975000074],[-78.28708191999993,26.68549205900007],[-78.31998377999992,26.68652744500008],[-78.41705909299992,26.704749029000084],[-78.43781648399994,26.69982110500007],[-78.5202572959999,26.728995355000052],[-78.53917395699995,26.739203192000048],[-78.55719967399995,26.778550523000035],[-78.5938207669999,26.81053294500009],[-78.60741126199991,26.755926825000074],[-78.64159094999991,26.714992580000057],[-78.60000566299993,26.708197333000044],[-78.5759422349999,26.694769508000036],[-78.6044346509999,26.65053474000007],[-78.63797875099993,26.609730986000045],[-78.7142887789999,26.570519948000083],[-78.73815509299993,26.57080854600008],[-78.77037512399994,26.58332478400007],[-78.80606035099993,26.577826239000046],[-78.83580481699994,26.614569403000075],[-78.96434485599991,26.68768952000005],[-78.98729407499991,26.68622467700004],[-78.93959961399992,26.65078778700007],[-78.83455712699993,26.55365117100007],[-78.79108864499995,26.529699767000068],[-78.76683934699992,26.508104203000073],[-78.72696836599994,26.49544569200009],[-78.68613184999992,26.49191095300006],[-78.65577003499993,26.497788470000046],[-78.59007727799991,26.53620026200008]]],[[[-77.81236731699994,26.906805731000077],[-77.75462805899991,26.879461981000077],[-77.69684811099995,26.877346096000053],[-77.63805091099994,26.859930731000077],[-77.61432857999995,26.85651276200008],[-77.57848059799994,26.871161200000074],[-77.55622311099995,26.87055084800005],[-77.51748613199993,26.85219961100006],[-77.49022376199991,26.82330963700008],[-77.44469153599994,26.755072333000044],[-77.42813066299993,26.721177476000037],[-77.42699133999992,26.695135809000078],[-77.39085852799991,26.68768952000005],[-77.37535559799994,26.69953034100007],[-77.35008704299992,26.66901276200008],[-77.35301673099991,26.64606354400007],[-77.32974199099993,26.60455963700008],[-77.31216386599993,26.594671942000048],[-77.21955318899995,26.582424221000053],[-77.16071529899995,26.564195054000038],[-77.15355383999992,26.54946523600006],[-77.16808020699995,26.515773830000057],[-77.18643144399994,26.505926825000074],[-77.18809973899994,26.479803778000075],[-77.19619706899994,26.46287669500009],[-77.21544348899994,26.447699286000045],[-77.23013261599993,26.44745514500005],[-77.22862708199995,26.37693919500009],[-77.21861731699994,26.355292059000078],[-77.25035559799994,26.313055731000077],[-77.23338782499991,26.298000393000052],[-77.23013261599993,26.258734442000048],[-77.23387610599991,26.24404531500005],[-77.25182044199994,26.22601959800005],[-77.25641842399995,26.203924872000073],[-77.23631751199991,26.183661200000074],[-77.23379472599993,26.162258205000057],[-77.22329667899993,26.145819403000075],[-77.24893144399994,26.136053778000075],[-77.27236894399994,26.118312893000052],[-77.30894934799994,26.080633856000077],[-77.34788977799991,26.07062409100007],[-77.35912024599992,26.04588450700004],[-77.37393144399994,26.03546784100007],[-77.4082738919999,26.029730536000045],[-77.38817298099991,26.00543854400007],[-77.31505286399994,25.99095286700009],[-77.29898027299993,25.97793203300006],[-77.27623450399994,25.92601146000004],[-77.25304114499994,25.897609768000052],[-77.22667395699995,25.88507721600007],[-77.1907445949999,25.91632721600007],[-77.18716386599993,25.98847077000005],[-77.19220943899995,26.06899648600006],[-77.1817113919999,26.12531159100007],[-77.18321692599994,26.144273179000038],[-77.17438717399995,26.22068919500009],[-77.16494706899994,26.245428778000075],[-77.1153051419999,26.29043203300006],[-77.08446204299992,26.30613841400009],[-77.05199133999992,26.30963776200008],[-77.03270423099991,26.297430731000077],[-77.01475989499994,26.315659898000035],[-77.02928626199991,26.34442780200004],[-77.03001868399991,26.36273834800005],[-77.01720130099994,26.385972398000035],[-77.04625403599994,26.396307684000078],[-77.05882727799991,26.42324453300006],[-77.05858313699991,26.46784088700008],[-77.04214433499993,26.48183828300006],[-77.0423070949999,26.49803294500009],[-77.05882727799991,26.529364325000074],[-77.10724850199995,26.556708075000074],[-77.15526282499991,26.58808014500005],[-77.18276933499993,26.60028717700004],[-77.24388587099992,26.610500393000052],[-77.26988684799994,26.625067450000074],[-77.29059811099995,26.646958726000037],[-77.30524654899995,26.67401764500005],[-77.27261308499993,26.673000393000052],[-77.28368079299992,26.68968333500004],[-77.32795162699995,26.709133205000057],[-77.51130123599995,26.869533596000053],[-77.55357825399994,26.89801666900007],[-77.60684160099993,26.920396226000037],[-77.62979081899994,26.90420156500005],[-77.68236243399991,26.90843333500004],[-77.77448482999995,26.92723216400009],[-77.86815344999991,26.92841217700004],[-77.91344153599994,26.92096588700008],[-77.94945227799991,26.89988841400009],[-77.9414770169999,26.89354075700004],[-77.90977942599994,26.904038804000038],[-77.83067786399994,26.909409898000035],[-77.81236731699994,26.906805731000077]]],[[[-75.72207597599993,22.20258209800005],[-75.74974524599992,22.20453522300005],[-75.73436438699991,22.175034898000035],[-75.71471106699994,22.182928778000075],[-75.72207597599993,22.20258209800005]]],[[[-73.61817706699992,22.601249625000037],[-73.62549800399995,22.574257902000056],[-73.60761801299992,22.57725526300004],[-73.59379446199995,22.601993096000058],[-73.61817706699992,22.601249625000037]]],[[[-77.8692113919999,24.346828518000052],[-77.88369706899994,24.327337958000044],[-77.86729895699995,24.319484768000052],[-77.84862219999991,24.32758209800005],[-77.83975175699993,24.350083726000037],[-77.8692113919999,24.346828518000052]]],[[[-76.77432206899994,24.53864166900007],[-76.79238847599993,24.54913971600007],[-76.78929602799991,24.518500067000048],[-76.77257239499994,24.51911041900007],[-76.77432206899994,24.53864166900007]]],[[[-79.2500707669999,25.774725653000075],[-79.28034420499995,25.75722890800006],[-79.29381262899994,25.736029364000046],[-79.30426998599995,25.702337958000044],[-79.26183020699995,25.693752346000053],[-79.26813717399995,25.707098700000074],[-79.29385331899994,25.72068919500009],[-79.27904212099992,25.74998607000009],[-79.26695716099994,25.732489325000074],[-79.24323482999995,25.74290599200009],[-79.2500707669999,25.774725653000075]]],[[[-77.24425208199995,26.478461005000042],[-77.26183020699995,26.477850653000075],[-77.27822831899994,26.46580638200004],[-77.26329505099994,26.442206122000073],[-77.24103756399995,26.44281647300005],[-77.24425208199995,26.478461005000042]]],[[[-77.85383053299995,26.600775458000044],[-77.85757402299993,26.611761786000045],[-77.88780676999994,26.606594143000052],[-77.88658606699994,26.585598049000055],[-77.86839758999992,26.56386953300006],[-77.85558020699995,26.572739976000037],[-77.85383053299995,26.600775458000044]]]]}},
{"type":"Feature","properties":{"ISO_A2":"TC","ADMIN":"Turks and Caicos Islands","ISO_A3":"TCA","BORDER":1},"geometry":{"type":"MultiPolygon","coordinates":[[[[-72.46076412699995,21.70538971600007],[-72.46336829299992,21.664943752000056],[-72.47020423099991,21.646877346000053],[-72.45201575399994,21.644964911000045],[-72.43325761599993,21.680121161000045],[-72.43342037699995,21.70921458500004],[-72.46076412699995,21.70538971600007]]],[[[-71.50796464799993,21.70538971600007],[-71.47126217399995,21.662176825000074],[-71.45958411399994,21.664455471000053],[-71.45189368399991,21.69017161700009],[-71.46178137899994,21.71084219000005],[-71.48094641799992,21.725490627000056],[-71.54308020699995,21.746527411000045],[-71.57628333199995,21.766831773000035],[-71.59056555899991,21.75381094000005],[-71.57323157499991,21.735296942000048],[-71.50796464799993,21.70538971600007]]],[[[-71.80284583199995,21.787909247000073],[-71.74111894399994,21.79490794500009],[-71.71544348899994,21.78729889500005],[-71.69981848899994,21.75999583500004],[-71.68614661399994,21.774318752000056],[-71.66852779899995,21.750433661000045],[-71.6309301419999,21.774318752000056],[-71.65518144399994,21.82322825700004],[-71.67878170499995,21.841864325000074],[-71.72032630099994,21.835760809000078],[-71.78172766799992,21.84316640800006],[-71.80939693899995,21.839992580000057],[-71.82583574099993,21.850775458000044],[-71.85065670499995,21.856268622000073],[-71.84801184799994,21.81940338700008],[-71.82770748599995,21.78579336100006],[-71.80284583199995,21.787909247000073]]],[[[-72.3372289699999,21.80097077000005],[-72.28123938699991,21.77960846600007],[-72.27383378799993,21.75141022300005],[-72.23424231699994,21.78115469000005],[-72.21971594999991,21.770331122000073],[-72.19444739499994,21.766913153000075],[-72.19326738199993,21.78115469000005],[-72.16527258999992,21.79482656500005],[-72.15908769399994,21.774318752000056],[-72.12437903599994,21.807806708000044],[-72.14631100199995,21.82713450700004],[-72.15713456899994,21.82453034100007],[-72.17959550699993,21.80097077000005],[-72.21214758999992,21.793443101000037],[-72.24502519399994,21.79791901200008],[-72.3079320949999,21.85224030200004],[-72.32974199099993,21.845404364000046],[-72.34019934799994,21.81403229400007],[-72.3372289699999,21.80097077000005]]],[[[-72.02196204299992,21.90460846600007],[-72.00161699099993,21.897772528000075],[-71.96401933499993,21.933417059000078],[-71.94758053299995,21.94245026200008],[-71.94001217399995,21.925116278000075],[-71.95921790299991,21.889797268000052],[-71.96051998599995,21.876695054000038],[-71.94298255099994,21.85814036700009],[-71.87730872299994,21.82831452000005],[-71.90339107999995,21.866522528000075],[-71.91209876199991,21.925116278000075],[-71.91104081899994,21.948431708000044],[-71.94994055899991,21.95921458500004],[-72.00829016799992,21.95921458500004],[-72.03180904899995,21.946478583000044],[-72.02196204299992,21.90460846600007]]],[[[-71.13809160099993,21.513617255000042],[-71.15099036399994,21.47284577000005],[-71.1516820949999,21.45058828300006],[-71.13361568899995,21.446478583000044],[-71.13036048099991,21.498114325000074],[-71.13809160099993,21.513617255000042]]],[[[-71.49828040299991,21.56940338700008],[-71.50877844999991,21.551662502000056],[-71.50894120999993,21.527818101000037],[-71.53970292899993,21.516546942000048],[-71.53758704299992,21.49945709800005],[-71.52098548099991,21.48932526200008],[-71.49632727799991,21.50739166900007],[-71.49828040299991,21.56940338700008]]]]}},
-{"type":"Feature","properties":{"ISO_A2":"CN-TW","ADMIN":"Taiwan","ISO_A3":"TWN","BORDER":0},"geometry":{"type":"MultiPolygon","coordinates":[[[[121.90577233200008,24.950100002000056],[121.92676842500009,24.966620184000078],[122.00538170700008,25.001898505000042],[121.98357181100005,25.01715729400007],[121.95386803500003,25.022365627000056],[121.92725670700008,25.018703518000052],[121.90919030000009,25.036037502000056],[121.89722741000003,25.08388906500005],[121.90512129000001,25.106594143000052],[121.89283287900003,25.113674221000053],[121.71851647200003,25.152004299000055],[121.70386803500003,25.15688711100006],[121.67644290500004,25.179388739000046],[121.676117384,25.20258209800005],[121.64966881600003,25.197088934000078],[121.63599694100003,25.222805080000057],[121.59791100400003,25.271429755000042],[121.58082116000003,25.283026434000078],[121.53972415500004,25.28742096600007],[121.49781334700003,25.27875397300005],[121.461273634,25.26190827000005],[121.43628991000003,25.241441148000035],[121.41627037900003,25.203599351000037],[121.39576256600003,25.182806708000044],[121.42945397200003,25.155991929000038],[121.37875410200002,25.157945054000038],[121.34831790500004,25.137111721000053],[121.33025149800005,25.130682684000078],[121.21094811300009,25.111151434000078],[121.17693118600005,25.100043036000045],[121.12940514400009,25.071112372000073],[121.05933678500003,25.05023834800005],[121.03199303500003,25.029242255000042],[121.00554446700005,25.00067780200004],[120.96387780000009,24.94041575700004],[120.95362389400009,24.90501536700009],[120.92603600400003,24.884711005000042],[120.92058353000004,24.861395575000074],[120.90235436300009,24.836818752000056],[120.90919030000009,24.816880601000037],[120.87696373800009,24.76190827000005],[120.87134850400003,24.74323151200008],[120.85767662900003,24.73700592700004],[120.84750410200002,24.71084219000005],[120.82300866000003,24.68504466400009],[120.82667076900009,24.666083075000074],[120.79932701900009,24.670803127000056],[120.77808678500003,24.665716864000046],[120.74463951900009,24.638820705000057],[120.72787519600001,24.61391836100006],[120.70215905000009,24.60700104400007],[120.69019616000003,24.576117255000042],[120.66773522200003,24.54002513200004],[120.64893639400009,24.48387278900009],[120.57081139400009,24.40493398600006],[120.55811608200008,24.385891018000052],[120.54737389400009,24.35154857000009],[120.51197350400003,24.29555898600006],[120.49317467500009,24.23065827000005],[120.47779381600003,24.20966217700004],[120.43230228000004,24.161078192000048],[120.41863040500004,24.142523505000042],[120.41187584700003,24.096909898000035],[120.39234459700003,24.082912502000056],[120.38550866000003,24.063055731000077],[120.36182701900009,24.048529364000046],[120.34009850400003,24.02179596600007],[120.31812584700003,23.96580638200004],[120.29224694100003,23.911932684000078],[120.25749759200005,23.87840403900009],[120.24073326900009,23.832831122000073],[120.21949303500003,23.81793854400007],[120.18921959700003,23.77480703300006],[120.17554772200003,23.73383209800005],[120.17080496100004,23.69890152000005],[120.1542299140001,23.672211863000086],[120.143609008,23.63803570200008],[120.14086753400011,23.595262518000084],[120.14168913100002,23.537946590000047],[120.1242615330001,23.520020550000083],[120.12502924700004,23.489611410000066],[120.15754360400001,23.491475715000036],[120.13054446700005,23.450995184000078],[120.13477623800009,23.426255601000037],[120.15064537900003,23.398138739000046],[120.1645773890001,23.36171145600008],[120.13014916000009,23.36096024400007],[120.096039259,23.25535716400009],[120.096097504,23.23949089200005],[120.07862389400009,23.206976630000042],[120.08692467500009,23.182033596000053],[120.06202233200008,23.16502513200004],[120.05941816500001,23.15102773600006],[120.086273634,23.12693919500009],[120.09644616000003,23.086411851000037],[120.05941816500001,23.075873114000046],[120.05518639400009,23.04368724200009],[120.07667076900009,23.03742096600007],[120.09245853000004,23.054754950000074],[120.10792076900009,23.048570054000038],[120.10914147200003,23.015448309000078],[120.12671959700003,23.007147528000075],[120.14210045700008,23.034898179000038],[120.16618899800005,23.031724351000037],[120.170258009,23.013373114000046],[120.16016686300009,22.992132880000042],[120.14210045700008,22.980292059000078],[120.16382897200003,22.95384349200009],[120.17554772200003,22.884100653000075],[120.19141686300009,22.852036851000037],[120.20134524800005,22.844142971000053],[120.19646243600005,22.81858958500004],[120.19996178500003,22.79246653900009],[120.21753991000003,22.756740627000056],[120.23617597700002,22.72988515800006],[120.25212649800005,22.68964264500005],[120.23959394600001,22.659084377000056],[120.25896243600005,22.62759023600006],[120.31348717500009,22.577053127000056],[120.32797285200002,22.551703192000048],[120.33122806100005,22.51972077000005],[120.38086998800009,22.48387278900009],[120.40211022200003,22.486802476000037],[120.45720462300005,22.451605536000045],[120.51197350400003,22.424750067000048],[120.51807701900009,22.40696849200009],[120.56967207100001,22.373846747000073],[120.59392337300005,22.32916901200008],[120.62126712300005,22.295070705000057],[120.69695071700005,22.113226630000042],[120.68336022200003,22.079046942000048],[120.68336022200003,22.01386139500005],[120.70435631600003,21.981390692000048],[120.70036868600005,21.948431708000044],[120.72730553500003,21.934475002000056],[120.73609459700003,21.94281647300005],[120.73731530000009,21.966131903000075],[120.76742597700002,21.95929596600007],[120.79737389400009,21.94676341400009],[120.82309004000001,21.928615627000056],[120.84156334700003,21.925482489000046],[120.83570397200003,21.987250067000048],[120.863617384,22.008937893000052],[120.879242384,22.055812893000052],[120.87501061300009,22.28925202000005],[120.88656660200002,22.36595286700009],[120.92286217500009,22.424750067000048],[120.94109134200005,22.483628648000035],[120.94825280000009,22.52680084800005],[120.96062259200005,22.547796942000048],[120.99122155000009,22.58307526200008],[121.02100670700008,22.64789459800005],[121.08025149800005,22.70408763200004],[121.14144941500001,22.74176666900007],[121.17741946700005,22.778631903000075],[121.19027754000001,22.80215078300006],[121.19027754000001,22.84316640800006],[121.20281009200005,22.85651276200008],[121.25562584700003,22.897772528000075],[121.29957116000003,22.95917389500005],[121.31885826900009,23.025539455000057],[121.33708743600005,23.055080471000053],[121.34742272200003,23.083319403000075],[121.38160241000003,23.110663153000075],[121.40007571700005,23.14549388200004],[121.40333092500009,23.21474844000005],[121.40886478000004,23.25458405200004],[121.42676842500009,23.293117580000057],[121.45362389400009,23.325995184000078],[121.46216881600003,23.343003648000035],[121.52149498800009,23.658677476000037],[121.53980553500003,23.69281647300005],[121.57032311300009,23.832098700000074],[121.59449303500003,23.891506252000056],[121.61109459700003,23.982489325000074],[121.62501061300009,24.002386786000045],[121.62891686300009,24.026190497000073],[121.61500084700003,24.042303778000075],[121.60743248800009,24.07684967700004],[121.61304772200003,24.09210846600007],[121.64128665500004,24.12986888200004],[121.65219160200002,24.17218659100007],[121.66285241000003,24.193101304000038],[121.69776451900009,24.21898021000004],[121.69825280000009,24.23151276200008],[121.72144616000003,24.251166083000044],[121.77271569100003,24.30923086100006],[121.75855553500003,24.33234284100007],[121.76644941500001,24.347357489000046],[121.77149498800009,24.380560614000046],[121.78077233200008,24.397609768000052],[121.78003991000003,24.43268463700008],[121.80681399800005,24.460638739000046],[121.84001712300005,24.481350002000056],[121.83008873800009,24.519232489000046],[121.85279381600003,24.530991929000038],[121.85596764400009,24.550523179000038],[121.84083092500009,24.590318101000037],[121.869476759,24.598944403000075],[121.82797285200002,24.622788804000038],[121.81495201900009,24.644354559000078],[121.81421959700003,24.672308661000045],[121.82032311300009,24.699652411000045],[121.80779056100005,24.753119208000044],[121.80884850400003,24.805161851000037],[121.82349694100003,24.85492584800005],[121.83472741000003,24.871527411000045],[121.90577233200008,24.950100002000056]]],[[[121.56348717500009,22.044623114000046],[121.56657962300005,22.082180080000057],[121.49203535200002,22.082180080000057],[121.50261478000004,22.066555080000057],[121.49830162900003,22.048041083000044],[121.53891035200002,22.01821523600006],[121.56690514400009,22.007757880000042],[121.58887780000009,22.023504950000074],[121.56348717500009,22.044623114000046]]],[[[118.39421634200005,24.522691148000035],[118.385392712,24.503817045000062],[118.381414146,24.473511031000044],[118.35480862500003,24.470561718000056],[118.3166568150001,24.49334875900007],[118.28207441500001,24.477362372000073],[118.28956139400009,24.455633856000077],[118.29200280000009,24.424994208000044],[118.28394616000003,24.401271877000056],[118.31821764600011,24.390544350000084],[118.34576026800005,24.417579187000058],[118.3728561050001,24.433201267000072],[118.40362421200007,24.436009837000086],[118.44878503900009,24.415513445000045],[118.463355546,24.431523084000048],[118.46735474900004,24.461827610000057],[118.44958993900002,24.505527833000087],[118.422596613,24.52796832200005],[118.39421634200005,24.522691148000035]]],[[[119.64795983200008,23.603461005000042],[119.64185631600003,23.59406159100007],[119.60629316500001,23.590399481000077],[119.59546959700003,23.604722398000035],[119.56723066500001,23.58852773600006],[119.55925540500004,23.56932200700004],[119.58716881600003,23.54881419500009],[119.55738366000003,23.53461334800005],[119.56397545700008,23.51658763200004],[119.60010826900009,23.52838776200008],[119.61500084700003,23.554632880000042],[119.64063561300009,23.56354401200008],[119.66846764400009,23.54881419500009],[119.66578209700003,23.57330963700008],[119.64795983200008,23.603461005000042]]],[[[119.47706139400009,23.603461005000042],[119.49073326900009,23.590399481000077],[119.50782311300009,23.63239166900007],[119.48943118600005,23.63963450700004],[119.47706139400009,23.603461005000042]]],[[[119.572601759,23.62767161700009],[119.59066816500001,23.63279857000009],[119.59107506600003,23.66351959800005],[119.56641686300009,23.67918528900009],[119.545258009,23.664943752000056],[119.56934655000009,23.63971588700008],[119.572601759,23.62767161700009]]],[[[121.46070397200003,22.659002997000073],[121.47144616000003,22.641791083000044],[121.49496504000001,22.644191799000055],[121.50277754000001,22.66950104400007],[121.49577884200005,22.678534247000073],[121.45753014400009,22.675930080000057],[121.46070397200003,22.659002997000073]]],[[[120.02186078900002,23.45164440700006],[120.04791202000001,23.458084713000062],[120.090300909,23.499395950000064],[120.07493184000009,23.50081817100005],[120.02186078900002,23.45164440700006]]]]}},
+{"type":"Feature","properties":{"ISO_A2":"TW","ADMIN":"Taiwan","ISO_A3":"TWN","BORDER":0},"geometry":{"type":"MultiPolygon","coordinates":[[[[121.90577233200008,24.950100002000056],[121.92676842500009,24.966620184000078],[122.00538170700008,25.001898505000042],[121.98357181100005,25.01715729400007],[121.95386803500003,25.022365627000056],[121.92725670700008,25.018703518000052],[121.90919030000009,25.036037502000056],[121.89722741000003,25.08388906500005],[121.90512129000001,25.106594143000052],[121.89283287900003,25.113674221000053],[121.71851647200003,25.152004299000055],[121.70386803500003,25.15688711100006],[121.67644290500004,25.179388739000046],[121.676117384,25.20258209800005],[121.64966881600003,25.197088934000078],[121.63599694100003,25.222805080000057],[121.59791100400003,25.271429755000042],[121.58082116000003,25.283026434000078],[121.53972415500004,25.28742096600007],[121.49781334700003,25.27875397300005],[121.461273634,25.26190827000005],[121.43628991000003,25.241441148000035],[121.41627037900003,25.203599351000037],[121.39576256600003,25.182806708000044],[121.42945397200003,25.155991929000038],[121.37875410200002,25.157945054000038],[121.34831790500004,25.137111721000053],[121.33025149800005,25.130682684000078],[121.21094811300009,25.111151434000078],[121.17693118600005,25.100043036000045],[121.12940514400009,25.071112372000073],[121.05933678500003,25.05023834800005],[121.03199303500003,25.029242255000042],[121.00554446700005,25.00067780200004],[120.96387780000009,24.94041575700004],[120.95362389400009,24.90501536700009],[120.92603600400003,24.884711005000042],[120.92058353000004,24.861395575000074],[120.90235436300009,24.836818752000056],[120.90919030000009,24.816880601000037],[120.87696373800009,24.76190827000005],[120.87134850400003,24.74323151200008],[120.85767662900003,24.73700592700004],[120.84750410200002,24.71084219000005],[120.82300866000003,24.68504466400009],[120.82667076900009,24.666083075000074],[120.79932701900009,24.670803127000056],[120.77808678500003,24.665716864000046],[120.74463951900009,24.638820705000057],[120.72787519600001,24.61391836100006],[120.70215905000009,24.60700104400007],[120.69019616000003,24.576117255000042],[120.66773522200003,24.54002513200004],[120.64893639400009,24.48387278900009],[120.57081139400009,24.40493398600006],[120.55811608200008,24.385891018000052],[120.54737389400009,24.35154857000009],[120.51197350400003,24.29555898600006],[120.49317467500009,24.23065827000005],[120.47779381600003,24.20966217700004],[120.43230228000004,24.161078192000048],[120.41863040500004,24.142523505000042],[120.41187584700003,24.096909898000035],[120.39234459700003,24.082912502000056],[120.38550866000003,24.063055731000077],[120.36182701900009,24.048529364000046],[120.34009850400003,24.02179596600007],[120.31812584700003,23.96580638200004],[120.29224694100003,23.911932684000078],[120.25749759200005,23.87840403900009],[120.24073326900009,23.832831122000073],[120.21949303500003,23.81793854400007],[120.18921959700003,23.77480703300006],[120.17554772200003,23.73383209800005],[120.17080496100004,23.69890152000005],[120.1542299140001,23.672211863000086],[120.143609008,23.63803570200008],[120.14086753400011,23.595262518000084],[120.14168913100002,23.537946590000047],[120.1242615330001,23.520020550000083],[120.12502924700004,23.489611410000066],[120.15754360400001,23.491475715000036],[120.13054446700005,23.450995184000078],[120.13477623800009,23.426255601000037],[120.15064537900003,23.398138739000046],[120.1645773890001,23.36171145600008],[120.13014916000009,23.36096024400007],[120.096039259,23.25535716400009],[120.096097504,23.23949089200005],[120.07862389400009,23.206976630000042],[120.08692467500009,23.182033596000053],[120.06202233200008,23.16502513200004],[120.05941816500001,23.15102773600006],[120.086273634,23.12693919500009],[120.09644616000003,23.086411851000037],[120.05941816500001,23.075873114000046],[120.05518639400009,23.04368724200009],[120.07667076900009,23.03742096600007],[120.09245853000004,23.054754950000074],[120.10792076900009,23.048570054000038],[120.10914147200003,23.015448309000078],[120.12671959700003,23.007147528000075],[120.14210045700008,23.034898179000038],[120.16618899800005,23.031724351000037],[120.170258009,23.013373114000046],[120.16016686300009,22.992132880000042],[120.14210045700008,22.980292059000078],[120.16382897200003,22.95384349200009],[120.17554772200003,22.884100653000075],[120.19141686300009,22.852036851000037],[120.20134524800005,22.844142971000053],[120.19646243600005,22.81858958500004],[120.19996178500003,22.79246653900009],[120.21753991000003,22.756740627000056],[120.23617597700002,22.72988515800006],[120.25212649800005,22.68964264500005],[120.23959394600001,22.659084377000056],[120.25896243600005,22.62759023600006],[120.31348717500009,22.577053127000056],[120.32797285200002,22.551703192000048],[120.33122806100005,22.51972077000005],[120.38086998800009,22.48387278900009],[120.40211022200003,22.486802476000037],[120.45720462300005,22.451605536000045],[120.51197350400003,22.424750067000048],[120.51807701900009,22.40696849200009],[120.56967207100001,22.373846747000073],[120.59392337300005,22.32916901200008],[120.62126712300005,22.295070705000057],[120.69695071700005,22.113226630000042],[120.68336022200003,22.079046942000048],[120.68336022200003,22.01386139500005],[120.70435631600003,21.981390692000048],[120.70036868600005,21.948431708000044],[120.72730553500003,21.934475002000056],[120.73609459700003,21.94281647300005],[120.73731530000009,21.966131903000075],[120.76742597700002,21.95929596600007],[120.79737389400009,21.94676341400009],[120.82309004000001,21.928615627000056],[120.84156334700003,21.925482489000046],[120.83570397200003,21.987250067000048],[120.863617384,22.008937893000052],[120.879242384,22.055812893000052],[120.87501061300009,22.28925202000005],[120.88656660200002,22.36595286700009],[120.92286217500009,22.424750067000048],[120.94109134200005,22.483628648000035],[120.94825280000009,22.52680084800005],[120.96062259200005,22.547796942000048],[120.99122155000009,22.58307526200008],[121.02100670700008,22.64789459800005],[121.08025149800005,22.70408763200004],[121.14144941500001,22.74176666900007],[121.17741946700005,22.778631903000075],[121.19027754000001,22.80215078300006],[121.19027754000001,22.84316640800006],[121.20281009200005,22.85651276200008],[121.25562584700003,22.897772528000075],[121.29957116000003,22.95917389500005],[121.31885826900009,23.025539455000057],[121.33708743600005,23.055080471000053],[121.34742272200003,23.083319403000075],[121.38160241000003,23.110663153000075],[121.40007571700005,23.14549388200004],[121.40333092500009,23.21474844000005],[121.40886478000004,23.25458405200004],[121.42676842500009,23.293117580000057],[121.45362389400009,23.325995184000078],[121.46216881600003,23.343003648000035],[121.52149498800009,23.658677476000037],[121.53980553500003,23.69281647300005],[121.57032311300009,23.832098700000074],[121.59449303500003,23.891506252000056],[121.61109459700003,23.982489325000074],[121.62501061300009,24.002386786000045],[121.62891686300009,24.026190497000073],[121.61500084700003,24.042303778000075],[121.60743248800009,24.07684967700004],[121.61304772200003,24.09210846600007],[121.64128665500004,24.12986888200004],[121.65219160200002,24.17218659100007],[121.66285241000003,24.193101304000038],[121.69776451900009,24.21898021000004],[121.69825280000009,24.23151276200008],[121.72144616000003,24.251166083000044],[121.77271569100003,24.30923086100006],[121.75855553500003,24.33234284100007],[121.76644941500001,24.347357489000046],[121.77149498800009,24.380560614000046],[121.78077233200008,24.397609768000052],[121.78003991000003,24.43268463700008],[121.80681399800005,24.460638739000046],[121.84001712300005,24.481350002000056],[121.83008873800009,24.519232489000046],[121.85279381600003,24.530991929000038],[121.85596764400009,24.550523179000038],[121.84083092500009,24.590318101000037],[121.869476759,24.598944403000075],[121.82797285200002,24.622788804000038],[121.81495201900009,24.644354559000078],[121.81421959700003,24.672308661000045],[121.82032311300009,24.699652411000045],[121.80779056100005,24.753119208000044],[121.80884850400003,24.805161851000037],[121.82349694100003,24.85492584800005],[121.83472741000003,24.871527411000045],[121.90577233200008,24.950100002000056]]],[[[121.56348717500009,22.044623114000046],[121.56657962300005,22.082180080000057],[121.49203535200002,22.082180080000057],[121.50261478000004,22.066555080000057],[121.49830162900003,22.048041083000044],[121.53891035200002,22.01821523600006],[121.56690514400009,22.007757880000042],[121.58887780000009,22.023504950000074],[121.56348717500009,22.044623114000046]]],[[[118.39421634200005,24.522691148000035],[118.385392712,24.503817045000062],[118.381414146,24.473511031000044],[118.35480862500003,24.470561718000056],[118.3166568150001,24.49334875900007],[118.28207441500001,24.477362372000073],[118.28956139400009,24.455633856000077],[118.29200280000009,24.424994208000044],[118.28394616000003,24.401271877000056],[118.31821764600011,24.390544350000084],[118.34576026800005,24.417579187000058],[118.3728561050001,24.433201267000072],[118.40362421200007,24.436009837000086],[118.44878503900009,24.415513445000045],[118.463355546,24.431523084000048],[118.46735474900004,24.461827610000057],[118.44958993900002,24.505527833000087],[118.422596613,24.52796832200005],[118.39421634200005,24.522691148000035]]],[[[119.64795983200008,23.603461005000042],[119.64185631600003,23.59406159100007],[119.60629316500001,23.590399481000077],[119.59546959700003,23.604722398000035],[119.56723066500001,23.58852773600006],[119.55925540500004,23.56932200700004],[119.58716881600003,23.54881419500009],[119.55738366000003,23.53461334800005],[119.56397545700008,23.51658763200004],[119.60010826900009,23.52838776200008],[119.61500084700003,23.554632880000042],[119.64063561300009,23.56354401200008],[119.66846764400009,23.54881419500009],[119.66578209700003,23.57330963700008],[119.64795983200008,23.603461005000042]]],[[[119.47706139400009,23.603461005000042],[119.49073326900009,23.590399481000077],[119.50782311300009,23.63239166900007],[119.48943118600005,23.63963450700004],[119.47706139400009,23.603461005000042]]],[[[119.572601759,23.62767161700009],[119.59066816500001,23.63279857000009],[119.59107506600003,23.66351959800005],[119.56641686300009,23.67918528900009],[119.545258009,23.664943752000056],[119.56934655000009,23.63971588700008],[119.572601759,23.62767161700009]]],[[[121.46070397200003,22.659002997000073],[121.47144616000003,22.641791083000044],[121.49496504000001,22.644191799000055],[121.50277754000001,22.66950104400007],[121.49577884200005,22.678534247000073],[121.45753014400009,22.675930080000057],[121.46070397200003,22.659002997000073]]],[[[120.02186078900002,23.45164440700006],[120.04791202000001,23.458084713000062],[120.090300909,23.499395950000064],[120.07493184000009,23.50081817100005],[120.02186078900002,23.45164440700006]]]]}},
{"type":"Feature","properties":{"ISO_A2":"JP","ADMIN":"Japan","ISO_A3":"JPN","BORDER":4},"geometry":{"type":"MultiPolygon","coordinates":[[[[129.7832137380001,31.799627997000073],[129.76775149800005,31.75934479400007],[129.75359134200005,31.743801174000055],[129.71664472700002,31.73037344000005],[129.69060286800004,31.70019805600009],[129.6762976550001,31.666966426000045],[129.655381433,31.659784838000064],[129.6593736750001,31.64193470500004],[129.695977406,31.621447336000074],[129.72934004,31.644232489000046],[129.74170983200008,31.676174221000053],[129.77613366000003,31.72947825700004],[129.79957116000003,31.745428778000075],[129.79859459700003,31.763657945000034],[129.7832137380001,31.799627997000073]]],[[[131.65979027600008,32.47681957400005],[131.69548587300005,32.46824778900009],[131.7172957690001,32.47296784100007],[131.73650149800005,32.49168528900009],[131.71892337300005,32.50873444200005],[131.695648634,32.51146067900004],[131.6821395190001,32.53506094000005],[131.70879259100002,32.585903477000045],[131.74333743600005,32.587958075000074],[131.76685631600003,32.620794989000046],[131.76783287900003,32.64411041900007],[131.800639584,32.68083304800007],[131.8339517390001,32.69866452900004],[131.85592484900008,32.686084221000044],[131.8593856130001,32.717678127000056],[131.8711043630001,32.72679271000004],[131.8730574880001,32.76235586100006],[131.88249759200005,32.78343333500004],[131.92920983200008,32.77826569200005],[131.94809004,32.793402411000045],[131.96241295700008,32.792669989000046],[131.9965926440001,32.810532945000034],[131.99781334700003,32.82965729400007],[131.96851647200003,32.82611725500004],[131.96371504,32.83901601800005],[131.98975670700008,32.854803778000075],[132.00017337300005,32.87128327000005],[131.98218834700003,32.88890208500004],[131.99480228000004,32.903550523000035],[132.02547498600006,32.923595910000074],[132.08118890900005,32.94322970300004],[132.0347898,32.95129061700004],[132.021830657,32.93691682100007],[131.99822024800005,32.93891022300005],[131.97917728000004,32.950384833000044],[131.95923912900003,32.944525458000044],[131.903168165,32.98305898600006],[131.90677350700003,33.01184695400008],[131.929550264,33.047089602000085],[131.98400888800006,33.04554450900008],[132.00945071700005,33.05117422100005],[131.99146569100003,33.06899648600006],[131.97608483200008,33.060248114000046],[131.9580184250001,33.06720612200007],[131.9423835450001,33.09255668600008],[131.929216945,33.068270865000045],[131.905797527,33.07729573600005],[131.88051074700002,33.07504043000006],[131.8632173310001,33.08253329200005],[131.9004012380001,33.12913646000004],[131.86883062000004,33.12876699900005],[131.84262129,33.113267320000034],[131.8095736890001,33.12764888700008],[131.83097601300005,33.16971306700009],[131.8487435400001,33.18799917800004],[131.87224368600005,33.20034414300005],[131.87155536400007,33.21541862600009],[131.902624533,33.26604183300009],[131.88444854400007,33.26830565600005],[131.82217409700002,33.24403243800009],[131.76209241900006,33.253543499000045],[131.71086238200007,33.27017583800006],[131.700043165,33.25409577000005],[131.6545351640001,33.271625235000045],[131.61221917500006,33.25293648400009],[131.5478621750001,33.25495026200008],[131.51037964,33.273403693000034],[131.49838511400003,33.34066626900005],[131.50602854600004,33.36307383900004],[131.532980942,33.366113306000045],[131.55616295700008,33.34638092700004],[131.59623445500006,33.349400219000074],[131.60108483200008,33.364935614000046],[131.63461347700002,33.37396881700005],[131.64031009200005,33.41657135600008],[131.6587020190001,33.42283763200004],[131.697436434,33.406392597000035],[131.737294841,33.48460779900006],[131.729109,33.51254769600007],[131.73991946700005,33.53969961100006],[131.72953798100002,33.58023616300005],[131.68955031300004,33.64134224500009],[131.66736627400007,33.66719200300008],[131.6274520190001,33.667669989000046],[131.59001712300005,33.68301015800006],[131.52104511700009,33.67637079200006],[131.4840322870001,33.653038364000054],[131.47563704700008,33.631565882000075],[131.41770310800007,33.58168167900004],[131.367620482,33.57480177900004],[131.323093352,33.58122055200005],[131.28383445500003,33.58236918000006],[131.2564638120001,33.60495535200005],[131.21986918300001,33.60754487000008],[131.188699101,33.616028293000056],[131.1199716970001,33.62633852100004],[131.10297618100003,33.62425323800005],[131.08130944100003,33.63495514500005],[131.0644637380001,33.65326569200005],[131.0507918630001,33.68187083500004],[131.0097762380001,33.746161200000074],[131.00359134200005,33.77968984600005],[131.0069447090001,33.813087590000066],[130.98551079600009,33.80866486500008],[130.96168053500003,33.81622955900008],[130.96357418600007,33.83323528200009],[130.99068439000007,33.856827183000064],[131.00177079800005,33.887228684000036],[130.99292883500004,33.89766466700007],[131.01531009200005,33.93512604400007],[131.0186856580001,33.956262097000035],[131.007147594,33.96605493200008],[130.97656724600006,33.957366930000035],[130.94947136400003,33.93819283900007],[130.9414168630001,33.91681549700007],[130.91358483200008,33.89118073100008],[130.88085281300005,33.89593952100006],[130.86142341900006,33.92556771800008],[130.83586868700002,33.925729009000065],[130.82822790400007,33.93958513800004],[130.790494512,33.94441130800004],[130.75604891900002,33.94368111300008],[130.7315048380001,33.931848734000084],[130.71705162900003,33.93789297100005],[130.684418165,33.93423086100006],[130.65406334700003,33.89630768400008],[130.63306725400003,33.882473049000055],[130.61207116000003,33.879787502000056],[130.548187696,33.88076406500005],[130.53052819100003,33.86155833500004],[130.50757897200003,33.84906647300005],[130.48894290500004,33.85537344000005],[130.47779381600003,33.82514069200005],[130.45435631600003,33.80768463700008],[130.44800866000003,33.789943752000056],[130.4667595950001,33.78035298600008],[130.46767384400005,33.74356360100006],[130.44459069100003,33.71820709800005],[130.4287215500001,33.71369049700007],[130.38648522200003,33.67723216400009],[130.3454978970001,33.66247159000005],[130.28926262000004,33.681544502000065],[130.29490003900003,33.66435087500008],[130.31839397600004,33.65838070800004],[130.34897188800005,33.64113020500008],[130.39869225400003,33.669134833000044],[130.40750100800005,33.650468484000044],[130.39947835200007,33.61611050100004],[130.39086562400007,33.60487402200005],[130.35804962400005,33.59725860000009],[130.31530703800001,33.59736182000006],[130.29029381600003,33.574204820000034],[130.26543167800003,33.58857951500005],[130.25147545700008,33.60797760600008],[130.2313843830001,33.61884615200006],[130.23170006600003,33.65330638200004],[130.208011242,33.66507643700004],[130.20476321700005,33.63971588700008],[130.18189537900003,33.62905508000006],[130.1595475270001,33.62959605400005],[130.156663622,33.60650551500004],[130.13941491000003,33.59467194200005],[130.10914147200003,33.58974844000005],[130.09205162900003,33.566473700000074],[130.11793053500003,33.55683014500005],[130.1298934250001,33.54002513200004],[130.16187584700003,33.54661692900004],[130.15642337300005,33.525376695000034],[130.10384486200007,33.51190524500004],[130.05529438300005,33.495225676000075],[130.04462561600008,33.48574090400007],[130.04021243600005,33.46124909100007],[130.01685631600003,33.44578685100004],[129.9889460280001,33.448851378000086],[129.9412089010001,33.47953658700004],[129.961273634,33.51276276200008],[129.94214928500003,33.531683661000045],[129.91005948600002,33.54720499900009],[129.8858313830001,33.53584022100006],[129.8545792440001,33.53285385400005],[129.8368347390001,33.513254121000045],[129.84395138900004,33.48121705400007],[129.86687259200005,33.456732489000046],[129.84888756600003,33.44245026200008],[129.81844004700008,33.48421177600005],[129.804244745,33.48182305800009],[129.7872572230001,33.45451903900005],[129.81639218700002,33.426102031000084],[129.86866295700008,33.39744700700004],[129.871025357,33.37817788400008],[129.84629540100002,33.34796460600006],[129.8379012380001,33.28538646000004],[129.81983483200008,33.304754950000074],[129.80746504,33.34813060100004],[129.74416374600003,33.364314257000046],[129.70100788500008,33.35300693100004],[129.67687347600008,33.362418888000036],[129.67318468100007,33.39853911700004],[129.64437910200002,33.36237213700008],[129.62061608200008,33.35927969000005],[129.59241527900008,33.381115210000075],[129.58041276000006,33.371007269000074],[129.57146659700004,33.32063960100004],[129.59149849000005,33.283410159000084],[129.56534239400003,33.27206736000005],[129.5521443460001,33.225838734000035],[129.56413821700005,33.21112702000005],[129.6001082690001,33.208319403000075],[129.62810306100005,33.19179922100005],[129.6455184250001,33.19745514500005],[129.6528426440001,33.17015208500004],[129.6743270190001,33.160223700000074],[129.68702233200008,33.142238674000055],[129.68074700600005,33.11487864500009],[129.7041121750001,33.11180247600004],[129.71216881600003,33.141058661000045],[129.7285262380001,33.122300523000035],[129.74647953500005,33.065590119000035],[129.77495184000009,33.05831396000008],[129.79751505900003,33.07730904400006],[129.82235552400005,33.06267999000005],[129.8238593850001,33.043118812000046],[129.881443666,33.062762999000086],[129.91208162100008,33.03650814800005],[129.9456226420001,33.015757714000074],[129.947601759,32.99506256700005],[129.929725265,32.95764131200008],[129.93122442100002,32.92889427000006],[129.985691116,32.863087170000085],[129.98787790900008,32.84171406100006],[129.9356096470001,32.860488941000085],[129.88320558400005,32.87569270700004],[129.86870212600002,32.853013496000074],[129.84178867900005,32.84441060100005],[129.79439983400005,32.874920702000054],[129.80747728000006,32.88594190900005],[129.80379470800005,32.90246010800007],[129.81466930400006,32.92265535000007],[129.81384491900008,32.96179569800006],[129.82327699500001,32.97953752800004],[129.796959,33.01006172800004],[129.76632809800003,33.01795398200005],[129.7518386370001,32.995939633000035],[129.73578382200003,33.003843619000065],[129.7386319100001,33.019118252000055],[129.7596653390001,33.047889438000084],[129.734711134,33.060248114000046],[129.7201975270001,33.078967687000045],[129.68161222900005,33.094092267000065],[129.66341673400007,33.06902397600004],[129.66065830000002,33.03969659100005],[129.65186151400007,33.03355786700007],[129.6535188360001,33.004253288000086],[129.6324026650001,32.97910666700005],[129.631129363,32.956533586000035],[129.638430137,32.92654636300006],[129.66968834700003,32.90497467700004],[129.67465254,32.880519924000055],[129.69809004,32.839544989000046],[129.72103494200007,32.82707413800006],[129.7327100450001,32.80557017500007],[129.754508987,32.81848265700006],[129.77100670700008,32.81395091400009],[129.778656446,32.791001695000034],[129.8037215500001,32.77228424700007],[129.82398522200003,32.73607005400004],[129.82379517000004,32.72198462400007],[129.85906009200005,32.72451406500005],[129.845693536,32.70127403300006],[129.8259541660001,32.704808136000054],[129.79692255500004,32.69224452900005],[129.81449596100003,32.662551104000045],[129.78297457600002,32.60741488500008],[129.7340049080001,32.58092038700005],[129.74035124,32.56844238700006],[129.7858295540001,32.58310535600009],[129.83228560100008,32.61580479700007],[129.84127669600002,32.63941524900008],[129.85544474200003,32.64008608000006],[129.89492938400008,32.662689399000044],[129.90344364200007,32.68113718500007],[129.95489089500006,32.745171077000066],[129.9908080350001,32.74691678000005],[130.032411657,32.75934986500005],[130.0641668070001,32.784811268000055],[130.093789355,32.79486030900006],[130.1382142650001,32.78884735300005],[130.14809900800003,32.79535371600008],[130.17842736700004,32.79292178700007],[130.19023316200003,32.78348173400008],[130.18681992200004,32.76206462600004],[130.207585429,32.752162345000045],[130.21031382400008,32.73324984100009],[130.1535077000001,32.68619748900005],[130.12813256000004,32.68387054600004],[130.12945734900006,32.641134202000046],[130.16956158300002,32.619092230000035],[130.18148028400003,32.59232724600008],[130.19841204400007,32.602978191000034],[130.23154564000004,32.61001434800005],[130.2539168630001,32.62482330900008],[130.26823067700002,32.65036133400008],[130.30557751000003,32.65143396800005],[130.34170370900006,32.66725767400004],[130.36082701600003,32.687963369000045],[130.35093633100007,32.70406338500004],[130.3757820940001,32.747884438000085],[130.37224368600005,32.793402411000045],[130.339419787,32.856579311000075],[130.30728451900006,32.876298975000054],[130.24358790200006,32.875284097000076],[130.1950023720001,32.844940112000074],[130.15363495200006,32.84245733200004],[130.12231510100003,32.86881610800009],[130.10531660200002,32.87592194200005],[130.1491302510001,32.89641304400004],[130.16359606100002,32.90929081300004],[130.18853629400007,32.91532115800004],[130.20378665500004,32.928900458000044],[130.2042577850001,32.95087110800006],[130.219146927,32.95505985900007],[130.22231019900005,32.97520504800008],[130.20390147900002,32.991781615000036],[130.160492384,33.05410390800006],[130.1319460850001,33.101971727000034],[130.1782332690001,33.13491445500006],[130.21930162,33.15946595200006],[130.24139647700008,33.188234066000064],[130.29044274400007,33.14958862900005],[130.36475670700008,33.140326239000046],[130.37224368600005,33.114894924000055],[130.422211134,33.08258698100008],[130.42847741000003,33.05052317900004],[130.42644290500004,33.007147528000075],[130.43311608200008,32.94171784100007],[130.44125410200002,32.92365143400008],[130.47022545700008,32.90973541900007],[130.5161238940001,32.868475653000075],[130.59302819100003,32.81586334800005],[130.61304772200003,32.781236070000034],[130.59197024800005,32.74872467700004],[130.60141035200002,32.72614166900007],[130.6259871750001,32.704006252000056],[130.576508009,32.69839101800005],[130.54420006600003,32.67670319200005],[130.47527103000004,32.64704010600008],[130.44800866000003,32.62140534100007],[130.47803795700008,32.62201569200005],[130.641449415,32.64834219000005],[130.668304884,32.64874909100007],[130.64128665500004,32.607489325000074],[130.57007897200003,32.55459219000005],[130.567718946,32.53416575700004],[130.59058678500003,32.518459377000056],[130.55046634200005,32.49168528900009],[130.56763756600003,32.47768789300005],[130.5712996750001,32.45189036700009],[130.56202233200008,32.427069403000075],[130.54053795700008,32.416001695000034],[130.51002037900003,32.37848541900007],[130.51246178500003,32.33820221600007],[130.50318444100003,32.32721588700008],[130.4770613940001,32.34902578300006],[130.46168053500003,32.32721588700008],[130.46371504,32.30939362200007],[130.44402103000004,32.28782786700009],[130.431000196,32.257147528000075],[130.35865319100003,32.190659898000035],[130.35865319100003,32.17267487200007],[130.34498131600003,32.16274648600006],[130.33187910200002,32.128607489000046],[130.29851321700005,32.10586172100005],[130.278656446,32.10053131700005],[130.24830162900003,32.13373444200005],[130.20679772200003,32.12067291900007],[130.17986087300005,32.093451239000046],[130.20404738700006,32.07258078500007],[130.20404812200002,32.04828530800006],[130.1729936600001,32.01744955600009],[130.17410608700004,31.998755987000038],[130.20512664400007,31.97167037300005],[130.22043774400004,31.929594515000076],[130.21472979100008,31.88561847400007],[130.1970037420001,31.85848542800005],[130.207855665,31.843695380000042],[130.181000196,31.826971747000073],[130.17143062700006,31.790136657000062],[130.1840926440001,31.758693752000056],[130.1997176440001,31.753729559000078],[130.25928795700008,31.720851955000057],[130.2973738940001,31.669989325000074],[130.33619225400003,31.626166083000044],[130.33765709700003,31.607896226000037],[130.32447350400003,31.586737372000073],[130.3344832690001,31.56240469000005],[130.31706790500004,31.493068752000056],[130.30476023300002,31.476634180000076],[130.293268129,31.44470403200006],[130.24488366000003,31.404852606000077],[130.22828209700003,31.381293036000045],[130.19597415500004,31.412054755000042],[130.16236412900003,31.421942450000074],[130.11898847700002,31.408636786000045],[130.170258009,31.377020575000074],[130.19654381600003,31.35419342700004],[130.20777428500003,31.33661530200004],[130.192952418,31.31515659300004],[130.2222047140001,31.246299862000058],[130.24878991000003,31.244126695000034],[130.2973738940001,31.253729559000078],[130.45114447600008,31.249388072000045],[130.49480228000004,31.22724030200004],[130.50660241000003,31.21279531500005],[130.52173912900003,31.16868724200009],[130.53679446700005,31.162176825000074],[130.573578321,31.16742584800005],[130.5864363940001,31.155422268000052],[130.60368899800005,31.172349351000037],[130.63982181100005,31.186468817000048],[130.66195722700002,31.258612372000073],[130.66032962300005,31.271429755000042],[130.6236819840001,31.275646770000037],[130.602875196,31.301988023000035],[130.57349694100003,31.314601955000057],[130.56267337300005,31.334377346000053],[130.55046634200005,31.375148830000057],[130.5205184250001,31.432440497000073],[130.5180770190001,31.472398179000038],[130.52914472700002,31.51325104400007],[130.55844160200002,31.56631094000005],[130.56763756600003,31.600734768000052],[130.60222415500004,31.63825104400007],[130.61304772200003,31.68235911700009],[130.64478600400003,31.71474844000005],[130.6860457690001,31.725531317000048],[130.73796468800003,31.70843859000007],[130.7763778000001,31.710272528000075],[130.80453535200002,31.687241929000038],[130.81031334700003,31.650946356000077],[130.79900149800005,31.611721096000053],[130.7763778000001,31.579901434000078],[130.758636915,31.563788153000075],[130.73601321700005,31.559475002000056],[130.7192488940001,31.570705471000053],[130.72234134200005,31.597316799000055],[130.71168053500003,31.614406643000052],[130.63787805900006,31.622594691000074],[130.61389305800003,31.612066478000088],[130.5908888460001,31.585987911000075],[130.59831790500004,31.573431708000044],[130.63689212300005,31.555243231000077],[130.69556725400003,31.545803127000056],[130.70533287900003,31.52875397300005],[130.69252519000008,31.485572010000055],[130.70341738600007,31.45930455100006],[130.74732506600003,31.435492255000042],[130.7695418630001,31.402411200000074],[130.79525160600008,31.344425107000063],[130.79664629800004,31.325345784000035],[130.78967014700004,31.275148674000036],[130.7803691150001,31.242514662000076],[130.753940574,31.20388265300005],[130.76478445300006,31.18650933200007],[130.7426863940001,31.12128327000005],[130.68897545700008,31.09015534100007],[130.66333941000005,31.069506364000063],[130.6577220260001,31.00319120000006],[130.67794304400002,31.00501043400004],[130.72542805,31.04703956700007],[130.7675887380001,31.056463934000078],[130.79235710500006,31.077847115000054],[130.81951105400003,31.08726047400006],[130.87068066200004,31.09601851800005],[130.92400149800005,31.112982489000046],[130.95622806100005,31.13117096600007],[130.97999108200008,31.155991929000038],[131.00660241000003,31.199774481000077],[131.00815940200005,31.22108228400009],[131.0329508970001,31.227524446000075],[131.06024973900003,31.225314840000067],[131.13211394400003,31.279513102000067],[131.12474719500005,31.288345546000073],[131.09259076700005,31.27605066400008],[131.082986237,31.297055970000088],[131.10438183500003,31.309449248000078],[131.1040145190001,31.32884349200009],[131.083343946,31.338446356000077],[131.02545128600002,31.354095225000037],[131.01361073600003,31.363069850000045],[131.02807477500005,31.401785761000042],[131.045258009,31.427476304000038],[131.07178795700008,31.449611721000053],[131.1093856130001,31.464544989000046],[131.15064537900003,31.47134023600006],[131.17863301300008,31.452428893000047],[131.21265709700003,31.454738674000055],[131.238617384,31.42841217700004],[131.24984785200002,31.38812897300005],[131.26587975400003,31.38117096600007],[131.325043165,31.38812897300005],[131.34546959700003,31.394964911000045],[131.3637801440001,31.419663804000038],[131.37623131600003,31.474432684000078],[131.39372806100005,31.491034247000073],[131.3852645190001,31.52692291900007],[131.39079837300005,31.548814195000034],[131.411468946,31.58612702000005],[131.45533287900003,31.620917059000078],[131.450531446,31.65110911700009],[131.47527103000004,31.762152411000045],[131.49634850400003,31.79596588700008],[131.468042276,31.80489943300006],[131.447621863,31.847434831000044],[131.4472762380001,31.876898505000042],[131.45857033700008,31.905451862000064],[131.53052845600007,32.10739128200004],[131.5503272420001,32.16946608900008],[131.56241690000002,32.21791637100006],[131.580414259,32.253973700000074],[131.59253991000003,32.30365631700005],[131.60279381600003,32.323553778000075],[131.619476759,32.337103583000044],[131.63347415500004,32.39549388200004],[131.65308678500003,32.401271877000056],[131.68913158600003,32.421737335000046],[131.65662940000004,32.45735773000007],[131.65979027600008,32.47681957400005]]],[[[134.689789259,33.82807038000004],[134.71705162900003,33.84796784100007],[134.66081790500004,33.84638092700004],[134.64625084700003,33.85309479400007],[134.66236412900003,33.875881252000056],[134.68677819100003,33.88621653900009],[134.70289147200003,33.906236070000034],[134.70257054,33.93726674100009],[134.69596250000006,33.95189325700005],[134.662916031,33.96886860900008],[134.637016,34.01066622400009],[134.62425075300007,33.99758112900008],[134.59978274800005,34.009466864000046],[134.59522545700008,34.04596588700008],[134.607758009,34.067613023000035],[134.60518737900009,34.110776919000045],[134.62273196700005,34.134466864000046],[134.628265821,34.17792389500005],[134.62789552800007,34.23308190400007],[134.610349369,34.216027454000084],[134.5942488940001,34.232123114000046],[134.560883009,34.22142161700009],[134.4952425680001,34.22423534200004],[134.46862330400006,34.20746014400004],[134.43976701100007,34.20834943300008],[134.41570071700005,34.22418854400007],[134.39665774800005,34.24945709800005],[134.35767662900003,34.24640534100007],[134.32178795700008,34.26894765800006],[134.2968856130001,34.27529531500005],[134.2578231130001,34.294175523000035],[134.27125084700003,34.32306549700007],[134.2168074880001,34.35504791900007],[134.1904403000001,34.325384833000044],[134.16627037900003,34.32485586100006],[134.16163170700008,34.362494208000044],[134.15300540500004,34.38572825700004],[134.1274520190001,34.38979726800005],[134.12281334700003,34.36660390800006],[134.10075931100005,34.376166083000044],[134.08130944100003,34.347723700000074],[134.04615319100003,34.35504791900007],[134.00123131600003,34.35224030200004],[133.94654381600003,34.376166083000044],[133.89966881600003,34.364162502000056],[133.87419681100005,34.32831452000005],[133.84359785200002,34.31224192900004],[133.78199303500003,34.288275458000044],[133.70142662900003,34.23126862200007],[133.67261803500003,34.22313060100004],[133.65447024800005,34.23895905200004],[133.637543165,34.233587958000044],[133.57935631600003,34.24941640800006],[133.5722762380001,34.242173570000034],[133.62037194100003,34.21165599200009],[133.6411238940001,34.18463776200008],[133.64812259200005,34.124660549000055],[133.64128665500004,34.07916901200008],[133.62086022200003,34.04555898600006],[133.587250196,34.02033112200007],[133.54859459700003,33.98094310100004],[133.51099694100003,33.96523672100005],[133.4863387380001,33.96674225500004],[133.41480553500003,33.98574453300006],[133.35994768400008,33.98270960000008],[133.322607543,33.990552810000054],[133.29982343000006,33.98102486000005],[133.2790455600001,33.983276759000034],[133.24887447000003,33.97410853100007],[133.24368022400006,33.95625729500006],[133.173292231,33.94058406500005],[133.146703956,33.93154110200004],[133.1270108880001,33.93329063900006],[133.08660030500005,33.94931331500004],[133.06031334700003,33.99135976800005],[133.0318488380001,34.052734410000085],[132.94238361200007,34.14126203700005],[132.9261582900001,34.11166809600007],[132.93033802800005,34.094375970000044],[132.91911043000005,34.06840607300006],[132.8623590960001,34.055893542000035],[132.84718604500006,34.03996357900007],[132.76986156700002,33.996270044000084],[132.7724715500001,33.95587799700007],[132.75748129400006,33.93494834400008],[132.7549368770001,33.91032024900005],[132.712370372,33.904548780000084],[132.69008267200002,33.81390538000005],[132.69711347700002,33.756740627000056],[132.676605665,33.71564362200007],[132.64547459300002,33.68958766600008],[132.60173587300005,33.66038646000004],[132.50061234100008,33.62499820900007],[132.47046959700003,33.60211823100008],[132.44166100400003,33.57294342700004],[132.42847741000003,33.54759349200009],[132.41236412900003,33.53497955900008],[132.2937458780001,33.48538023100008],[132.26463165100006,33.464073502000076],[132.24068462600007,33.464576055000066],[132.22188075000008,33.45402577700008],[132.19976662700003,33.455010144000084],[132.155280026,33.437046147000046],[132.16657605400007,33.42020372400009],[132.13548759700006,33.42002259800006],[132.094319993,33.40848712800005],[132.0515547,33.37604022700009],[132.02963653000006,33.36524659100007],[132.02548739100007,33.344903696000074],[132.1102955670001,33.386727643000086],[132.1015677480001,33.37013383500005],[132.13066048300004,33.36356636600004],[132.1499849060001,33.389167102000044],[132.17973878400005,33.40698609900005],[132.253606956,33.44070882600005],[132.2785860240001,33.447300719000054],[132.30780389000006,33.46428988300005],[132.32223273500006,33.45755532700008],[132.3445962300001,33.46715465400007],[132.38111412900003,33.466050523000035],[132.42115319100003,33.44448476800005],[132.40414472700002,33.43764883000006],[132.40805097700002,33.41657135600008],[132.3911238940001,33.39789459800005],[132.3906356130001,33.374986070000034],[132.41553795700008,33.341457424000055],[132.384125196,33.33087799700007],[132.37867272200003,33.31948476800005],[132.42012139200006,33.30435563900005],[132.48680352500003,33.316068149000046],[132.5256097680001,33.31466423100005],[132.484735461,33.29084085900007],[132.48457057000007,33.27652591800006],[132.53682887000002,33.242584289000035],[132.54836358700004,33.226902944000074],[132.51376684500008,33.21334291800008],[132.50266988300007,33.19873474700006],[132.505077281,33.18123508500008],[132.48570176400005,33.169153220000055],[132.461201039,33.191403309000066],[132.45965886500005,33.20401231800008],[132.42012383400004,33.209824239000056],[132.39467584800002,33.20389482100006],[132.398647482,33.18718380200005],[132.42366068900003,33.19917609400005],[132.44924661700009,33.185075323000035],[132.43539472700002,33.15582916900007],[132.4502059250001,33.13666413000004],[132.4795028000001,33.135972398000035],[132.48755944100003,33.12547435100004],[132.46273847700002,33.105454820000034],[132.46900475400003,33.095770575000074],[132.46273847700002,33.067694403000075],[132.45183353000004,33.05288320500006],[132.47149541200008,33.04068590400004],[132.50147545700008,32.99323151200008],[132.48291855800005,32.97050124400005],[132.494039466,32.94893138500004],[132.47227232600005,32.933672312000056],[132.49048912900003,32.90119049700007],[132.52076256600003,32.88930898600006],[132.51734459700003,32.909409898000035],[132.52418053500003,32.93024323100008],[132.57203209700003,32.923081773000035],[132.57545006600003,32.93183014500005],[132.61841881600003,32.90688711100006],[132.70915774800005,32.909409898000035],[132.70516686400003,32.89716469000007],[132.6630072600001,32.85680765600006],[132.64600159500003,32.825692004000075],[132.623376882,32.79949569300004],[132.61990336400004,32.772990306000054],[132.62766076800006,32.752093098000046],[132.66464317600003,32.77886830400007],[132.71140079600002,32.78952538900006],[132.75757897200003,32.752386786000045],[132.7915145190001,32.740668036000045],[132.81218509200005,32.740301825000074],[132.8584090500001,32.769598700000074],[132.9150496750001,32.77798086100006],[132.95679772200003,32.75861237200007],[132.9765731130001,32.72451406500005],[133.00220787900003,32.71165599200009],[133.02800540500004,32.72064850500004],[133.01734459700003,32.757879950000074],[132.95547713300004,32.81461873300009],[132.95509926500006,32.85262854200005],[132.96105903500006,32.86410052600007],[132.98990530800006,32.862576095000065],[133.0024520190001,32.86933014500005],[133.00928795700008,32.90346914300005],[132.994412721,32.948993177000034],[133.001702206,32.98695675100004],[133.01825880900003,33.024023267000075],[133.052847681,33.03949864300006],[133.0898543630001,33.01927317900004],[133.0971161340001,33.048111006000056],[133.13238695400003,33.08902945600005],[133.16503341100008,33.13719955500005],[133.19014923300006,33.16331643500007],[133.21794681100005,33.16583893400008],[133.21533962500007,33.187503519000074],[133.253265821,33.21906159100007],[133.25847415500004,33.24213288000004],[133.270843946,33.25950755400004],[133.2519637380001,33.29474518400008],[133.25456790500004,33.326239325000074],[133.26937910200002,33.346869208000044],[133.31234785200002,33.37555573100008],[133.32601972700002,33.368801174000055],[133.36500084700003,33.387600002000056],[133.41480553500003,33.39305247600004],[133.4550887380001,33.41469961100006],[133.45036868600005,33.430853583000044],[133.46590938600002,33.454324098000086],[133.58930516600003,33.501933551000036],[133.60773318400004,33.51582468000004],[133.67214782000008,33.528902337000034],[133.73939679,33.537506879000034],[133.76305219800008,33.51978440700009],[133.85490166900001,33.50009453000007],[133.92520516600007,33.49369326100009],[133.94214849600007,33.45887910400006],[133.9687606130001,33.43569570500006],[134.025536461,33.41540034500008],[134.0400496750001,33.384100653000075],[134.07997292100004,33.33904807300007],[134.107960423,33.31492348200004],[134.1062243450001,33.294332361000045],[134.14176414200006,33.29179326500008],[134.17652712200004,33.23612782100008],[134.19025513100007,33.24832929400009],[134.19849694100003,33.31028880400004],[134.21452884200005,33.376613674000055],[134.2446336050001,33.46075022600007],[134.2732050850001,33.50800352400006],[134.313701067,33.54307917700004],[134.312348015,33.57250647400008],[134.339452853,33.57706668000009],[134.36052417400003,33.58870858700004],[134.36376953800004,33.600755389000085],[134.38862168100002,33.623634081000034],[134.3761745380001,33.63565700800007],[134.40402239800005,33.65447756000009],[134.4573003920001,33.67092600800004],[134.53844025700005,33.71685772500007],[134.54637791100004,33.73250210200007],[134.57066101500004,33.736448978000055],[134.57937739500005,33.76417006200006],[134.607758009,33.76601797100005],[134.616799862,33.782601997000086],[134.65186608200008,33.786444403000075],[134.66465403400002,33.79848707600007],[134.73230385400007,33.825951916000065],[134.722031249,33.83407324800004],[134.689789259,33.82807038000004]]],[[[133.12134850400003,36.10976797100005],[133.09351647200003,36.102769273000035],[133.08130944100003,36.080145575000074],[133.0791121750001,36.02094147300005],[133.12322024800005,36.062933661000045],[133.13542728000004,36.089544989000046],[133.12134850400003,36.10976797100005]]],[[[133.0742293630001,36.12791575700004],[133.03874759200005,36.13080475500004],[132.96949303500003,36.082017320000034],[132.94939212300005,36.06195709800005],[132.9814559250001,36.03986237200007],[133.00407962300005,36.04828522300005],[132.99040774800005,36.077053127000056],[133.01490319100003,36.079779364000046],[133.02702884200005,36.056301174000055],[133.05583743600005,36.061753648000035],[133.0517684250001,36.08551666900007],[133.05836022200003,36.103176174000055],[133.08716881600003,36.11078522300005],[133.0742293630001,36.12791575700004]]],[[[133.422211134,34.445013739000046],[133.41911868600005,34.46792226800005],[133.45655358200008,34.472357489000046],[133.50114993600005,34.492173570000034],[133.5224715500001,34.48305898600006],[133.50928795700008,34.462103583000044],[133.470062696,34.42397695500006],[133.5688582690001,34.45990631700005],[133.5962020190001,34.48232656500005],[133.66944420700008,34.51080963700008],[133.69996178500003,34.52594635600008],[133.72575931100005,34.51642487200007],[133.768890821,34.48802317900004],[133.79444420700008,34.44293854400007],[133.82642662900003,34.437567450000074],[133.82178795700008,34.462144273000035],[133.85906009200005,34.462876695000034],[133.93628991000003,34.451239325000074],[133.95386803500003,34.473822333000044],[133.96656334700003,34.50641510600008],[133.98316491000003,34.528631903000075],[134.00513756600003,34.53384023600006],[134.04566491000003,34.56391022300005],[134.04615319100003,34.58152903900009],[134.01929772200003,34.59682851800005],[133.98406009200005,34.589056708000044],[133.94255618600005,34.555121161000045],[133.925140821,34.57786692900004],[133.97925866000003,34.605902411000045],[134.02881920700008,34.60887278900009],[134.0473738940001,34.60464101800005],[134.08366946700005,34.58152903900009],[134.14795983200008,34.596991278000075],[134.17725670700008,34.614447333000044],[134.1782332690001,34.64883047100005],[134.22022545700008,34.66351959800005],[134.24480228000004,34.691473700000074],[134.27881920700008,34.69822825700004],[134.2168074880001,34.71190013200004],[134.2207137380001,34.72846100500004],[134.24781334700003,34.71816640800006],[134.3266707690001,34.704331773000035],[134.3540145190001,34.705145575000074],[134.357758009,34.72736237200007],[134.3813582690001,34.73981354400007],[134.4087020190001,34.726263739000046],[134.43140709700003,34.730454820000034],[134.4731551440001,34.77684153900009],[134.51905358200008,34.75971100500004],[134.54908287900003,34.77326080900008],[134.5669051440001,34.76654694200005],[134.6962996750001,34.77619049700007],[134.74439537900003,34.76654694200005],[134.80046634200005,34.730902411000045],[134.833343946,34.72138092700004],[134.90762493400007,34.67760643300005],[134.9370248570001,34.667368869000086],[134.960297071,34.64362213700008],[135.0168455700001,34.642155986000034],[135.04576878800003,34.624966887000085],[135.07954647400004,34.62777575900009],[135.11131213300007,34.64038240500008],[135.1839760470001,34.65115585800004],[135.186030437,34.67490879400009],[135.2044032770001,34.677260673000035],[135.21090197,34.65518024800008],[135.24234854000008,34.650751996000054],[135.22006657000009,34.69077377000008],[135.25489028200002,34.70091002200007],[135.25360478100004,34.67699777100006],[135.28730090800002,34.67996987600009],[135.286862671,34.70544269800007],[135.3412673680001,34.708509605000074],[135.38121442800002,34.68126282900005],[135.41293379,34.69208405200004],[135.403590677,34.65891218200005],[135.4157815220001,34.60961152200008],[135.41437915100005,34.55812547000005],[135.390996836,34.52364861700005],[135.37320827300005,34.51893221900008],[135.372562312,34.48451311600007],[135.31968984900004,34.428758197000036],[135.256564013,34.37675475200007],[135.20898538000006,34.34227764500008],[135.105030715,34.31811688100004],[135.0712996750001,34.28579336100006],[135.06104364700002,34.267274919000045],[135.09266843600005,34.25988713800007],[135.13542728000004,34.22768789300005],[135.148773634,34.185614325000074],[135.16585549400008,34.18672339300008],[135.1899520190001,34.149603583000044],[135.18702233200008,34.135931708000044],[135.12283938400003,34.137101115000064],[135.13062584700003,34.122381903000075],[135.09449303500003,34.067613023000035],[135.11109459700003,34.065741278000075],[135.15308678500003,34.04857005400004],[135.16277103000004,34.03974030200004],[135.153168165,34.009751695000034],[135.11327708800002,33.99338693300007],[135.0919983990001,33.99203611100006],[135.07573725100008,33.97748793800008],[135.0773346210001,33.95898546600006],[135.1012224010001,33.954437547000055],[135.06955997700004,33.93066660300008],[135.06545384900005,33.88557181900006],[135.11868351100009,33.89293870400007],[135.15275760100008,33.875854179000044],[135.17335045700008,33.838324286000045],[135.19646243600005,33.81476471600007],[135.22984796100002,33.798554135000074],[135.23258264800006,33.781998007000084],[135.26434503500002,33.78131573100006],[135.29402077300006,33.76371566600005],[135.312102234,33.76552878400008],[135.3916121750001,33.71356842700004],[135.4023543630001,33.70111725500004],[135.34294453100006,33.69389835100009],[135.3276951260001,33.67119293500008],[135.38070722700002,33.63711172100005],[135.39234459700003,33.61888255400004],[135.387324143,33.594067317000054],[135.39750451600003,33.58258693600004],[135.44483483200008,33.550848700000074],[135.490763022,33.54122717300004],[135.637543165,33.49461497600004],[135.7655706070001,33.48154548800005],[135.77075201000002,33.46822920000005],[135.75384601400003,33.45157716400007],[135.78926326300007,33.441062324000086],[135.78296949000003,33.47289128700004],[135.79799322100007,33.495161487000075],[135.82806328700008,33.513648259000036],[135.8847762380001,33.528998114000046],[135.91253492700002,33.54768686700004],[135.95987107500002,33.59308822500009],[135.93873646200007,33.60394802800005],[135.937066684,33.61747297900007],[135.95728600400003,33.62571849200009],[135.96159915500004,33.64252350500004],[135.98072350400003,33.65298086100006],[136.06931712800008,33.84704388300008],[136.1047442250001,33.89141140000004],[136.1425887380001,33.89362213700008],[136.16480553500003,33.91616445500006],[136.19890384200005,33.92088450700004],[136.2275496750001,33.96173737200007],[136.20484459700003,33.97142161700009],[136.23218834700003,33.99315013200004],[136.24732506600003,33.97150299700007],[136.27320397200003,33.97142161700009],[136.2797957690001,34.01732005400004],[136.25269616000003,34.04344310100004],[136.24382571700005,34.064154364000046],[136.21851647200003,34.067613023000035],[136.24634850400003,34.103583075000074],[136.2607528000001,34.105129299000055],[136.28345787900003,34.081284898000035],[136.3134871750001,34.094956773000035],[136.3164168630001,34.11432526200008],[136.29704837300005,34.12567780200004],[136.29900149800005,34.154282945000034],[136.33668053500003,34.184027411000045],[136.38428795700008,34.20425039300005],[136.4165145190001,34.20425039300005],[136.4716903000001,34.23895905200004],[136.5029403000001,34.22907135600008],[136.5058699880001,34.24640534100007],[136.52295983200008,34.273098049000055],[136.5605574880001,34.259466864000046],[136.57406660200002,34.280503648000035],[136.59253991000003,34.26264069200005],[136.6547957690001,34.286037502000056],[136.6670028000001,34.29791901200008],[136.6762801440001,34.32640208500004],[136.70679772200003,34.33429596600007],[136.72339928500003,34.331284898000035],[136.70264733200008,34.29515208500004],[136.72584069100003,34.291001695000034],[136.75660241000003,34.296087958000044],[136.7734481130001,34.307847398000035],[136.80746504,34.28733958500004],[136.80746504,34.307847398000035],[136.84644616000003,34.30756256700005],[136.85621178500003,34.294175523000035],[136.848480665,34.273098049000055],[136.82960045700008,34.26654694200005],[136.78793379,34.277411200000074],[136.77515709700003,34.26186758000006],[136.79420006600003,34.253119208000044],[136.83545983200008,34.24640534100007],[136.88599694100003,34.26044342700004],[136.90023847700002,34.282538153000075],[136.897797071,34.30097077000005],[136.8833113940001,34.32831452000005],[136.89437910200002,34.34271881700005],[136.87525475400003,34.36933014500005],[136.8930770190001,34.38239166900007],[136.91675866000003,34.376166083000044],[136.9257918630001,34.41290924700007],[136.9160262380001,34.43398672100005],[136.89633222700002,34.43341705900008],[136.8735457690001,34.44708893400008],[136.86890709700003,34.46735260600008],[136.85230553500003,34.485541083000044],[136.80355879,34.49526601800005],[136.752289259,34.513332424000055],[136.677093946,34.55426666900007],[136.63298587300005,34.591131903000075],[136.61207116000003,34.59589264500005],[136.56812584700003,34.59638092700004],[136.5400496750001,34.61570872600004],[136.54639733200008,34.651597398000035],[136.5195418630001,34.680853583000044],[136.53493993900008,34.765695099000084],[136.61611454500007,34.85350057100004],[136.63931594000007,34.889048701000036],[136.64825961300005,34.911600243000066],[136.64242597700002,34.94806549700007],[136.6591903000001,34.98395416900007],[136.7255965500001,35.02724844000005],[136.7784691060001,35.020401545000084],[136.83932725500006,35.03251957900005],[136.83011829400004,35.07883139300009],[136.843151734,35.07504335100003],[136.84886474000007,35.03946941500004],[136.871821469,35.093395273000056],[136.88572989700003,35.08919548600005],[136.87985931700007,35.04383685700009],[136.8658805010001,35.02569016400008],[136.82346477800002,34.95485542300008],[136.8229381970001,34.89562323900009],[136.82772581000006,34.87194149700008],[136.856286199,34.85649521600004],[136.8647340120001,34.821545090000086],[136.8428861010001,34.76907728900005],[136.8559676440001,34.73981354400007],[136.913204285,34.71087424700005],[136.94539164900004,34.69847069000008],[136.9714965940001,34.69641132500004],[136.9626499850001,34.73496420500004],[136.93726647200003,34.74603913000004],[136.9155640360001,34.77596001200004],[136.92574639300005,34.82827095600004],[136.9384871750001,34.860988674000055],[136.96949303500003,34.91160716400009],[136.98259524800005,34.89826080900008],[136.9624035280001,34.85799564600006],[136.9582313730001,34.83235218500005],[136.9766259920001,34.82550679500008],[137.01824785300005,34.78328903500005],[137.050165591,34.777606125000034],[137.0727086490001,34.78496379100005],[137.14053338200006,34.78781337000004],[137.1714708070001,34.78404980300007],[137.18873911600008,34.770111700000086],[137.19443232100002,34.80699872100007],[137.22282679300008,34.81626329100004],[137.29792810200001,34.79719109500007],[137.32431631200006,34.766844656000046],[137.309256237,34.72566427800007],[137.282254577,34.73117851600006],[137.2597158000001,34.70414459500006],[137.241111284,34.69667799400008],[137.16765384200005,34.656683661000045],[137.1210200710001,34.63766082400008],[137.10108483200008,34.63914622600004],[137.07283016600002,34.664599003000035],[137.05221990300004,34.646803169000066],[137.04248611100002,34.61757853200004],[137.01609532100008,34.578784710000036],[137.04482177,34.58063023500006],[137.09582116700005,34.59323397800006],[137.14072680900006,34.589289111000085],[137.33970288,34.64744751300009],[137.4275572790001,34.66774579100007],[137.54699814000003,34.67728852500005],[137.595062696,34.67719147300005],[137.68350648400008,34.67075459900008],[137.79216556100005,34.63939036700009],[137.8927975680001,34.667696567000064],[137.96758503500007,34.666540387000055],[138.04723701,34.65463981500005],[138.1901763740001,34.601227717000086],[138.23328976100004,34.59458877000009],[138.22505168600003,34.61630119900008],[138.20093834700003,34.62156810100004],[138.19361412900003,34.641506252000056],[138.21598919100006,34.712282661000074],[138.24950579000006,34.73902319000007],[138.29322798300007,34.76577441500007],[138.3148726610001,34.79836868000007],[138.3408309250001,34.82648346600007],[138.32829837300005,34.85529205900008],[138.3533526120001,34.90411501600005],[138.367107895,34.917309439000064],[138.51429303400005,34.98017780400005],[138.53161744200008,35.016923751000036],[138.52098226200008,35.01937347200004],[138.4948830940001,35.00073149500008],[138.49788298200008,35.03256950200006],[138.52765127300006,35.050808017000065],[138.55408217500008,35.096069379000085],[138.58678602100008,35.11436020000008],[138.6372695660001,35.11598535200005],[138.69618557600006,35.13897567500004],[138.74422004400003,35.13484289300004],[138.8033316850001,35.121909308000056],[138.83853700300006,35.099525698000036],[138.8534354960001,35.07822164400005],[138.907562696,35.03066640800006],[138.89380944100003,35.02000560100004],[138.85541578700008,35.01902692800007],[138.78608990100008,35.026438762000055],[138.7618935590001,34.97990260500006],[138.7636926460001,34.95605640500008],[138.77849368600005,34.92202383000006],[138.7767874000001,34.90482837700006],[138.753838793,34.87946727500008],[138.7622176440001,34.84642161700009],[138.74812686400003,34.81320925300008],[138.774261915,34.751288153000075],[138.74400124200008,34.72889189500006],[138.7465926440001,34.691473700000074],[138.7778426440001,34.663316148000035],[138.7713901310001,34.64731395300004],[138.81999759200005,34.60236237200007],[138.835297071,34.59589264500005],[138.86094893900008,34.61349798100008],[138.89597911200008,34.629045776000055],[138.9408259810001,34.661107187000084],[138.9824324880001,34.64447663000004],[138.99366295700008,34.64984772300005],[138.98560631600003,34.683294989000046],[139.00261478000004,34.72182851800005],[138.99875872200005,34.73328434200005],[139.02632726900003,34.764246600000035],[139.042983467,34.76756908400006],[139.07481770100003,34.80896906900006],[139.0883895190001,34.84227122600004],[139.119297985,34.86738948800007],[139.14087975400003,34.87055084800005],[139.14828535200002,34.90509674700007],[139.1425887380001,34.942287502000056],[139.13070722700002,34.96515534100007],[139.100602546,34.974755422000044],[139.08654809100005,34.99730546900008],[139.09709101300007,35.01149501300006],[139.1059676440001,35.04938385600008],[139.07587403600007,35.05317728600005],[139.07673175500008,35.075094009000054],[139.08847267500005,35.11005777000008],[139.12077055300006,35.15025745000008],[139.15805097700002,35.13646067900004],[139.14242597700002,35.184027411000045],[139.145957437,35.235226985000054],[139.18353782100007,35.26326671600009],[139.23336693300007,35.28532441900006],[139.2820504240001,35.29619758400008],[139.4148557200001,35.318814659000054],[139.46691429100008,35.31331858400006],[139.486664259,35.30052317900004],[139.52660337300006,35.30203700900006],[139.54851321700005,35.28790924700007],[139.62395895200007,35.211617096000055],[139.60179274300003,35.201835319000054],[139.6143019980001,35.17404269600007],[139.6114520120001,35.141972689000056],[139.625336134,35.13178131700005],[139.64151397500007,35.139633865000064],[139.66431725400003,35.134833075000074],[139.68506920700008,35.14044830900008],[139.660589416,35.17823249200006],[139.666456229,35.19389322400008],[139.68879598,35.20913368600009],[139.7280246570001,35.20708410000009],[139.725922071,35.233303127000056],[139.745453321,35.24896881700005],[139.7342280480001,35.26520292300006],[139.69418379,35.268744208000044],[139.676899305,35.29153718100008],[139.648358142,35.297890507000034],[139.65723847900006,35.37702195400004],[139.6357919080001,35.40163242700004],[139.6499959250001,35.41055080800004],[139.686859571,35.40070221600007],[139.68054456100003,35.43838609900007],[139.6423018370001,35.45426118200004],[139.66689706,35.46793427800009],[139.6882217330001,35.46110650400004],[139.71407684000008,35.46473072500004],[139.77857506600003,35.50185781500005],[139.78687584700003,35.52716705900008],[139.79905634500005,35.54001012800006],[139.78480408500002,35.580466096000066],[139.789146748,35.60324341200004],[139.80682518800006,35.63819253400004],[139.83727706700006,35.61454952400004],[139.85027103000004,35.63751862200007],[139.8911238940001,35.623114325000074],[139.89716783000006,35.614373758000056],[139.9395792910001,35.63689149800007],[139.92053189700005,35.66206667300008],[139.95041397400007,35.67259201500008],[140.008949863,35.659759819000044],[140.07960045700008,35.603583075000074],[140.09015640100006,35.54084883200005],[140.07188712900006,35.542856802000074],[140.005859289,35.47342712000005],[139.98233663500002,35.458668431000035],[139.95867218600006,35.45620969700008],[139.94075591,35.44251764400008],[139.9128234540001,35.434436180000034],[139.900121044,35.42310445500004],[139.907865055,35.37871859200004],[139.8927430760001,35.36418368900007],[139.84477673600009,35.37620508400005],[139.824667997,35.34700932700008],[139.82415804200002,35.32842398900004],[139.78215300800002,35.31417710000005],[139.84933413400006,35.29355360000005],[139.84831790500004,35.27171458500004],[139.86632662800002,35.24620537700008],[139.86515060700003,35.22409896400006],[139.82460882400005,35.199063900000056],[139.81897429600008,35.157662877000064],[139.8364363940001,35.135199286000045],[139.83578535200002,35.099269924000055],[139.84742272200003,35.06932200700004],[139.8462020190001,35.04682038000004],[139.83578535200002,35.02724844000005],[139.8676863940001,35.013332424000055],[139.85865319100003,34.98680247600004],[139.8271049220001,34.99019351800007],[139.794453804,34.97344853800007],[139.75277736500004,34.97580871200006],[139.756993555,34.956283674000076],[139.77881155300008,34.95413884000004],[139.82318224300002,34.934921744000064],[139.821984262,34.91210983000008],[139.843099617,34.89998186000008],[139.9263958460001,34.90735665200009],[139.9621388810001,34.940850503000036],[139.9640643580001,34.975959644000056],[139.99096763300008,35.02050211200009],[140.05553662300008,35.058868491000055],[140.075487834,35.05926940100005],[140.10080523400006,35.07575134500007],[140.10595281100007,35.097603003000074],[140.134602814,35.12264536400005],[140.15551320300006,35.118075357000066],[140.23738075200004,35.11242952500004],[140.2533254880001,35.134623945000044],[140.32121361200007,35.13039407800005],[140.33353722800007,35.14234798800004],[140.33047969900008,35.15778336400007],[140.3489351930001,35.18051589700008],[140.377629651,35.17635690900005],[140.39419527500002,35.19659166200006],[140.4130965500001,35.301214911000045],[140.38591856900007,35.380998573000056],[140.42321951300005,35.49064438000005],[140.4834674970001,35.56599105500004],[140.5961624470001,35.65207529800006],[140.6610564560001,35.68863497800004],[140.716260551,35.69879072800006],[140.72078890300008,35.68251721100006],[140.80314419800004,35.70937139000006],[140.82830145700007,35.71396943700006],[140.8606646720001,35.68779101800004],[140.8794051440001,35.72052643400008],[140.87427819100003,35.72923411700009],[140.84164472700002,35.74241771000004],[140.83082116000003,35.761053778000075],[140.78931725400003,35.808661200000074],[140.646034105,36.013320444000044],[140.57837975400003,36.16095612200007],[140.56643664500007,36.24583748900005],[140.5641073710001,36.28309580200005],[140.57938199800003,36.30475890300005],[140.61687259200005,36.33779531500005],[140.6235457690001,36.37962474200009],[140.61011803500003,36.43390534100007],[140.6235457690001,36.50608958500004],[140.63892662900003,36.53974030200004],[140.6938582690001,36.61798737200007],[140.74187259200005,36.768459377000056],[140.79314212300005,36.837551174000055],[140.80225670700008,36.85700104400007],[140.80437259200005,36.885972398000035],[140.83521569100003,36.90843333500004],[140.85906009200005,36.91400788000004],[140.89177493600005,36.93211497600004],[140.9257918630001,36.93891022300005],[140.960297071,36.96621328300006],[140.97437584700003,37.00177643400008],[140.98796634200005,37.08978913000004],[141.0078231130001,37.13385651200008],[141.0078231130001,37.22675202000005],[141.01449629,37.23997630400004],[141.01441491000003,37.271470445000034],[141.0361434250001,37.34471263200004],[141.04184004,37.37714264500005],[141.028168165,37.648098049000055],[141.00928795700008,37.685248114000046],[141.0219832690001,37.706732489000046],[141.02222741000003,37.72695547100005],[141.008148634,37.75018952000005],[140.99325908700007,37.79983832100004],[140.93336022200003,37.88922760600008],[140.91976972700002,37.96515534100007],[140.91863040500004,38.00722890800006],[140.9257918630001,38.04938385600008],[140.9668074880001,38.17047760600008],[140.98796634200005,38.207017320000034],[141.01734459700003,38.24485911700009],[141.0527449880001,38.27871328300006],[141.0805770190001,38.294907945000034],[141.0791121750001,38.31159088700008],[141.04867597700002,38.309475002000056],[141.06470787900003,38.34662506700005],[141.09782962300005,38.371527411000045],[141.1318465500001,38.371527411000045],[141.15211022200003,38.32306549700007],[141.17497806100005,38.32436758000006],[141.17212975400003,38.347316799000055],[141.18091881600003,38.371161200000074],[141.20240319100003,38.38580963700008],[141.2295028000001,38.39411041900007],[141.30445397200003,38.407904364000046],[141.33318118600005,38.40900299700007],[141.39812259200005,38.37767161700009],[141.428070509,38.38043854400007],[141.43970787900003,38.371527411000045],[141.42554772200003,38.34357330900008],[141.45777428500003,38.33087799700007],[141.46998131600003,38.296087958000044],[141.4912215500001,38.29238515800006],[141.5254012380001,38.26780833500004],[141.54281660200002,38.30263906500005],[141.5297957690001,38.31793854400007],[141.52523847700002,38.35993073100008],[141.50123131600003,38.371527411000045],[141.51840254,38.402329820000034],[141.4775496750001,38.401556708000044],[141.47144616000003,38.43573639500005],[141.50798587300005,38.446600653000075],[141.49496504,38.46088288000004],[141.51482181100005,38.50804271000004],[141.52418053500003,38.48969147300005],[141.54281660200002,38.48139069200005],[141.54761803500003,38.51862213700008],[141.52914472700002,38.54901764500005],[141.5146590500001,38.534816799000055],[141.4990340500001,38.54165273600006],[141.46705162900003,38.569525458000044],[141.51140384200005,38.59739817900004],[141.52808678500003,38.61790599200009],[141.5322371750001,38.63841380400004],[141.485199415,38.64085521000004],[141.46306399800005,38.661363023000035],[141.49105879,38.67755768400008],[141.51587975400003,38.68427155200004],[141.53785241000003,38.70408763200004],[141.5664168630001,38.703558661000045],[141.572601759,38.71865469000005],[141.53199303500003,38.763332424000055],[141.53052819100003,38.78082916900007],[141.5732528000001,38.80658600500004],[141.58936608200008,38.82607656500005],[141.58375084700003,38.85443756700005],[141.58855228000004,38.88222890800006],[141.6367293630001,38.89109935100004],[141.65211022200003,38.86432526200008],[141.668793165,38.87262604400007],[141.64527428500003,38.92300039300005],[141.63835696700005,38.98541901200008],[141.65105228000004,38.99681224200009],[141.69092858200008,38.95962148600006],[141.717133009,38.94684479400007],[141.73023522200003,38.951157945000034],[141.7270613940001,38.987616278000075],[141.74822024800005,38.98786041900007],[141.72820071700005,39.02171458500004],[141.80648847700002,39.02948639500005],[141.84742272200003,39.02195872600004],[141.8481551440001,39.03510163000004],[141.8232528000001,39.049261786000045],[141.87183678500003,39.062933661000045],[141.859711134,39.07330963700008],[141.81714928500003,39.09088776200008],[141.83545983200008,39.10431549700007],[141.89047285200002,39.089544989000046],[141.90886478000004,39.10976797100005],[141.8437606130001,39.152329820000034],[141.89519290500004,39.159369208000044],[141.91578209700003,39.169989325000074],[141.9262801440001,39.18707916900007],[141.89372806100005,39.186753648000035],[141.90211022200003,39.204331773000035],[141.930023634,39.212225653000075],[141.9492293630001,39.22801341400009],[141.94703209700003,39.249701239000046],[141.89909915500004,39.24852122600004],[141.89909915500004,39.27521393400008],[141.9326278000001,39.27521393400008],[141.914886915,39.29486725500004],[141.96371504,39.312567450000074],[141.98365319100003,39.32713450700004],[141.9809676440001,39.35097890800006],[141.94678795700008,39.33038971600007],[141.92465254,39.32489655200004],[141.9052840500001,39.33047109600005],[141.95337975400003,39.36200592700004],[141.96412194100003,39.37555573100008],[141.93873131600003,39.38507721600007],[141.9809676440001,39.41242096600007],[142.031586134,39.417303778000075],[142.039886915,39.45457591400009],[142.05844160200002,39.46629466400009],[142.03638756600003,39.48379140800006],[142.018890821,39.45937734600005],[141.99626712300005,39.44525788000004],[141.97087649800005,39.44326406500005],[141.95297285200002,39.46076080900008],[141.96631920700008,39.47134023600006],[142.01514733200008,39.488714911000045],[142.0693465500001,39.54047272300005],[142.06812584700003,39.55638255400004],[142.03077233200008,39.58417389500005],[142.0361434250001,39.61163971600007],[142.01246178500003,39.63580963700008],[141.96631920700008,39.59845612200007],[141.96045983200008,39.61163971600007],[141.97828209700003,39.64785390800006],[141.98389733200008,39.70697663000004],[141.9912215500001,39.730454820000034],[142.0087996750001,39.75275299700007],[141.97868899800005,39.83930084800005],[141.97185306100005,39.88300202000005],[141.942230665,39.92450592700004],[141.9614363940001,39.95368073100008],[141.949554884,39.99656810100004],[141.91960696700005,40.00885651200008],[141.85499108200008,40.05499909100007],[141.84245853000004,40.070379950000074],[141.83757571700005,40.09454987200007],[141.84742272200003,40.11513906500005],[141.87720787900003,40.13930898600006],[141.86500084700003,40.15973541900007],[141.813324415,40.183579820000034],[141.81519616000003,40.20286692900004],[141.84197024800005,40.225775458000044],[141.830414259,40.251166083000044],[141.80347741000003,40.29002513200004],[141.77849368600005,40.31549713700008],[141.772797071,40.33392975500004],[141.74377693500003,40.38392223200009],[141.72608451300005,40.393792775000065],[141.719140981,40.41283517800008],[141.69092858200008,40.442287502000056],[141.62370853000004,40.50136953300006],[141.58926137100002,40.52210463500006],[141.57554958600008,40.542279059000066],[141.54821362400003,40.531595226000036],[141.5196960080001,40.537986372000034],[141.491639652,40.562326700000085],[141.470398091,40.59341807100009],[141.43738666000002,40.67014093100005],[141.4231560400001,40.72577228600005],[141.4055534050001,40.816822863000084],[141.39026167600002,40.93393259100009],[141.38630694100004,41.03036180100008],[141.396359255,41.09173074200004],[141.39828535200002,41.128119208000044],[141.39081676700005,41.16101119700005],[141.40891673300007,41.256677393000075],[141.42379443000004,41.312706921000085],[141.44528424100008,41.36618409700009],[141.4574654930001,41.37747296200007],[141.44819724900003,41.41840423500008],[141.38216087800004,41.37259958300007],[141.31791884200004,41.35304670600004],[141.276095682,41.35319552800007],[141.19499759200005,41.387152411000045],[141.10775040700003,41.46386893600004],[141.06530723900005,41.48111619600007],[141.00333950800007,41.48708488900007],[140.97805707600003,41.49765904100008],[140.9508002450001,41.521911388000035],[140.913572359,41.54685719300005],[140.89946179000003,41.52417520800009],[140.90710228800003,41.50079851500004],[140.89795983200008,41.47614166900007],[140.88382800500005,41.46740993900005],[140.85333808400003,41.430339450000076],[140.83695137100005,41.41822188600008],[140.815172877,41.33756239600007],[140.80321422500003,41.32118674800006],[140.79309271300008,41.27842175200004],[140.780395753,41.25193437900003],[140.78469859000006,41.23514784300005],[140.778168165,41.20685455900008],[140.76555366700006,41.19595586000008],[140.76579604300002,41.14501873900008],[140.79815518300006,41.128577901000085],[140.81452501500007,41.128461806000075],[140.82850222700006,41.14610662600006],[140.86273009800004,41.16412311100004],[140.89078478800002,41.16377995100004],[140.907788697,41.17364541400008],[140.946265175,41.17328947700008],[140.977561863,41.19647781500004],[141.00563260700005,41.19576286500006],[141.05807571800005,41.183068125000034],[141.09473672700005,41.210264513000084],[141.11337199500008,41.21436328300007],[141.14683011300008,41.236288570000056],[141.1369634360001,41.246697177000044],[141.1612324580001,41.27846726200005],[141.18337455300002,41.27706277200008],[141.20591119200003,41.26471970400007],[141.235466901,41.23797276500005],[141.2704802500001,41.18520388400009],[141.277882083,41.155329909000045],[141.27223835200004,41.13657760500007],[141.25234706100002,41.11875852600008],[141.24190264900005,41.082742],[141.23640716500006,41.01572156100008],[141.22640007400003,40.987442920000035],[141.18524863300001,40.91121452900006],[141.1471450140001,40.87883994300006],[141.11134557100002,40.87263328600005],[141.083704774,40.886413220000065],[141.06807564200005,40.91084813100008],[140.97506573300006,40.937755320000065],[140.98140227400006,40.959768599000085],[140.94870251300006,40.99074773500007],[140.914405859,40.99609776500006],[140.902885406,41.008995810000044],[140.88233210700002,41.007822022000084],[140.8610516550001,40.97204276600007],[140.85360123300006,40.94491889100004],[140.8707263990001,40.915135476000046],[140.85206829100002,40.883848724000075],[140.79922346800004,40.83908125100004],[140.752402511,40.835186570000076],[140.7031216700001,40.85329202300005],[140.67302493600005,40.89557526200008],[140.65455162900003,41.00849030200004],[140.64128665500004,41.04148997600004],[140.63152103000004,41.093085028000075],[140.64266573200007,41.17137914700004],[140.63270238300004,41.194257795000055],[140.59390699100004,41.22017794000004],[140.54859759400006,41.226242993000085],[140.52475613000001,41.21997640700005],[140.51106755500007,41.19771902400004],[140.48367398100004,41.18346812600004],[140.46224804000008,41.18388740700004],[140.43580162900003,41.198146877000056],[140.391481214,41.24044348400008],[140.35914755300007,41.25394959800008],[140.33277902700002,41.237153409000086],[140.33277428500003,41.19131094000005],[140.32043302,41.14977976200004],[140.303971091,41.13668050100006],[140.25618438200001,41.13889667800004],[140.24809544900006,41.12336100200008],[140.3001619810001,41.11127729200007],[140.32185208300007,41.08292085900007],[140.32395648300007,41.04528458400006],[140.35058592000007,41.03884449200007],[140.39898677500003,41.03937203000004],[140.36962853800003,41.021661632000075],[140.38233483200008,40.988267320000034],[140.36959444000001,40.98605469100005],[140.34099368600005,41.005072333000044],[140.324833894,41.027461359000085],[140.29664147200003,40.87376536700003],[140.27076256600003,40.81240469000005],[140.240570509,40.78351471600007],[140.21908790400005,40.77833468000006],[140.1980380870001,40.78645254300005],[140.14013941000007,40.74957629000005],[140.1076180130001,40.74407301800005],[140.06931012100006,40.76558251200004],[140.04597727600003,40.76777008700009],[139.99815616500007,40.742104090000055],[139.9868773930001,40.71789968300004],[139.95954043000006,40.67769251500005],[139.938624076,40.66984990800006],[139.925841999,40.646821001000035],[139.90029743700006,40.64247505800006],[139.8603621750001,40.60879140800006],[139.856781446,40.59015534100007],[139.87639612100008,40.58343784600004],[139.91515451300006,40.58617340700005],[139.94278633300007,40.54458555800005],[139.94605553500003,40.418850002000056],[140.0242629610001,40.355016067000065],[140.01989872700005,40.33321038300005],[140.01115209900001,40.23545842000004],[139.98861094900008,40.191113804000054],[139.993500196,40.169623114000046],[139.97803795700008,40.12547435100004],[139.90419316100008,40.01769512100009],[139.86692463200006,39.98429728600007],[139.82211347700002,39.96108633000006],[139.80046634200005,39.962591864000046],[139.7434519960001,39.982463933000076],[139.700639313,40.00563611200005],[139.69773066900007,39.98856648200007],[139.70866946700005,39.924261786000045],[139.73015384200005,39.90273672100005],[139.760101759,39.85805898600006],[139.825531446,39.85805898600006],[139.8437606130001,39.861761786000045],[139.8774520190001,39.89280833500004],[139.92139733200008,39.898504950000074],[139.96648196700005,39.880072333000044],[140.00456790500004,39.850897528000075],[140.02759850400003,39.82453034100007],[140.03908976800005,39.79328263100007],[140.03911708700002,39.76928182100005],[140.05929588100003,39.72749224900008],[140.05933387900006,39.69806625600006],[140.04857715000003,39.50466964600008],[139.99531019000005,39.32732856000007],[139.9635796240001,39.29882013200006],[139.93181044000005,39.287111834000086],[139.91684255200005,39.26249903000007],[139.90358143900005,39.22493941100004],[139.891937696,39.13320547100005],[139.8740340500001,39.09845612200007],[139.8575639610001,39.03228377100004],[139.81527369900004,38.94106263500004],[139.77384433300006,38.817116774000056],[139.747406446,38.778957424000055],[139.716156446,38.74567291900007],[139.62705899900004,38.68121429300004],[139.5992151490001,38.648254087000055],[139.52995853000004,38.50800202000005],[139.4926863940001,38.45416901200008],[139.47282962300005,38.41250234600005],[139.46216881600003,38.374986070000034],[139.43238366000003,38.169134833000044],[139.40805097700002,38.13190338700008],[139.30453535200002,38.04193756700005],[139.21594314100003,37.98993395400004],[139.13246598800004,37.958828931000085],[139.068193436,37.95509385200006],[139.05988081300006,37.94594167000008],[138.93039446900002,37.874923177000085],[138.85767662900003,37.827826239000046],[138.82702387000006,37.79728696600006],[138.8094257350001,37.76077187900006],[138.7656356130001,37.63467031500005],[138.7465926440001,37.596869208000044],[138.71827233200008,37.56484609600005],[138.6597345780001,37.52443260400008],[138.62253960700002,37.47754346900007],[138.57797285200002,37.40289948100008],[138.55062910200002,37.37791575700004],[138.437515909,37.32203684800004],[138.32510269600004,37.23104347200007],[138.24489497900004,37.18378944500006],[138.2069532590001,37.16920530200008],[138.16443255600007,37.16169930800004],[138.0975018040001,37.17105935200004],[138.05339603000004,37.130113023000035],[137.99937413200007,37.11105409800007],[137.89673912900003,37.057521877000056],[137.8075446790001,37.030805238000085],[137.68327884200005,36.987982489000046],[137.5867332050001,36.969893864000085],[137.4993469650001,36.95574161700006],[137.42710766900007,36.92201115800009],[137.413764539,36.89689210800009],[137.41953143800004,36.87733545700007],[137.40835627700005,36.845203748000074],[137.3836057960001,36.80014545500006],[137.35053007200008,36.78230909800004],[137.33334435900008,36.762726067000074],[137.29867296700002,36.75700875800004],[137.2414419590001,36.76506775100006],[137.1972877510001,36.758124588000044],[137.15699303500003,36.76080963700008],[137.12802035700008,36.77683954300005],[137.0806963020001,36.79008655300004],[137.05291460800004,36.81104398500008],[137.00554446700005,36.835923570000034],[136.987741561,36.87102163700007],[137.025040116,36.91394722700005],[137.02741452300006,36.93884298800009],[137.05444619900004,36.966749864000064],[137.04884394200008,37.02292600100009],[137.05396569100003,37.07982005400004],[137.04712975400003,37.098822333000044],[137.02429826600007,37.09540888600009],[137.003991628,37.055457543000045],[136.9801259620001,37.04685964000004],[136.963226759,37.05174388200004],[136.9336043630001,37.07982005400004],[136.91423587300005,37.08242422100005],[136.87468509200005,37.069647528000075],[136.868670501,37.11091236500005],[136.88775294000004,37.110318420000056],[136.8956676370001,37.13890407800005],[136.880215453,37.144152871000074],[136.89259776400002,37.17332110000007],[136.916567793,37.198207266000054],[136.93043053500003,37.22162506700005],[136.98023522200003,37.196478583000044],[137.0141707690001,37.184881903000075],[137.0328921260001,37.20466420300005],[137.070666716,37.218643978000046],[137.07130008000001,37.23811173100006],[137.10872138900004,37.28344965100007],[137.16560551600003,37.30083373900004],[137.23023522200003,37.29214101800005],[137.26254497500008,37.326049487000034],[137.267485534,37.35508577600007],[137.24887841500004,37.35726027800007],[137.237363609,37.37988162000005],[137.2536012390001,37.42910590200006],[137.28725280900005,37.44297530500006],[137.3251360810001,37.43911155300009],[137.35120513000004,37.44567560400009],[137.35897927200006,37.46663506600004],[137.3452992980001,37.475020106000045],[137.350873771,37.50610344200004],[137.3409655590001,37.51709413800006],[137.29570988800003,37.53149016500004],[137.25549087500008,37.52459398900004],[137.11384056300005,37.487746918000084],[137.0715345210001,37.45300312900008],[137.04487433600002,37.447916391000035],[136.934766136,37.39945701800008],[136.90348352500007,37.398323368000035],[136.880798507,37.407986994000055],[136.842170667,37.39953277200004],[136.749094194,37.357917400000076],[136.72153094200007,37.32532953900005],[136.72443485100007,37.297491111000056],[136.73321719800003,37.28501484600008],[136.7127711610001,37.25106551400006],[136.7055770190001,37.22825755400004],[136.690423893,37.21500773200006],[136.67447257100002,37.17547192100005],[136.6702589030001,37.149957088000065],[136.69626613100002,37.13636152400005],[136.70856414900004,37.14987732700007],[136.72673587300005,37.126613674000055],[136.71841293800003,37.076261489000046],[136.7422883710001,37.01338806100006],[136.759909676,37.00008710100008],[136.76761331400007,36.98099874800005],[136.75038571000005,36.92572910200005],[136.76544802900003,36.91339711100005],[136.76055452600008,36.870599361000075],[136.68968479300008,36.73082693100008],[136.63996143100007,36.662600574000066],[136.4301863940001,36.432521877000056],[136.38229344500007,36.39107136800004],[136.3467788480001,36.365800910000075],[136.29841744100008,36.34727817000004],[136.27866738800003,36.32240577500005],[136.1967378060001,36.26436216400003],[136.16262712500009,36.252242211000066],[136.12417327600008,36.25317933200006],[136.13484480500006,36.22455239400006],[136.09282041200004,36.161363244000086],[136.04361081500008,36.11083253800007],[136.03109785200002,36.08071523600006],[136.0001323890001,36.02418682700005],[135.96321741100007,35.999923222000064],[135.95728600400003,35.97313060100004],[135.99438018900003,35.93591278200006],[135.99345406400005,35.88514211700004],[136.04880787900004,35.83866838800009],[136.07049761300004,35.82474933100008],[136.09945722700002,35.77570221600007],[136.09239818700007,35.71516472700006],[136.06911435400002,35.66187974300004],[136.04745939000009,35.65881225800007],[136.027006444,35.68094741500005],[136.04684700500002,35.70372189800008],[136.01970526600007,35.765347201000054],[135.98154353000007,35.739854378000075],[135.95600508100006,35.728324936000035],[135.97008375400003,35.70299009100006],[135.97315915800004,35.67621119000006],[135.96354036800005,35.65540930800006],[135.98166761900006,35.64363788800006],[135.9710528820001,35.625981151000076],[135.95093848200008,35.62888168500007],[135.91944111600003,35.60816038200005],[135.9018474180001,35.62436015600008],[135.86784078100004,35.61775318300005],[135.82322255600002,35.644620961000044],[135.81601831600005,35.63085602400008],[135.834320509,35.61318594000005],[135.85474694100003,35.57904694200005],[135.80150220300004,35.568003462000036],[135.81617272200003,35.54596588700008],[135.7940502560001,35.53138182200007],[135.77725798100005,35.549856049000084],[135.75613707800005,35.54907241500007],[135.73844643100006,35.57053137300005],[135.70609333200002,35.56209461800006],[135.70860440900003,35.54055094000006],[135.72080139600007,35.52230481500004],[135.74819794300004,35.533525420000046],[135.74835148300008,35.51371915000004],[135.72443120100002,35.49073208600004],[135.69668606000005,35.491842543000075],[135.645065724,35.48020001400005],[135.63461347700002,35.49355703300006],[135.64894517800008,35.54283352900006],[135.62479780900003,35.53234396900007],[135.59644616000003,35.505113023000035],[135.55827884200005,35.49176666900007],[135.51099694100003,35.49819570500006],[135.51246178500003,35.52985260600008],[135.4790145190001,35.52252838700008],[135.482937686,35.559199188000036],[135.452389789,35.571781119000036],[135.43948099800002,35.59056974300006],[135.4160262380001,35.56167226800005],[135.3613387380001,35.55418528900009],[135.3377962190001,35.537477564000085],[135.3502240250001,35.51031344800003],[135.37378991000003,35.50169505400004],[135.4023543630001,35.507025458000044],[135.394867384,35.47646719000005],[135.35251785900005,35.486984186000086],[135.33025149800005,35.45453522300005],[135.31527754,35.466782945000034],[135.33548989200006,35.50821008500009],[135.32443612400004,35.524446383000054],[135.301521966,35.51324263100008],[135.23873893400003,35.54202559100008],[135.2398152190001,35.55796303400007],[135.26204205600004,35.557546596000066],[135.24822550200008,35.59169447900007],[135.188324232,35.54525757600004],[135.18817537200005,35.56504457600005],[135.2049023830001,35.58735277400007],[135.242393935,35.62264035600003],[135.25882700300008,35.65682881300006],[135.27921371900004,35.66853270000007],[135.2993270190001,35.657904364000046],[135.304047071,35.68891022300005],[135.276703321,35.73139069200005],[135.25066024,35.75781549700008],[135.22684561200003,35.772320652000076],[135.21013431100005,35.763373114000046],[135.13430528700007,35.751901306000036],[135.084119836,35.73902244700008],[135.08578535200002,35.72597890800006],[135.0290315420001,35.69286863100007],[134.98794378000002,35.689777236000054],[134.95962217700003,35.67375324500006],[134.96371504,35.661037502000056],[134.92701256600003,35.64118073100008],[134.88096824600007,35.657322495000074],[134.834137144,35.65714558500008],[134.79829520400006,35.665642253000044],[134.64744713200002,35.664265416000035],[134.59020875200008,35.65942839100006],[134.5369873090001,35.67220309900006],[134.46621486200002,35.63755465300005],[134.37649117900003,35.61118425500007],[134.36150149800005,35.595770575000074],[134.29387121000002,35.585893956000064],[134.281586134,35.564886786000045],[134.23776516100008,35.546651206000035],[134.17366172700008,35.53627136000006],[134.03478971200002,35.519355662000066],[134.00838333700005,35.53704892300004],[133.9115467370001,35.51459457200008],[133.731144642,35.50290661400004],[133.6325682480001,35.52132835600008],[133.58018171100002,35.533602742000085],[133.52559462700003,35.52242699200008],[133.44124508900006,35.49103766500008],[133.41741190900007,35.45654296500004],[133.40015709700003,35.454291083000044],[133.3296256210001,35.46550438700007],[133.2646590500001,35.496161200000074],[133.254730665,35.53384023600006],[133.27013504800004,35.54961013700006],[133.312904215,35.56197612100004],[133.308772373,35.57718857800006],[133.266376798,35.574236505000044],[133.23933637000005,35.563614802000075],[133.20706772900007,35.57282813000006],[133.15646656900003,35.56262168500007],[133.10535285100002,35.597516589000065],[133.08144222400006,35.57532590200009],[133.05177218200004,35.57373032500004],[133.033489379,35.545992142000046],[133.0143994870001,35.540780117000054],[132.97320551100006,35.54188385000003],[132.9697371750001,35.51699453300006],[132.86490913700004,35.51098268100009],[132.80482662200006,35.49518180100006],[132.77542651400006,35.481007496000075],[132.72549563500002,35.47077964400006],[132.73443814200004,35.44863223200008],[132.68209873600006,35.44901641600006],[132.63535916900003,35.44095469500007],[132.63114205500005,35.41981396300008],[132.6707911430001,35.39947458800009],[132.67144646200006,35.355128136000076],[132.663346756,35.32953554900007],[132.63867918900007,35.29464467300005],[132.62114153700009,35.28444409600007],[132.5756172450001,35.27327009700008],[132.53193742200006,35.25767159600008],[132.52971849900007,35.24491322200004],[132.49582670100006,35.23245521000007],[132.40808491500002,35.17505067800005],[132.386574258,35.14553759200004],[132.37994884300008,35.124980778000065],[132.326594268,35.09206984400004],[132.31684998700007,35.062346339000044],[132.27084188700007,35.040329058000054],[132.23649497000008,35.034818088000065],[132.21631920700008,35.01357656500005],[132.1318465500001,34.958644924000055],[132.06330411800002,34.907489343000066],[132.05388431100005,34.872259833000044],[132.0345512,34.87657900500005],[131.99732506600003,34.837225653000075],[131.95700657400005,34.81303024300007],[131.90132190300005,34.77045129700008],[131.87375767000003,34.75922287700007],[131.8584090500001,34.714504299000055],[131.790375196,34.68610260600008],[131.746081807,34.675353457000085],[131.71159915500004,34.671576239000046],[131.680332196,34.674649043000045],[131.65641471600009,34.65358629000008],[131.6152418900001,34.66488063600008],[131.59687157300004,34.659589372000084],[131.59587649800005,34.616848049000055],[131.55729919400005,34.61485699900004],[131.55103600400003,34.58152903900009],[131.52076256600003,34.55190664300005],[131.48267662900003,34.52757396000004],[131.45476321700005,34.51911041900007],[131.45622806100005,34.49038320500006],[131.41331462800008,34.45387373300008],[131.41431725400003,34.42397695500006],[131.3650500120001,34.41565087500004],[131.33473090300004,34.41611429100004],[131.32309029500004,34.393011809000086],[131.28448215100002,34.384211288000074],[131.23617597700002,34.38231028900009],[131.2089949880001,34.36933014500005],[131.21607506600003,34.396918036000045],[131.2295028000001,34.41771067900004],[131.25102696400006,34.41913055400005],[131.2526317610001,34.437386992000086],[131.192718946,34.42475006700005],[131.17359459700003,34.410223700000074],[131.19170165700007,34.39364779300007],[131.172699415,34.36774323100008],[131.14079837300005,34.37848541900007],[131.133922988,34.41156180400009],[131.09937584700003,34.413275458000044],[131.0747176440001,34.42475006700005],[131.03728274800005,34.41111888200004],[131.0097762380001,34.41030508000006],[131.0028453540001,34.42594628900008],[130.97639607600001,34.438967292000086],[130.9477645190001,34.41893138200004],[130.9550887380001,34.40346914300005],[131.00359134200005,34.39288971600007],[131.03360681000004,34.381789240000046],[130.9961043630001,34.36933014500005],[130.95476321700005,34.36172109600005],[130.92741946700005,34.348944403000075],[130.89991295700008,34.35504791900007],[130.8896590500001,34.33120351800005],[130.88624108200008,34.300441799000055],[130.8715926440001,34.27924225500004],[130.8916121750001,34.259833075000074],[130.91041100400003,34.22492096600007],[130.92335045700008,34.18378327000005],[130.9111434250001,34.15448639500005],[130.87322024800005,34.12909577000005],[130.86158287900003,34.11273834800005],[130.87842858200008,34.06785716400009],[130.9039136780001,34.06582577800009],[130.91257739700006,34.05334156200007],[130.9052840500001,34.02960846600007],[130.91713452200008,34.01091306300009],[130.90965033600003,33.973651604000054],[130.8994246750001,33.94985586100006],[130.8794051440001,33.937933661000045],[130.91575345500007,33.913989822000076],[130.93208630100003,33.94536528400005],[130.96859785200002,33.97142161700009],[131.01396225400003,34.01476201500009],[131.03559480800004,34.04797509500008],[131.10316468400003,34.03021689000008],[131.12399879100008,33.99993139600008],[131.1660262380001,33.97711823100008],[131.16026595300002,33.95279605600007],[131.1782332690001,33.93048737200007],[131.2207137380001,33.950384833000044],[131.22584069100003,33.93500397300005],[131.24984785200002,33.937933661000045],[131.27222741000003,33.92682526200008],[131.32195071700005,33.95307038000004],[131.35572350400003,33.989447333000044],[131.383148634,34.03302643400008],[131.40064537900003,34.03294505400004],[131.40219160200002,33.999335028000075],[131.39332116000003,33.98574453300006],[131.449554884,33.98224518400008],[131.45671634200005,33.999335028000075],[131.4829207690001,34.03204987200007],[131.51050866000003,34.01239655200004],[131.55103600400003,33.99941640800006],[131.56763756600003,33.98627350500004],[131.59302819100003,33.99168528900009],[131.59742272200003,34.00263092700004],[131.58513431100005,34.026678778000075],[131.644867384,34.02887604400007],[131.68873131600003,34.04718659100007],[131.70337975400003,34.044175523000035],[131.74146569100003,34.05621979400007],[131.79957116000003,34.03774648600006],[131.82390384200005,34.02016836100006],[131.77751712300005,33.98574453300006],[131.791351759,33.964056708000044],[131.81226647200003,33.97142161700009],[131.83366946700005,33.99469635600008],[131.8520613940001,33.99835846600007],[131.8935653000001,33.97142161700009],[131.9419051440001,33.95091380400004],[131.97234134200005,33.91681549700007],[131.99480228000004,33.91347890800006],[132.05779056100005,33.89524974200009],[132.09945722700002,33.872259833000044],[132.1132918630001,33.84487539300005],[132.09888756600003,33.82363515800006],[132.07634524800005,33.82672760600008],[132.0654403000001,33.80695221600007],[132.04444420700008,33.79328034100007],[132.041351759,33.776271877000056],[132.07593834700003,33.78705475500004],[132.105804884,33.809027411000045],[132.15007571700005,33.830145575000074],[132.15992272200003,33.85732656500005],[132.13892662900003,33.894964911000045],[132.1337996750001,33.913397528000075],[132.1425887380001,33.94643789300005],[132.18148847700002,33.95783112200007],[132.213226759,33.98965078300006],[132.2109481130001,34.02521393400008],[132.19906660200002,34.06476471600007],[132.20191491000003,34.108587958000044],[132.23894290500004,34.142645575000074],[132.24366295700008,34.16693756700005],[132.2382918630001,34.20425039300005],[132.22250410200002,34.23895905200004],[132.31234785200002,34.32697174700007],[132.346039259,34.348822333000044],[132.38347415500004,34.365668036000045],[132.40552819100003,34.368068752000056],[132.44157962300005,34.35000234600005],[132.46168053500003,34.34955475500004],[132.49683678500003,34.362494208000044],[132.5175887380001,34.35301341400009],[132.53101647200003,34.335150458000044],[132.50367272200003,34.32831452000005],[132.50904381600003,34.31622955900008],[132.50367272200003,34.28733958500004],[132.52100670700008,34.26976146000004],[132.53345787900003,34.24477773600006],[132.55982506600003,34.218410549000055],[132.54468834700003,34.20425039300005],[132.56617272200003,34.19196198100008],[132.61353600400003,34.218451239000046],[132.62720787900003,34.19798411700009],[132.6562606130001,34.199286200000074],[132.70297285200002,34.225490627000056],[132.75660241000003,34.23232656500005],[132.77808678500003,34.24274323100008],[132.7622176440001,34.27033112200007],[132.79908287900003,34.28400299700007],[132.81820722700002,34.30337148600006],[132.83619225400003,34.31110260600008],[132.85303795700008,34.28733958500004],[132.91187584700003,34.31476471600007],[133.01832116000003,34.32330963700008],[133.0590926440001,34.33234284100007],[133.08578535200002,34.35101959800005],[133.0791121750001,34.38231028900009],[133.13428795700008,34.38231028900009],[133.1684676440001,34.362494208000044],[133.1474715500001,34.35138580900008],[133.133148634,34.31818268400008],[133.1132918630001,34.314113674000055],[133.11793053500003,34.34227122600004],[133.10328209700003,34.34511953300006],[133.07691491000003,34.31671784100007],[133.070648634,34.27724844000005],[133.07642662900003,34.25958893400008],[133.09327233200008,34.25263092700004],[133.12517337300005,34.276271877000056],[133.16578209700003,34.292303778000075],[133.1855574880001,34.280503648000035],[133.20289147200003,34.28888580900008],[133.20866946700005,34.307806708000044],[133.20191491000003,34.32831452000005],[133.1821395190001,34.34137604400007],[133.22966556100005,34.376166083000044],[133.24732506600003,34.42649974200009],[133.2666121750001,34.42059967700004],[133.27662194100003,34.40176015800006],[133.25847415500004,34.38979726800005],[133.3046981130001,34.37759023600006],[133.3149520190001,34.36603424700007],[133.305511915,34.348822333000044],[133.269867384,34.339178778000075],[133.26978600400003,34.32241445500006],[133.2954207690001,34.32489655200004],[133.34107506600003,34.34804922100005],[133.412445509,34.42446523600006],[133.422211134,34.445013739000046]]],[[[143.8964949880001,44.15814850500004],[143.85954837300005,44.172756252000056],[143.81373131600003,44.168402411000045],[143.8964949880001,44.14590078300006],[143.8964949880001,44.15814850500004]]],[[[145.76742597700002,43.38727448100008],[145.74439537900003,43.391791083000044],[145.7231551440001,43.37982819200005],[145.67595462300005,43.38198476800005],[145.64918053500003,43.37376536700003],[145.6337996750001,43.353176174000055],[145.59489993600005,43.34284088700008],[145.4965926440001,43.27057526200008],[145.5219832690001,43.24119700700004],[145.51710045700008,43.22898997600004],[145.46843509200005,43.25299713700008],[145.383636915,43.259019273000035],[145.321625196,43.276271877000056],[145.30420983200008,43.29169342700004],[145.31511478000004,43.30292389500005],[145.26449629,43.31671784100007],[145.2700301440001,43.34638092700004],[145.299164259,43.34125397300005],[145.361664259,43.30145905200004],[145.3935653000001,43.30536530200004],[145.3457137380001,43.33270905200004],[145.31169681100005,43.36676666900007],[145.29078209700003,43.40534088700008],[145.26441491000003,43.48969147300005],[145.24683678500003,43.530462958000044],[145.23226972700002,43.539943752000056],[145.21851647200003,43.58966705900008],[145.19483483200008,43.62067291900007],[145.13013756600003,43.671576239000046],[145.11597741000003,43.686835028000075],[145.0747176440001,43.75372955900008],[145.0747176440001,43.786037502000056],[145.089203321,43.80072663000004],[145.11597741000003,43.86245351800005],[145.10613040500004,43.88076406500005],[145.12232506600003,43.91705963700008],[145.16960696700005,43.98529694200005],[145.26042728000004,44.06940338700008],[145.26921634200005,44.085150458000044],[145.27914472700002,44.13393789300005],[145.29053795700008,44.15448639500005],[145.34986412900003,44.21161530200004],[145.37183678500003,44.24876536700003],[145.3730574880001,44.292222398000035],[145.34148196700005,44.34552643400008],[145.3183699880001,44.33633047100005],[145.30974368600005,44.31635163000004],[145.25025475400003,44.27179596600007],[145.2016707690001,44.20579661700003],[145.16667728000004,44.19049713700008],[145.11931399800005,44.15448639500005],[145.04851321700005,44.12213776200008],[144.947601759,44.034084377000056],[144.83122806100005,43.94220612200007],[144.7880965500001,43.92177969000005],[144.74048912900003,43.91461823100008],[144.5703231130001,43.92479075700004],[144.43043053500003,43.942572333000044],[144.3794051440001,43.95648834800005],[144.31861412900003,43.99119700700004],[144.30225670700008,44.02411530200004],[144.2783309250001,44.036769924000055],[144.2558699880001,44.079413153000075],[144.26343834700003,44.10781484600005],[144.24293053500003,44.119208075000074],[144.21509850400003,44.11147695500006],[144.18873131600003,44.05027903900009],[144.1670028000001,44.025376695000034],[144.13835696700005,44.020697333000044],[144.1054793630001,44.03156159100007],[144.12680097700002,44.080145575000074],[144.17172285200002,44.10602448100008],[144.11931399800005,44.12905508000006],[143.99919681100005,44.133978583000044],[143.9296981130001,44.098944403000075],[143.7832137380001,44.10065338700008],[143.74244225400003,44.12091705900008],[143.72828209700003,44.11347077000005],[143.71461022200003,44.15135325700004],[143.68043053500003,44.18235911700003],[143.6953231130001,44.18984609600005],[143.77662194100003,44.18235911700003],[143.77662194100003,44.18927643400008],[143.55241946700005,44.25250885600008],[143.4052840500001,44.31745026200008],[143.3691512380001,44.34625885600008],[143.35141035200002,44.38104889500005],[143.2968856130001,44.39468008000006],[143.2407332690001,44.43634674700007],[143.204437696,44.447251695000034],[143.11841881600003,44.49689362200007],[143.04273522200003,44.55491771000004],[142.9814559250001,44.58649323100008],[142.939789259,44.63471100500004],[142.90748131600003,44.64740631700005],[142.71404056100005,44.80621979400007],[142.66570071700005,44.86664459800005],[142.63070722700002,44.88373444200005],[142.6142684250001,44.90387604400007],[142.53931725400003,45.018459377000056],[142.2212020190001,45.29962799700007],[142.18311608200008,45.32599518400008],[142.09652754,45.372300523000035],[142.0566512380001,45.402044989000046],[142.01775149800005,45.45571523600006],[141.98755944100003,45.46849192900004],[141.96045983200008,45.51023997600004],[141.94499759200005,45.520412502000056],[141.91944420700008,45.51585521000004],[141.87867272200003,45.484035549000055],[141.875336134,45.44818756700005],[141.8232528000001,45.42255280200004],[141.77409915500004,45.41290924700007],[141.69214928500003,45.40228913000004],[141.6836043630001,45.43618398600006],[141.66570071700005,45.450425523000035],[141.63941491000003,45.421128648000035],[141.639008009,45.38898346600007],[141.64918053500003,45.341009833000044],[141.62769616000003,45.31439850500004],[141.61784915500004,45.28534577000005],[141.58375084700003,45.251166083000044],[141.57488040500004,45.20685455900008],[141.5864363940001,45.16437409100007],[141.72185306100005,44.955145575000074],[141.75359134200005,44.88544342700004],[141.78345787900003,44.76536692900004],[141.79607181100005,44.64175039300005],[141.79175866000003,44.596991278000075],[141.74724368600005,44.42422109600005],[141.66570071700005,44.31273021000004],[141.65544681100005,44.27692291900007],[141.67017662900003,44.06663646000004],[141.65894616000003,43.98379140800006],[141.6406356130001,43.937933661000045],[141.6157332690001,43.902777411000045],[141.58472741000003,43.875433661000045],[141.528168165,43.84121328300006],[141.490570509,43.827337958000044],[141.44800866000003,43.82249583500004],[141.40211022200003,43.803615627000056],[141.38502037900003,43.79132721600007],[141.35816491000003,43.75580475500004],[141.33741295700008,43.71283600500004],[141.34392337300005,43.69017161700003],[141.36695397200003,43.64630768400008],[141.36402428500003,43.62750885600008],[141.39136803500003,43.57908763200004],[141.36247806100005,43.54336172100005],[141.3598738940001,43.52358633000006],[141.37086022200003,43.49713776200008],[141.42701256600003,43.43455638200004],[141.43970787900003,43.40778229400007],[141.44011478000004,43.36172109600005],[141.42335045700008,43.31785716400009],[141.3974715500001,43.279730536000045],[141.37086022200003,43.25071849200003],[141.30396569100003,43.197251695000034],[141.2275496750001,43.15448639500005],[141.20085696700005,43.14362213700008],[141.16911868600005,43.14085521000004],[141.06983483200008,43.168850002000056],[141.0219832690001,43.174994208000044],[141.0044051440001,43.193060614000046],[141.02230879,43.22378164300005],[140.9882918630001,43.226996161000045],[140.9126082690001,43.20416901200008],[140.809906446,43.19245026200008],[140.78630618600005,43.20062897300005],[140.76140384200005,43.22345612200007],[140.68921959700003,43.24835846600007],[140.668793165,43.26032135600008],[140.62094160200002,43.301214911000045],[140.48715254,43.372381903000075],[140.46509850400003,43.368068752000056],[140.45289147200003,43.33885325700004],[140.409515821,43.32469310100004],[140.36011803500003,43.32518138200004],[140.32935631600003,43.25071849200003],[140.33806399800005,43.21051666900007],[140.36793053500003,43.18309153900009],[140.40609785200002,43.160223700000074],[140.43921959700003,43.134019273000035],[140.4873153000001,43.08270905200004],[140.5019637380001,43.04572174700007],[140.52711022200003,43.01544830900008],[140.52116946700005,42.99628327000005],[140.50318444100003,42.98004791900007],[140.46119225400003,42.95888906500005],[140.37712649800005,42.893784898000035],[140.33741295700008,42.83413320500006],[140.31177819100003,42.81598541900007],[140.3032332690001,42.76972077000005],[140.28150475400003,42.75726959800005],[140.2607528000001,42.760972398000035],[140.22291100400003,42.80133698100008],[140.20036868600005,42.81427643400008],[140.16976972700002,42.79584381700005],[140.13591556100005,42.74713776200008],[140.09546959700003,42.73379140800006],[140.05388431100005,42.69415924700007],[140.014008009,42.68158600500004],[139.91724694100003,42.67723216400009],[139.8769637380001,42.663397528000075],[139.84343509200005,42.626939195000034],[139.83277428500003,42.58881256700005],[139.84929446700005,42.51414622600004],[139.8496199880001,42.46930573100008],[139.8261824880001,42.402044989000046],[139.78443444100003,42.350816148000035],[139.7675887380001,42.310980536000045],[139.787445509,42.24677155200004],[139.84343509200005,42.20856354400007],[139.883636915,42.194973049000055],[139.90935306100005,42.15656159100007],[139.92750084700003,42.13890208500004],[140.02759850400003,42.105536200000074],[140.0610457690001,42.08002350500004],[140.1118270190001,42.01113515800006],[140.1450301440001,41.98265208500004],[140.1396590500001,41.94871653900009],[140.151703321,41.913763739000046],[140.1450301440001,41.87958405200004],[140.12777754,41.870794989000046],[140.129730665,41.82322825700004],[140.1176863940001,41.80451080900008],[140.0830184250001,41.800767320000034],[140.071136915,41.760199286000045],[140.0195418630001,41.696966864000046],[140.00342858200008,41.63467031500005],[139.98422285200002,41.603176174000055],[139.98365319100003,41.56049225500004],[140.014008009,41.50287506700005],[140.03825931100005,41.442572333000044],[140.10035241000003,41.41351959800005],[140.13746178500003,41.42096588700008],[140.18506920700008,41.40379466400009],[140.2089949880001,41.40110911700003],[140.22584069100003,41.437933661000045],[140.2412215500001,41.45722077000005],[140.27466881600003,41.48236725500004],[140.40064537900003,41.513006903000075],[140.43238366000003,41.53082916900007],[140.44320722700002,41.563706773000035],[140.43327884200005,41.64468008000006],[140.45289147200003,41.68105703300006],[140.5234481130001,41.69867584800005],[140.54476972700002,41.712591864000046],[140.60425866000003,41.73627350500004],[140.6157332690001,41.75470612200007],[140.6416121750001,41.81509023600006],[140.66041100400003,41.82713450700004],[140.7089949880001,41.82440827000005],[140.72722415500004,41.81134674700007],[140.7334090500001,41.79364655200004],[140.70671634200005,41.77411530200004],[140.698578321,41.759833075000074],[140.7080184250001,41.74555084800005],[140.72999108200008,41.749986070000034],[140.77507571700005,41.770412502000056],[140.82398522200003,41.770697333000044],[140.87427819100003,41.758246161000045],[140.9599715500001,41.721991278000075],[141.0063582690001,41.712144273000035],[141.043304884,41.723863023000035],[141.1040145190001,41.770412502000056],[141.1435653000001,41.78579336100006],[141.1899520190001,41.79092031500005],[141.19996178500003,41.79946523600006],[141.18279056100005,41.81830475500004],[141.10694420700008,41.87177155200004],[141.08326256600003,41.88263580900008],[141.01156660200002,41.893744208000044],[140.97429446700005,41.913763739000046],[140.90845787900003,41.978949286000045],[140.83708743600005,42.02362702000005],[140.78931725400003,42.07453034100007],[140.77361087300005,42.10219961100006],[140.71355228000004,42.13349030200004],[140.67408287900003,42.12205638200004],[140.61011803500003,42.117621161000045],[140.5488387380001,42.105536200000074],[140.5302840500001,42.111761786000045],[140.456390821,42.173081773000035],[140.42408287900003,42.192287502000056],[140.321625196,42.23395416900007],[140.29175866000003,42.25958893400008],[140.28150475400003,42.30483633000006],[140.28858483200008,42.35016510600008],[140.30396569100003,42.39297109600005],[140.32545006600003,42.432806708000044],[140.3808699880001,42.505072333000044],[140.4170028000001,42.53815338700008],[140.4575301440001,42.56427643400008],[140.50066165500004,42.57916901200008],[140.52564537900003,42.58063385600008],[140.59742272200003,42.57172272300005],[140.66293379,42.57982005400004],[140.68628991000003,42.57916901200008],[140.73365319100003,42.564154364000046],[140.9013778000001,42.42084381700005],[140.9121199880001,42.380560614000046],[140.94695071700005,42.353216864000046],[140.9868270190001,42.339829820000034],[140.97543379,42.32656484600005],[140.9360457690001,42.33270905200004],[140.95240319100003,42.30483633000006],[140.97730553500003,42.29962799700007],[141.01677493600005,42.316107489000046],[141.0732528000001,42.37714264500005],[141.09156334700003,42.391791083000044],[141.16871178500003,42.433294989000046],[141.21509850400003,42.444525458000044],[141.26246178500003,42.46824778900009],[141.4277449880001,42.56439850500004],[141.54957116000003,42.60114166900007],[141.63648522200003,42.61579010600008],[141.72608483200008,42.617173570000034],[141.81714928500003,42.599595445000034],[141.88819420700008,42.56976959800005],[141.92595462300005,42.558579820000034],[141.9809676440001,42.51023997600004],[142.06258038500005,42.47072827200009],[142.16775167700007,42.45985850800008],[142.22217858200008,42.411769924000055],[142.28101647200003,42.366522528000075],[142.4599715500001,42.27130768400008],[142.54639733200008,42.25055573100008],[142.75766035200002,42.15839264500005],[142.95972741000003,42.10537344000005],[143.1152449880001,42.02798086100006],[143.16334069100003,41.99038320500006],[143.18238366000003,41.96157461100006],[143.22608483200008,41.93227773600006],[143.2529403000001,41.94188060100004],[143.26547285200002,41.98672109600005],[143.292328321,42.005194403000075],[143.30347741000003,42.02578359600005],[143.3310653000001,42.10691966400009],[143.3379012380001,42.15021393400008],[143.32488040500004,42.229722398000035],[143.33171634200005,42.27724844000005],[143.348968946,42.31915924700007],[143.44809004,42.45502350500004],[143.5752059250001,42.60333893400008],[143.63884524800005,42.66107819200005],[143.79029381600003,42.75726959800005],[143.88640384200005,42.844183661000045],[144.0420028000001,42.92658112200007],[144.18311608200008,42.97638580900008],[144.32300866000003,43.00263092700004],[144.35808353000004,42.99945709800005],[144.3730574880001,42.96954987200007],[144.3906356130001,42.95644765800006],[144.4648543630001,42.93707916900007],[144.49586022200003,42.933254299000055],[144.52173912900003,42.94407786700003],[144.59913170700008,42.94912344000005],[144.74195397200003,42.92422109600005],[144.78345787900003,42.934759833000044],[144.74146569100003,42.96523672100005],[144.739105665,42.99135976800005],[144.75757897200003,43.018784898000035],[144.79867597700002,43.04669830900008],[144.840179884,43.04010651200008],[144.84205162900003,43.007147528000075],[144.86882571700005,42.98847077000005],[144.899750196,42.97947825700004],[144.93376712300005,42.97825755400004],[145.00017337300005,42.98615143400008],[145.02369225400003,42.99628327000005],[145.02751712300005,43.00934479400007],[145.00310306100005,43.044623114000046],[145.0512801440001,43.03310781500005],[145.07569420700008,43.03400299700007],[145.09929446700005,43.044623114000046],[145.11101321700005,43.06362539300005],[145.14918053500003,43.06631094000005],[145.16822350400003,43.079413153000075],[145.12273196700005,43.08820221600007],[145.1254988940001,43.12006256700005],[145.15406334700003,43.13226959800005],[145.23113040500004,43.14109935100004],[145.2583113940001,43.14948151200008],[145.30250084700003,43.173488674000055],[145.40406334700003,43.174994208000044],[145.45240319100003,43.18390534100007],[145.47828209700003,43.17210521000004],[145.51710045700008,43.174994208000044],[145.54558353000004,43.20197174700007],[145.57984459700003,43.26504140800006],[145.6064559250001,43.28424713700008],[145.62916100400003,43.309759833000044],[145.6840926440001,43.310248114000046],[145.70850670700008,43.32632070500006],[145.760101759,43.33698151200008],[145.777679884,43.35626862200007],[145.81031334700003,43.36078522300005],[145.81324303500003,43.378607489000046],[145.76742597700002,43.38727448100008]]],[[[123.81470787900003,24.391750393000052],[123.77686608200008,24.419745184000078],[123.769867384,24.418361721000053],[123.75318444100003,24.364447333000044],[123.74805748800009,24.32957591400009],[123.72925866000003,24.315741278000075],[123.70704186300009,24.328680731000077],[123.68376712300005,24.323472398000035],[123.66822350400003,24.296616929000038],[123.68344160200002,24.285305080000057],[123.74577884200005,24.28123607000009],[123.85124759200005,24.256984768000052],[123.87305748800009,24.258246161000045],[123.91553795700008,24.30683014500005],[123.93604576900009,24.341457424000055],[123.93441816500001,24.36749909100007],[123.910492384,24.37954336100006],[123.87208092500009,24.39012278900009],[123.83472741000003,24.39549388200004],[123.81470787900003,24.391750393000052]]],[[[123.00261478000004,24.474310614000046],[122.94459069100003,24.46625397300005],[122.94752037900003,24.439520575000074],[122.98780358200008,24.443304755000042],[122.998383009,24.439520575000074],[123.01823978000004,24.45929596600007],[123.00261478000004,24.474310614000046]]],[[[124.32911217500009,24.58356354400007],[124.31446373800009,24.58730703300006],[124.30062910200002,24.56989166900007],[124.26840254000001,24.51044342700004],[124.24024498800009,24.49485911700009],[124.21192467500009,24.446966864000046],[124.19125410200002,24.440008856000077],[124.15650475400003,24.44163646000004],[124.11622155000009,24.46747467700004],[124.09522545700008,24.439520575000074],[124.08863366000003,24.421942450000074],[124.12273196700005,24.41429271000004],[124.14307701900009,24.384914455000057],[124.12224368600005,24.360581773000035],[124.14031009200005,24.34198639500005],[124.17701256600003,24.33299388200004],[124.21192467500009,24.337103583000044],[124.24187259200005,24.363714911000045],[124.25391686300009,24.40180084800005],[124.26050866000003,24.445257880000042],[124.27393639400009,24.487941799000055],[124.30909264400009,24.534857489000046],[124.33228600400003,24.57103099200009],[124.32911217500009,24.58356354400007]]],[[[141.357758009,24.789618231000077],[141.33025149800005,24.794094143000052],[141.29948978000004,24.78587474200009],[141.28980553500003,24.760239976000037],[141.30258222700002,24.73436107000009],[141.3305770190001,24.76911041900007],[141.35726972700002,24.77342357000009],[141.357758009,24.789618231000077]]],[[[125.38721764400009,24.782171942000048],[125.35824629000001,24.78261953300006],[125.32667076900009,24.81073639500005],[125.30225670700008,24.857570705000057],[125.28516686300009,24.877346096000053],[125.26026451900009,24.885199286000045],[125.27475019600001,24.85643138200004],[125.27393639400009,24.820013739000046],[125.26148522200003,24.76386139500005],[125.25879967500009,24.73338450700004],[125.29965254000001,24.72565338700008],[125.38379967500009,24.726955471000053],[125.44402103000004,24.737209377000056],[125.44483483200008,24.756740627000056],[125.42628014400009,24.768866278000075],[125.38721764400009,24.782171942000048]]],[[[131.25513756600003,25.87498607000009],[131.23609459700003,25.87205638200004],[131.21273847700002,25.841457424000055],[131.21208743600005,25.829046942000048],[131.2524520190001,25.81903717700004],[131.2641707690001,25.84634023600006],[131.27711022200003,25.85838450700004],[131.25513756600003,25.87498607000009]]],[[[126.81006920700008,26.35187409100007],[126.78711998800009,26.380072333000044],[126.75212649800005,26.390692450000074],[126.72364342500009,26.385972398000035],[126.69776451900009,26.36904531500005],[126.70362389400009,26.355292059000078],[126.75993899800005,26.334418036000045],[126.78239993600005,26.304510809000078],[126.80738366000003,26.332586981000077],[126.81006920700008,26.35187409100007]]],[[[142.19320722700002,26.64606354400007],[142.1801863940001,26.67401764500005],[142.16578209700003,26.674383856000077],[142.1318465500001,26.714992580000057],[142.1113387380001,26.721177476000037],[142.139008009,26.662502346000053],[142.15992272200003,26.632025458000044],[142.17505944100003,26.630072333000044],[142.19320722700002,26.64606354400007]]],[[[128.33765709700003,26.796942450000074],[128.331390821,26.813421942000048],[128.25505618600005,26.88629791900007],[128.2544051440001,26.864406643000052],[128.225922071,26.80097077000005],[128.1879988940001,26.780503648000035],[128.16195722700002,26.757147528000075],[128.1523543630001,26.73151276200008],[128.1113387380001,26.70343659100007],[128.10328209700003,26.691229559000078],[128.11109459700003,26.67401764500005],[128.05738366000003,26.64476146000004],[128.01490319100003,26.634833075000074],[127.99561608200008,26.639227606000077],[127.98853600400003,26.65298086100006],[128.00123131600003,26.680243231000077],[127.98731530000009,26.69293854400007],[127.96461022200003,26.69717031500005],[127.88542728000004,26.696112372000073],[127.87794030000009,26.68659088700008],[127.88575280000009,26.653509833000044],[127.88559004000001,26.62018463700008],[127.89429772200003,26.60927969000005],[127.94060306100005,26.602484442000048],[127.96241295700008,26.581976630000042],[127.96021569100003,26.564113674000055],[127.93751061300009,26.532131252000056],[127.902679884,26.516424872000073],[127.86426842500009,26.505682684000078],[127.83057701900009,26.488430080000057],[127.80518639400009,26.44757721600007],[127.78109785200002,26.441229559000078],[127.73129316500001,26.439886786000045],[127.72250410200002,26.42991771000004],[127.72437584700003,26.38544342700004],[127.74187259200005,26.32054271000004],[127.73129316500001,26.28937409100007],[127.71827233200008,26.27407461100006],[127.70020592500009,26.27610911700009],[127.67774498800009,26.230617580000057],[127.65845787900003,26.21234772300005],[127.63819420700008,26.21474844000005],[127.63591556100005,26.191351630000042],[127.65707441500001,26.12177155200004],[127.65170332100001,26.084377346000053],[127.71013431100005,26.084377346000053],[127.72535241000003,26.089585679000038],[127.74170983200008,26.115627346000053],[127.75489342500009,26.12531159100007],[127.775726759,26.12177155200004],[127.79688561300009,26.13971588700008],[127.82357832100001,26.186753648000035],[127.77540123800009,26.18512604400007],[127.76840254000001,26.197658596000053],[127.77751712300005,26.233343817000048],[127.82829837300005,26.316229559000078],[127.84083092500009,26.323919989000046],[127.90186608200008,26.30499909100007],[127.91993248800009,26.30963776200008],[127.88884524800005,26.339097398000035],[127.873383009,26.36481354400007],[127.86475670700008,26.40648021000004],[127.83887780000009,26.42523834800005],[127.85173587300005,26.441229559000078],[127.87061608200008,26.447414455000057],[127.94027754000001,26.447739976000037],[127.95093834700003,26.46670156500005],[127.98414147200003,26.478461005000042],[128.03638756600003,26.52732982000009],[128.05591881600003,26.550482489000046],[128.071625196,26.54401276200008],[128.108734571,26.54515208500004],[128.14177493600005,26.556586005000042],[128.14576256600003,26.573431708000044],[128.13209069100003,26.581244208000044],[128.15162194100003,26.612453518000052],[128.172211134,26.623480536000045],[128.217133009,26.62563711100006],[128.24333743600005,26.633286851000037],[128.26441491000003,26.652411200000074],[128.33765709700003,26.76430898600006],[128.32984459700003,26.78034088700008],[128.33765709700003,26.796942450000074]]],[[[142.19320722700002,27.091742255000042],[142.1801863940001,27.078070380000042],[142.198578321,27.05109284100007],[142.21851647200003,27.040350653000075],[142.23462975400003,27.050034898000035],[142.24105879,27.08429596600007],[142.22087649800005,27.09833405200004],[142.19320722700002,27.091742255000042]]],[[[128.70069420700008,27.454820054000038],[128.6504012380001,27.434719143000052],[128.61882571700005,27.41559479400007],[128.5615340500001,27.420396226000037],[128.53386478000004,27.415269273000035],[128.52613366000003,27.39716217700004],[128.52686608200008,27.371893622000073],[128.5454207690001,27.35374583500004],[128.57447350400003,27.353338934000078],[128.58741295700008,27.36204661700009],[128.61312910200002,27.36432526200008],[128.63070722700002,27.37567780200004],[128.65577233200008,27.41014232000009],[128.697601759,27.43573639500005],[128.70069420700008,27.454820054000038]]],[[[128.9887801440001,27.811102606000077],[128.97730553500003,27.83661530200004],[128.96550540500004,27.894354559000078],[128.94703209700003,27.914129950000074],[128.8945418630001,27.89130280200004],[128.88851972700002,27.853176174000055],[128.8789168630001,27.824774481000077],[128.899261915,27.76951732000009],[128.89087975400003,27.741888739000046],[128.8945418630001,27.728216864000046],[128.916351759,27.722357489000046],[128.92945397200003,27.686712958000044],[128.96070397200003,27.68577708500004],[128.98951256600003,27.707464911000045],[129.024261915,27.75657786700009],[129.02466881600003,27.786078192000048],[128.9887801440001,27.811102606000077]]],[[[129.29664147200003,28.119574286000045],[129.2641707690001,28.134711005000042],[129.2504988940001,28.15102773600006],[129.26929772200003,28.17487213700008],[129.23259524800005,28.170721747000073],[129.22771243600005,28.195257880000042],[129.1958113940001,28.195461330000057],[129.18702233200008,28.174994208000044],[129.21412194100003,28.14008209800005],[129.218109571,28.10610586100006],[129.23064212300005,28.09666575700004],[129.26392662900003,28.09634023600006],[129.27914472700002,28.077093817000048],[129.3007918630001,28.100531317000048],[129.32398522200003,28.078599351000037],[129.35043379,28.086493231000077],[129.34734134200005,28.10691966400009],[129.325531446,28.123480536000045],[129.29664147200003,28.119574286000045]]],[[[130.0302840500001,28.373480536000045],[129.9755965500001,28.34324778900009],[129.91358483200008,28.29710521000004],[129.95655358200008,28.284816799000055],[129.995453321,28.29092031500005],[130.0214949880001,28.331284898000035],[130.0302840500001,28.373480536000045]]],[[[129.7075301440001,28.462144273000035],[129.69800866000003,28.49485911700009],[129.68775475400003,28.508978583000044],[129.66724694100003,28.50299713700008],[129.67335045700008,28.48273346600007],[129.64405358200008,28.43976471600007],[129.61394290500004,28.44798411700009],[129.62623131600003,28.48273346600007],[129.56080162900003,28.451117255000042],[129.53345787900003,28.430487372000073],[129.51636803500003,28.39329661700009],[129.4824324880001,28.411037502000056],[129.45093834700003,28.39203522300005],[129.43230228000004,28.38690827000005],[129.41334069100003,28.39329661700009],[129.39958743600005,28.37173086100006],[129.38062584700003,28.36676666900007],[129.33130944100003,28.366034247000073],[129.31625410200002,28.350165106000077],[129.28825931100005,28.33307526200008],[129.26392662900003,28.32884349200009],[129.22771243600005,28.30459219000005],[129.25367272200003,28.28457265800006],[129.290375196,28.284084377000056],[129.270843946,28.25535716400009],[129.24138431100005,28.256333726000037],[129.20883222700002,28.264227606000077],[129.180511915,28.25617096600007],[129.16285241000003,28.262640692000048],[129.15894616000003,28.240627346000053],[129.207855665,28.229437567000048],[129.24244225400003,28.21430084800005],[129.28077233200008,28.218573309000078],[129.28663170700008,28.17792389500005],[129.33757571700005,28.14752838700008],[129.3657332690001,28.123195705000057],[129.3794051440001,28.12596263200004],[129.36443118600005,28.154038804000038],[129.3711043630001,28.167222398000035],[129.40154056100005,28.163316148000035],[129.41521243600005,28.18573639500005],[129.46794681100005,28.215765692000048],[129.42017662900003,28.249945380000042],[129.43962649800005,28.259019273000035],[129.46119225400003,28.29169342700004],[129.4873153000001,28.282538153000075],[129.49887129,28.287502346000053],[129.53345787900003,28.318833726000037],[129.55396569100003,28.32843659100007],[129.58155358200008,28.37067291900007],[129.61117597700002,28.38312409100007],[129.61931399800005,28.400783596000053],[129.6816512380001,28.421087958000044],[129.71599368600005,28.42914459800005],[129.7216903000001,28.44403717700004],[129.7075301440001,28.462144273000035]]],[[[129.7148543630001,29.65452708500004],[129.7129012380001,29.629299221000053],[129.721039259,29.60614655200004],[129.74382571700005,29.622300523000035],[129.75375410200002,29.645412502000056],[129.74594160200002,29.661037502000056],[129.7148543630001,29.65452708500004]]],[[[129.8935653000001,29.874741929000038],[129.85098067800004,29.886729695000042],[129.83277226500002,29.873077651000074],[129.840184754,29.84972943000008],[129.86340189200007,29.82460653000004],[129.9204207690001,29.819647528000075],[129.92725670700008,29.825873114000046],[129.89741851200006,29.846915495000076],[129.8935653000001,29.874741929000038]]],[[[130.67457116000003,30.37457916900007],[130.6386781760001,30.40266076900008],[130.59958849400005,30.41245819800008],[130.56782501700002,30.435941711000055],[130.52369225400003,30.449652411000045],[130.4989507360001,30.467962261000082],[130.48834512500002,30.457026279000047],[130.467118926,30.45722526700007],[130.4611824100001,30.441141568000035],[130.42336102800004,30.398968251000042],[130.376246508,30.394435997000073],[130.38626403800004,30.339605661000064],[130.4271399380001,30.255535937000047],[130.4377132080001,30.238970153000082],[130.48597745300003,30.22577701000006],[130.57727971400004,30.23590585900007],[130.60375105100002,30.252468518000057],[130.64714603000004,30.29051341400009],[130.66944420700008,30.339667059000078],[130.67457116000003,30.37457916900007]]],[[[131.05201256600003,30.840236721000053],[131.02717589000008,30.819371621000073],[131.00625712800002,30.789246757000058],[131.00813866600004,30.767864858000053],[130.99970311700008,30.745686335000073],[130.96634534600003,30.701441462000048],[130.93929969700002,30.672251498000037],[130.94837684900006,30.64110939300008],[130.95093834700003,30.595038153000075],[130.93022119900002,30.54085964500007],[130.90113980700005,30.503691978000063],[130.8762783420001,30.480704794000076],[130.870002258,30.46386097900006],[130.85149508200004,30.470143740000083],[130.8533975470001,30.440830762000076],[130.86257151000007,30.42394700300008],[130.8655153950001,30.397292703000062],[130.857117893,30.36619154600004],[130.87038107900003,30.343904295000073],[130.88581760500006,30.34385066400006],[130.90651933000004,30.366014516000064],[130.9436506090001,30.37468118600009],[130.97779709100007,30.400280234000036],[130.96963857800006,30.41543863600009],[130.97803086800002,30.43759680100004],[130.9647533760001,30.456319105000034],[130.96696052800007,30.479399958000045],[130.98047401100007,30.49709550500006],[130.98263885900008,30.519305321000047],[131.00340350800002,30.55116662200004],[131.0355637560001,30.557209431000047],[131.037847983,30.58917133600005],[131.05761514300002,30.611269028000038],[131.05376059500009,30.661867794000045],[131.07893733100002,30.699084278000043],[131.070968966,30.737329878000082],[131.07120798000005,30.76930179900006],[131.087933572,30.782606299000065],[131.073594694,30.79958158100004],[131.05201256600003,30.840236721000053]]],[[[130.15308678500003,32.20376211100006],[130.1211043630001,32.21263255400004],[130.11109459700003,32.19330475500004],[130.12777754,32.13231028900009],[130.149261915,32.11050039300005],[130.167328321,32.11432526200008],[130.19410241000003,32.17641836100006],[130.18726647200003,32.19635651200008],[130.15308678500003,32.20376211100006]]],[[[130.41000410200002,32.51162344000005],[130.3833113940001,32.509426174000055],[130.36304772200003,32.52708567900004],[130.34156334700003,32.51434967700004],[130.3051863940001,32.50405508000006],[130.26783287900003,32.48419830900008],[130.24789472700002,32.46458567900004],[130.21859785200002,32.455959377000056],[130.21306399800005,32.43650950700004],[130.24195397200003,32.39549388200004],[130.28443444100003,32.407538153000075],[130.32447350400003,32.409816799000055],[130.343109571,32.42182038000004],[130.35865319100003,32.41290924700007],[130.3623153000001,32.374986070000034],[130.39283287900003,32.39716217700004],[130.43482506600003,32.49274323100008],[130.46851647200003,32.51162344000005],[130.4570418630001,32.52558014500005],[130.41000410200002,32.51162344000005]]],[[[130.160492384,32.54633209800005],[130.13982181100005,32.54767487200007],[130.07691491000003,32.53099192900004],[130.05648847700002,32.521185614000046],[130.03296959700003,32.521918036000045],[130.0254012380001,32.50214264500005],[130.0302840500001,32.482407945000034],[129.99439537900003,32.42413971600007],[129.98902428500003,32.40448639500005],[129.995453321,32.388657945000034],[129.9804793630001,32.37563711100006],[129.97527103000004,32.35541413000004],[129.99146569100003,32.314439195000034],[130.0087996750001,32.305894273000035],[130.0302840500001,32.31362539300005],[130.0532332690001,32.296535549000055],[130.0166121750001,32.29311758000006],[129.9653426440001,32.25820547100005],[129.97584069100003,32.24591705900008],[129.99732506600003,32.24404531500005],[129.99439537900003,32.19989655200004],[130.01197350400003,32.190619208000044],[130.07496178500003,32.220770575000074],[130.10531660200002,32.25214264500005],[130.1333113940001,32.26512278900009],[130.15772545700008,32.310248114000046],[130.19068444100003,32.323553778000075],[130.20337975400003,32.33885325700004],[130.208343946,32.37327708500004],[130.201508009,32.39549388200004],[130.19898522200003,32.480169989000046],[130.1894637380001,32.52179596600007],[130.160492384,32.54633209800005]]],[[[128.87842693200002,32.673924825000086],[128.842068713,32.70386139100009],[128.84978274800005,32.74274323100008],[128.8422957690001,32.759466864000046],[128.8105574880001,32.766058661000045],[128.8084903890001,32.799908412000036],[128.78412547900007,32.788181200000054],[128.779223828,32.776625290000084],[128.7431746750001,32.75828685100004],[128.69965289100003,32.743771302000084],[128.679047071,32.782904364000046],[128.66081790500004,32.78278229400007],[128.64176757200005,32.76138397300008],[128.6504012380001,32.74453359600005],[128.6425887380001,32.72239817900004],[128.65349368600005,32.69961172100005],[128.652679884,32.66791413000004],[128.6318465500001,32.66986725500004],[128.6181746750001,32.65965403900009],[128.60043889300005,32.614177909000034],[128.65263104100006,32.59107501300008],[128.66285241000003,32.601548570000034],[128.7360418190001,32.603493841000045],[128.75538170700008,32.580511786000045],[128.773110265,32.57166584300006],[128.79020082700004,32.58638451500008],[128.79495874300005,32.610093420000055],[128.77222741000003,32.63190338700008],[128.79322350400003,32.65249258000006],[128.81548846500004,32.64681013500007],[128.81771562400002,32.63458958800004],[128.85789915700002,32.64340912400007],[128.899261915,32.64252350500004],[128.87842693200002,32.673924825000086]]],[[[139.7910262380001,33.13532135600008],[139.76775149800005,33.14988841400009],[139.73975670700008,33.133246161000045],[139.74341881600003,33.11859772300005],[139.7763778000001,33.07998281500005],[139.7876082690001,33.05841705900008],[139.82097415500004,33.040269273000035],[139.84522545700008,33.04677969000005],[139.8774520190001,33.087591864000046],[139.8623153000001,33.10700104400007],[139.81316165500004,33.12376536700009],[139.7910262380001,33.13532135600008]]],[[[129.097992384,32.988267320000034],[129.09725354,33.01810575300004],[129.1027123340001,33.05449343500004],[129.12391110500005,33.109172923000074],[129.1085937680001,33.12241788400007],[129.10172462800006,33.08297300600009],[129.085875278,33.07620750500007],[129.08099664400004,33.05678184700008],[129.0839949880001,33.02338288000004],[129.067645125,33.01911582900004],[129.06521060500006,33.04395398200006],[129.04862219400002,33.039602796000054],[129.04258414900005,33.00015967200005],[129.05445397200003,32.97931549700007],[129.03503841200006,32.967404474000034],[129.02042041900006,32.980624687000045],[128.98146031200008,32.960351967000065],[128.97942307000005,32.94760914500006],[129.03305097700002,32.93333567900004],[129.0478195170001,32.909964614000046],[129.04147538400002,32.89599958800005],[129.04041432000008,32.862728196000035],[129.04572282800007,32.822092732000044],[129.0649520190001,32.81781647300005],[129.0679197190001,32.86118283400009],[129.0965722520001,32.85694237600006],[129.09253686500006,32.90660793700005],[129.11754349400007,32.935841081000035],[129.1372176440001,32.93817780200004],[129.15632505300005,32.958490107000046],[129.17595462300005,32.96698639500005],[129.18280727800004,32.984685539000054],[129.1696282160001,33.005816536000054],[129.109711134,32.98289622600004],[129.097992384,32.988267320000034]]],[[[129.5556842000001,33.41080491100007],[129.52532080700007,33.40492725000007],[129.49512950000008,33.35829457300008],[129.442662691,33.34806608900004],[129.43726510800002,33.296679880000056],[129.40641102400002,33.27495079100004],[129.40843180000002,33.253823673000056],[129.3776568840001,33.21463952800008],[129.40650475400003,33.19403717700004],[129.384781986,33.183493173000045],[129.3607009320001,33.19871806800006],[129.342500664,33.179620180000086],[129.37602131000006,33.16971922700009],[129.41259095300006,33.17831786000005],[129.44660473600004,33.19851960500006],[129.49309512600007,33.245163286000036],[129.501148101,33.28003244000007],[129.51434189400004,33.29804494000007],[129.5383170030001,33.31559461200004],[129.54202815300005,33.33567670100007],[129.56216330100006,33.366393867000056],[129.5556842000001,33.41080491100007]]],[[[132.24366295700008,33.79328034100007],[132.20875084700003,33.800116278000075],[132.204437696,33.782131252000056],[132.2363387380001,33.76854075700004],[132.27409915500004,33.77220286700009],[132.24366295700008,33.79328034100007]]],[[[129.7763778000001,33.773423570000034],[129.7968856130001,33.786444403000075],[129.76678701100002,33.80209537200005],[129.762202774,33.82313843700007],[129.76726321700005,33.84454987200007],[129.70069420700008,33.86155833500004],[129.68671992200007,33.85979586600007],[129.67314449900005,33.832217928000034],[129.6569338810001,33.81545368900004],[129.6602833510001,33.783591931000046],[129.64665808300003,33.77277290600006],[129.657126909,33.74255478300006],[129.71045983200008,33.700832424000055],[129.73506403500005,33.73673856400006],[129.78386478000004,33.73818594000005],[129.79965254,33.75104401200008],[129.79957116000003,33.764837958000044],[129.7763778000001,33.773423570000034]]],[[[132.23617597700002,33.86904531500005],[132.284515821,33.86155833500004],[132.29810631600003,33.884019273000035],[132.3286238940001,33.875067450000074],[132.32837975400003,33.85651276200008],[132.34058678500003,33.844956773000035],[132.3740340500001,33.84170156500005],[132.38070722700002,33.886419989000046],[132.3945418630001,33.899318752000056],[132.4365340500001,33.903550523000035],[132.46949303500003,33.937933661000045],[132.42758222700002,33.939886786000045],[132.40967858200008,33.93390534100007],[132.39527428500003,33.91547272300005],[132.3740340500001,33.91681549700007],[132.34009850400003,33.89720286700009],[132.3261824880001,33.89630768400008],[132.298187696,33.93048737200007],[132.25359134200005,33.950100002000056],[132.22608483200008,33.95270416900007],[132.20191491000003,33.94476959800005],[132.18628991000003,33.921210028000075],[132.1933699880001,33.897650458000044],[132.21631920700008,33.85537344000005],[132.23617597700002,33.86904531500005]]],[[[139.48707116000003,34.06077708500004],[139.50611412900003,34.03587474200009],[139.534515821,34.037543036000045],[139.56128991000003,34.05809153900009],[139.575043165,34.09003327000005],[139.55632571700005,34.11347077000005],[139.5234481130001,34.11237213700008],[139.49439537900003,34.09275950700004],[139.48707116000003,34.06077708500004]]],[[[132.5551863940001,34.07135651200008],[132.56983483200008,34.075873114000046],[132.5805770190001,34.09983958500004],[132.57764733200008,34.11888255400004],[132.55152428500003,34.108587958000044],[132.543304884,34.17670319200005],[132.51929772200003,34.17438385600008],[132.49683678500003,34.15704987200007],[132.51002037900003,34.13182200700004],[132.49732506600003,34.11713288000004],[132.45533287900003,34.08812083500004],[132.47803795700008,34.08047109600005],[132.5322371750001,34.082098700000074],[132.5551863940001,34.07135651200008]]],[[[132.99947548700004,34.208377288000065],[133.03484134200005,34.20734284100007],[133.0517684250001,34.222316799000055],[133.0556746750001,34.24237702000005],[133.0449324880001,34.273098049000055],[133.03003991000003,34.29531484600005],[133.01002037900003,34.29124583500004],[132.9697371750001,34.25263092700004],[132.99170983200008,34.242254950000074],[132.98585045700008,34.22695547100005],[132.9629012380001,34.218451239000046],[132.9638778000001,34.19188060100004],[132.99947548700004,34.208377288000065]]],[[[129.31252838900002,34.27975637600008],[129.30143578800005,34.28809728200008],[129.30246202400008,34.30778735000007],[129.28019698700007,34.32287172800005],[129.2620431790001,34.30776139300008],[129.22405260200003,34.29344308800006],[129.22524498500002,34.32740336900008],[129.19853454100007,34.316228084000045],[129.18925958300008,34.234227385000054],[129.18049970800007,34.227217013000086],[129.17588028900002,34.195509135000066],[129.17880296200008,34.169765202000065],[129.1703536440001,34.14681568300006],[129.17163425900003,34.10680681200006],[129.19030802600003,34.11481074200009],[129.216062405,34.09833929200005],[129.2482968390001,34.09992685800006],[129.27658744000007,34.12911120600006],[129.287848285,34.16512018200007],[129.29721196100002,34.170476062000034],[129.29971373600006,34.19782318000006],[129.30938369900002,34.21905214800006],[129.32480321700007,34.22925262400008],[129.31807550400003,34.25834963300008],[129.34296047100008,34.278277529000036],[129.34258315800002,34.29305183900004],[129.31678390000002,34.294468719000065],[129.31252838900002,34.27975637600008]]],[[[139.270355665,34.39598216400009],[139.25367272200003,34.34137604400007],[139.26783287900003,34.322699286000045],[139.28345787900003,34.33685944200005],[139.29639733200008,34.36786530200004],[139.30250084700003,34.399725653000075],[139.2915145190001,34.41453685100004],[139.270355665,34.39598216400009]]],[[[135.002681887,34.60842304800008],[134.98133299800008,34.60240240600007],[134.93811010500008,34.55090579700004],[134.8838308930001,34.529488695000055],[134.8564559250001,34.483303127000056],[134.84253991000003,34.47028229400007],[134.79895562900003,34.44679379100006],[134.72081440700003,34.329788749000045],[134.70515563800006,34.32316807600006],[134.68325958600008,34.328226407000045],[134.65716913300002,34.288204316000076],[134.6665145190001,34.26552969000005],[134.68811432000007,34.24357989100008],[134.72388756600003,34.25263092700004],[134.70216251400007,34.221198790000074],[134.72686973700002,34.209970042000066],[134.72867714100005,34.190939349000075],[134.800049522,34.20075373700007],[134.83385849900003,34.22661011400004],[134.8849059370001,34.24448107000006],[134.90947036600005,34.25718023200005],[134.95825406100005,34.27035550100004],[134.95045006600003,34.28733958500004],[134.92618311400008,34.30849507800008],[134.91627037900003,34.33828359600005],[134.899674838,34.34903575900006],[134.89576256600003,34.372748114000046],[134.902973179,34.41900554700004],[134.9563008670001,34.48487725400008],[134.98414147200003,34.50576406500005],[135.00327729100002,34.551574781000056],[135.03012129,34.57884349200009],[135.028168165,34.591009833000044],[135.002681887,34.60842304800008]]],[[[129.45264733200008,34.61945221600007],[129.480912804,34.619909575000065],[129.4882777150001,34.63567767100005],[129.47814236700003,34.65072063800005],[129.4877511300001,34.676396890000035],[129.46456058500007,34.70080530600006],[129.41822846900004,34.68539200500004],[129.37833743400006,34.644851764000066],[129.32330009600003,34.64807215700006],[129.303359203,34.58687697000005],[129.28910691400006,34.56986674800004],[129.30845618800004,34.56038743000005],[129.3261191900001,34.53543877000004],[129.3049307670001,34.520287272000076],[129.276044124,34.46218849400009],[129.28073429900007,34.45064666500008],[129.26856862500006,34.43305128800006],[129.27375144500002,34.41293237800005],[129.272349114,34.375696607000066],[129.23538380000002,34.368132348000074],[129.24829130400008,34.351925460000075],[129.27199211700008,34.35621928200004],[129.30493826400004,34.37242417900006],[129.3051037470001,34.34320609200006],[129.3345957140001,34.36059135700009],[129.3470027080001,34.35585198900009],[129.34506369500002,34.32724397800007],[129.32090476000008,34.333849042000054],[129.30546399500008,34.32487545400005],[129.324511883,34.304593338000075],[129.34383322200006,34.30031431500004],[129.368241614,34.30743171200004],[129.4079686450001,34.351579901000036],[129.38814520100004,34.36675509200006],[129.38431153200008,34.38341631900005],[129.35946395400003,34.39062150700005],[129.3618308670001,34.405485748000046],[129.38428795700008,34.42401764500005],[129.379472781,34.452622973000075],[129.40232637400004,34.48164673200006],[129.4460831890001,34.51477564000004],[129.464241065,34.55427396300007],[129.47820071700005,34.59711334800005],[129.45264733200008,34.61945221600007]]],[[[139.41822350400003,34.78082916900007],[139.35646512200003,34.79722949200004],[139.35119548300008,34.72003363400006],[139.3593528460001,34.70229865300007],[139.42164147200003,34.67719147300005],[139.45378665500004,34.678941148000035],[139.457855665,34.689886786000045],[139.45671634200005,34.73712799700007],[139.43775475400003,34.771918036000045],[139.41822350400003,34.78082916900007]]],[[[133.01246178500003,36.01479726800005],[133.0278426440001,35.99363841400009],[133.0649520190001,35.99506256700005],[133.06666100400003,36.00653717700004],[133.02418053500003,36.02558014500005],[133.01246178500003,36.01479726800005]]],[[[133.36703535200002,36.27423737200007],[133.3486434250001,36.29759349200009],[133.329437696,36.30491771000004],[133.32016035200002,36.32184479400007],[133.29281660200002,36.33055247600004],[133.2920028000001,36.34247467700004],[133.2094832690001,36.29718659100007],[133.1962996750001,36.27798086100006],[133.18775475400003,36.216131903000075],[133.24976647200003,36.16828034100007],[133.32601972700002,36.15814850500004],[133.33741295700008,36.16559479400007],[133.34351647200003,36.204331773000035],[133.360199415,36.19163646000004],[133.379242384,36.208238023000035],[133.3818465500001,36.25771719000005],[133.36703535200002,36.27423737200007]]],[[[138.45923912900003,38.06989166900007],[138.4375106130001,38.063788153000075],[138.4438582690001,38.10224030200004],[138.48658287900003,38.181301174000055],[138.50709069100003,38.241441148000035],[138.51929772200003,38.30744049700007],[138.5141707690001,38.32306549700007],[138.48951256600003,38.32245514500005],[138.46941165500004,38.31317780200004],[138.42855879,38.256293036000045],[138.40186608200008,38.24119700700004],[138.3481551440001,38.19281647300005],[138.31006920700008,38.16811758000006],[138.29468834700003,38.13617584800005],[138.25310306100005,38.09031810100004],[138.23560631600003,38.05215078300006],[138.23267662900003,38.00775788000004],[138.24789472700002,37.978501695000034],[138.314789259,37.99746328300006],[138.34253991000003,37.966864325000074],[138.29859459700003,37.92243073100008],[138.28785241000003,37.90599192900004],[138.28785241000003,37.861029364000046],[138.27214603000004,37.842230536000045],[138.22388756600003,37.83417389500005],[138.21216881600003,37.81940338700008],[138.2226668630001,37.80223216400009],[138.26514733200008,37.79832591400009],[138.37037194100003,37.82876211100006],[138.48650149800005,37.89175039300005],[138.5004988940001,37.906683661000045],[138.51319420700008,37.94481028900009],[138.56592858200008,38.01634349200009],[138.5752059250001,38.03510163000004],[138.5752059250001,38.074286200000074],[138.54078209700003,38.07672760600008],[138.4946395190001,38.06696198100008],[138.45923912900003,38.06989166900007]]],[[[139.54851321700005,42.243353583000044],[139.509125196,42.225775458000044],[139.4521590500001,42.21531810100004],[139.43702233200008,42.20722077000005],[139.41431725400003,42.17251211100006],[139.41081790500004,42.153998114000046],[139.422699415,42.13149648600006],[139.43539472700002,42.062933661000045],[139.45533287900003,42.05158112200007],[139.49390709700003,42.07884349200003],[139.50896243600005,42.100816148000035],[139.52068118600005,42.17450592700004],[139.55396569100003,42.21157461100006],[139.56421959700003,42.231756903000075],[139.54851321700005,42.243353583000044]]],[[[145.3115340500001,43.59332916900007],[145.28695722700002,43.60101959800005],[145.20240319100003,43.61383698100008],[145.3320418630001,43.56541575700004],[145.28785241000003,43.54751211100006],[145.27320397200003,43.522365627000056],[145.2973738940001,43.522650458000044],[145.31202233200008,43.542181708000044],[145.3501082690001,43.54962799700007],[145.3603621750001,43.56476471600007],[145.3115340500001,43.59332916900007]]],[[[141.3271590500001,45.17234935100004],[141.26832116000003,45.22553131700005],[141.235199415,45.24127838700008],[141.20264733200008,45.24909088700008],[141.16716556100005,45.23997630400004],[141.1318465500001,45.210272528000075],[141.1455184250001,45.18976471600007],[141.13111412900003,45.17015208500004],[141.15007571700005,45.14712148600006],[141.18181399800005,45.126613674000055],[141.2353621750001,45.10248444200005],[141.26840254,45.10101959800005],[141.30445397200003,45.12018463700008],[141.33562259200005,45.15778229400007],[141.3271590500001,45.17234935100004]]],[[[141.0527449880001,45.450425523000035],[141.00660241000003,45.44041575700004],[140.98609459700003,45.46743398600006],[140.9668074880001,45.46344635600008],[141.0009871750001,45.419134833000044],[140.9941512380001,45.37103913000004],[141.01050866000003,45.327785549000055],[141.0356551440001,45.292181708000044],[141.04957116000003,45.29291413000004],[141.0590926440001,45.321966864000046],[141.073578321,45.424709377000056],[141.06902103000004,45.442694403000075],[141.0527449880001,45.450425523000035]]],[[[127.80982506600003,26.73541901200008],[127.76880944100003,26.734035549000055],[127.75855553500003,26.71287669500009],[127.78565514400009,26.70258209800005],[127.81120853000004,26.706976630000042],[127.80982506600003,26.73541901200008]]],[[[127.99537194100003,27.091742255000042],[127.98707116000003,27.078599351000037],[127.93572024800005,27.025091864000046],[127.95215905000009,27.01829661700009],[127.97575931100005,27.03974030200004],[127.99317467500009,27.06671784100007],[127.99537194100003,27.091742255000042]]],[[[130.220062696,30.471584377000056],[130.191016268,30.476172262000034],[130.16505068900005,30.489890078000087],[130.14176604600004,30.489301081000065],[130.17311500100004,30.454421868000054],[130.2124676420001,30.420690741000044],[130.23184544100002,30.42354163300007],[130.26214412700006,30.447032385000057],[130.242867,30.46361180400004],[130.220062696,30.471584377000056]]],[[[134.36736087300005,34.55426666900007],[134.27125084700003,34.540676174000055],[134.22828209700003,34.51837799700007],[134.20378665500004,34.51243724200009],[134.17505944100003,34.52008698100008],[134.17017662900003,34.46548086100006],[134.22339928500003,34.472357489000046],[134.24870853000004,34.453192450000074],[134.24439537900003,34.41771067900004],[134.26238040500004,34.42088450700004],[134.29908287900003,34.464911200000074],[134.320648634,34.46893952000005],[134.32496178500003,34.45368073100008],[134.34693444100003,34.43081289300005],[134.36890709700003,34.484361070000034],[134.37427819100003,34.513332424000055],[134.3632918630001,34.545355536000045],[134.36736087300005,34.55426666900007]]],[[[141.287445509,25.417710679000038],[141.29948978000004,25.434800523000035],[141.28825931100005,25.45254140800006],[141.274750196,25.451117255000042],[141.27466881600003,25.427069403000075],[141.287445509,25.417710679000038]]],[[[131.28972415500004,25.95136139500005],[131.29086347700002,25.938666083000044],[131.3095809250001,25.922756252000056],[131.32789147200003,25.93707916900007],[131.3212996750001,25.947699286000045],[131.28972415500004,25.95136139500005]]],[[[127.22217858200008,26.598944403000075],[127.20883222700002,26.581976630000042],[127.23682701900009,26.57916901200008],[127.24236087300005,26.59674713700008],[127.22217858200008,26.598944403000075]]],[[[128.45435631600003,27.04751211100006],[128.42896569100003,27.066473700000074],[128.39625084700003,27.040757554000038],[128.43140709700003,27.02167389500005],[128.448578321,27.020697333000044],[128.45435631600003,27.04751211100006]]],[[[129.943695509,29.965277411000045],[129.9219895450001,30.00472988100006],[129.899750196,29.97565338700008],[129.9133333310001,29.960901639000042],[129.94092858200008,29.953192450000074],[129.943695509,29.965277411000045]]],[[[129.9462996750001,30.84471263200004],[129.9321395190001,30.85175202000005],[129.90398196700005,30.834418036000045],[129.91195722700002,30.81976959800005],[129.9394637380001,30.813421942000048],[129.95980879,30.828680731000077],[129.9462996750001,30.84471263200004]]],[[[139.62281334700003,33.88556549700007],[139.59245853000004,33.895819403000075],[139.57976321700005,33.88031647300005],[139.5942488940001,33.854722398000035],[139.62289472700002,33.856756903000075],[139.62281334700003,33.88556549700007]]],[[[139.37289472700002,41.52216217700004],[139.3403426440001,41.52383047100005],[139.34555097700002,41.493353583000044],[139.37427819100003,41.504339911000045],[139.37289472700002,41.52216217700004]]],[[[125.20289147200003,24.846258856000077],[125.16358483200008,24.854641018000052],[125.15756269600001,24.833807684000078],[125.14112389400009,24.828599351000037],[125.15064537900003,24.804348049000055],[125.16814212300005,24.80097077000005],[125.20525149800005,24.80499909100007],[125.21705162900003,24.81659577000005],[125.20289147200003,24.846258856000077]]],[[[127.36963951900009,26.214585679000038],[127.35417728000004,26.21893952000005],[127.34262129000001,26.18573639500005],[127.34538821700005,26.158270575000074],[127.36109459700003,26.164007880000042],[127.36963951900009,26.214585679000038]]],[[[129.93336022200003,31.86782461100006],[129.913422071,31.867743231000077],[129.90202884200005,31.848130601000037],[129.86451256600003,31.875555731000077],[129.84498131600003,31.880601304000038],[129.82309004,31.863023179000038],[129.83952884200005,31.853664455000057],[129.85906009200005,31.82367584800005],[129.8759871750001,31.81126536700009],[129.922211134,31.81981028900009],[129.93148847700002,31.83348216400009],[129.93336022200003,31.86782461100006]]],[[[130.24390709700003,32.30182526200008],[130.21705162900003,32.28693268400008],[130.21070397200003,32.25535716400009],[130.24284915500004,32.26308828300006],[130.266449415,32.28705475500004],[130.24390709700003,32.30182526200008]]],[[[130.44678795700008,32.59682851800005],[130.45289147200003,32.610988674000055],[130.4365340500001,32.62067291900007],[130.41358483200008,32.61359284100007],[130.398773634,32.59316640800006],[130.41138756600003,32.573879299000055],[130.4091903000001,32.549261786000045],[130.4458113940001,32.55255768400008],[130.45264733200008,32.59064362200007],[130.44678795700008,32.59682851800005]]],[[[132.56202233200008,32.74213288000004],[132.539886915,32.73379140800006],[132.54322350400003,32.70844147300005],[132.5708113940001,32.71727122600004],[132.56202233200008,32.74213288000004]]],[[[128.899750196,32.81830475500004],[128.8681746750001,32.83783600500004],[128.837250196,32.83380768400008],[128.83521569100003,32.78766510600008],[128.86646569100003,32.764960028000075],[128.88233483200008,32.765285549000055],[128.9072371750001,32.79401276200008],[128.899750196,32.81830475500004]]],[[[128.9531356130001,32.874212958000044],[128.93767337300005,32.86078522300005],[128.89087975400003,32.87091705900008],[128.89576256600003,32.85643138200004],[128.92400149800005,32.82965729400007],[128.94459069100003,32.82339101800005],[128.9580184250001,32.841498114000046],[128.9531356130001,32.874212958000044]]],[[[129.0278426440001,32.89980703300006],[129.02068118600005,32.91669342700004],[128.99244225400003,32.92942942900004],[128.983734571,32.90664297100005],[128.96461022200003,32.89826080900008],[128.96802819100003,32.87368398600006],[129.028168165,32.85565827000005],[129.0278426440001,32.89980703300006]]],[[[129.13550866000003,33.29987213700008],[129.08912194100003,33.283270575000074],[129.0761824880001,33.268784898000035],[129.10320071700005,33.24917226800005],[129.127289259,33.24750397300005],[129.14958743600005,33.26902903900009],[129.13550866000003,33.29987213700008]]],[[[129.55591881600003,33.49396393400008],[129.54175866000003,33.50731028900009],[129.50668379,33.48309967700004],[129.55445397200003,33.47345612200007],[129.55591881600003,33.49396393400008]]],[[[132.64136803500003,34.01406484600005],[132.59636478000004,33.98094310100004],[132.5869246750001,33.96967194200005],[132.60043379,33.94989655200004],[132.63445071700005,33.964829820000034],[132.64429772200003,34.000881252000056],[132.64136803500003,34.01406484600005]]],[[[133.09351647200003,34.17430247600004],[133.05543053500003,34.19354889500005],[133.03305097700002,34.18939850500004],[133.02621504,34.17617422100005],[133.03679446700005,34.15326569200005],[133.0200301440001,34.149644273000035],[133.01710045700008,34.118841864000046],[133.04517662900003,34.118394273000035],[133.09441165500004,34.16038646000004],[133.09351647200003,34.17430247600004]]],[[[132.73853600400003,34.196966864000046],[132.7089949880001,34.20331452000005],[132.68897545700008,34.189886786000045],[132.7236434250001,34.170803127000056],[132.75611412900003,34.16746653900009],[132.75505618600005,34.189113674000055],[132.73853600400003,34.196966864000046]]],[[[132.92115319100003,34.277329820000034],[132.9082137380001,34.262884833000044],[132.84644616000003,34.240220445000034],[132.849945509,34.21515534100007],[132.86312910200002,34.20848216400009],[132.90577233200008,34.21308014500005],[132.91675866000003,34.221869208000044],[132.93230228000004,34.27484772300005],[132.92115319100003,34.277329820000034]]],[[[132.3383895190001,34.30442942900004],[132.2988387380001,34.292222398000035],[132.26726321700005,34.253607489000046],[132.2793074880001,34.238348700000074],[132.2959090500001,34.24323151200008],[132.34498131600003,34.28196849200009],[132.3383895190001,34.30442942900004]]],[[[133.715668165,34.396144924000055],[133.6919051440001,34.37840403900009],[133.6884871750001,34.36615631700005],[133.7031356130001,34.35175202000005],[133.72584069100003,34.37140534100007],[133.715668165,34.396144924000055]]],[[[135.20517916100005,34.43049087500009],[135.22441622000008,34.41630428000008],[135.2570542850001,34.435778061000065],[135.24097098900006,34.45464659500004],[135.20517916100005,34.43049087500009]]],[[[134.04900149800005,34.47435130400004],[134.0839949880001,34.459824937000064],[134.102875196,34.48094310100004],[134.083425326,34.50071849200009],[134.03834069100003,34.483303127000056],[134.04900149800005,34.47435130400004]]],[[[136.79805969000006,34.87640064000004],[136.8077305920001,34.839196455000035],[136.82595147000006,34.84907610000005],[136.810968999,34.876305527000056],[136.79805969000006,34.87640064000004]]],[[[137.04810631600003,37.15228913000004],[137.02719160200002,37.15521881700005],[136.9990340500001,37.13947174700007],[136.9736434250001,37.15326569200005],[136.92839603000004,37.13776276200008],[136.91553795700008,37.12775299700007],[136.943695509,37.097357489000046],[136.9726668630001,37.101629950000074],[137.00375410200002,37.11347077000005],[137.03874759200005,37.12010325700004],[137.04810631600003,37.15228913000004]]]]}},
{"type":"Feature","properties":{"ISO_A2":"PM","ADMIN":"Saint Pierre and Miquelon","ISO_A3":"SPM","BORDER":3},"geometry":{"type":"MultiPolygon","coordinates":[[[[-56.150949673999946,46.79364655200004],[-56.161366339999915,46.81159088700008],[-56.19188391799992,46.806626695000034],[-56.240305141999954,46.765692450000074],[-56.221913214999915,46.75462474200003],[-56.168080206999946,46.75726959800005],[-56.150949673999946,46.79364655200004]]],[[[-56.27379309799994,46.89545319200005],[-56.311756964999915,46.90013255400004],[-56.31602942599994,46.91278717700004],[-56.313710089999915,46.97565338700008],[-56.32970130099994,46.992254950000074],[-56.301991339999915,46.996975002000056],[-56.28034420499995,46.98969147300005],[-56.25601152299993,47.018011786000045],[-56.25332597599993,47.05369700700004],[-56.304798956999946,47.08315664300005],[-56.35700436099995,47.09467194200005],[-56.37169348899994,47.11615631700005],[-56.39590410099993,47.11359284100007],[-56.38499915299991,47.03563060100004],[-56.336781378999945,46.95425039300005],[-56.32827714799993,46.914943752000056],[-56.34276282499991,46.87555573100008],[-56.39635169199994,46.836371161000045],[-56.375111456999946,46.81395091400009],[-56.37067623599995,46.78620026200008],[-56.346262173999946,46.78359609600005],[-56.28929602799991,46.813788153000075],[-56.249256964999915,46.84227122600004],[-56.23550370999993,46.87201569200005],[-56.27379309799994,46.89545319200005]]]]}},
{"type":"Feature","properties":{"ISO_A2":"IS","ADMIN":"Iceland","ISO_A3":"ISL","BORDER":2},"geometry":{"type":"MultiPolygon","coordinates":[[[[-14.563628709999932,66.38450755400004],[-14.75413977799991,66.37140534100007],[-14.83975175699993,66.39183177300004],[-14.925445115999935,66.38532135600008],[-14.991118943999936,66.36131419500003],[-15.025949673999946,66.32184479400007],[-15.096750454999949,66.30312734600005],[-15.108754035999937,66.27838776200008],[-15.179025844999956,66.26837799700007],[-15.302154100999928,66.26837799700007],[-15.337880011999914,66.25177643400008],[-15.329457160999937,66.21747467700004],[-15.340443488999938,66.18219635600008],[-15.371083136999914,66.16596100500004],[-15.419667120999918,66.17230866100005],[-15.575428839999915,66.23395416900007],[-15.610666469999956,66.22736237200007],[-15.69945227799991,66.23419830900008],[-15.69054114499994,66.26577383000006],[-15.775135870999918,66.28945547100005],[-15.721302863999938,66.31240469000005],[-15.67955481699994,66.34406159100007],[-15.706695115999935,66.39590078300006],[-15.741037563999953,66.40550364800004],[-15.836537238999938,66.40550364800004],[-15.84797115799995,66.42230866100005],[-15.92406165299991,66.42234935100004],[-15.94640051999994,66.44647858300004],[-15.933338995999918,66.47549062700006],[-15.891224738999938,66.49494049700007],[-15.938710089999915,66.50267161700003],[-16.02293860599991,66.53660716400003],[-16.080799933999913,66.52594635600008],[-16.13780676999994,66.53587474200003],[-16.235585089999915,66.51788971600007],[-16.26862545499995,66.48749420800004],[-16.312855597999942,66.48126862200007],[-16.36473548099991,66.51536692900004],[-16.434437628999945,66.50092194200005],[-16.429676886999914,66.51984284100007],[-16.543324347999942,66.50698476800005],[-16.572987433999913,66.48102448100008],[-16.544667120999918,66.46759674700007],[-16.525502081999946,66.43573639500005],[-16.529286261999914,66.41917552300004],[-16.508168097999942,66.39931875200006],[-16.515370245999918,66.36733633000006],[-16.480824347999942,66.35028717700004],[-16.476185675999943,66.32733795800004],[-16.421945766999954,66.28709544500003],[-16.421986456999946,66.26239655200004],[-16.439930792999917,66.23419830900008],[-16.472767706999946,66.21767812700006],[-16.487049933999913,66.18952057500007],[-16.667713995999918,66.15851471600007],[-16.744699673999946,66.11444733300004],[-16.782541469999956,66.13117096600007],[-16.75812740799995,66.14960358300004],[-16.884103969999956,66.13532135600008],[-16.919748501999948,66.12433502800008],[-16.99242102799991,66.19953034100007],[-17.01866614499994,66.20693594000005],[-17.119252081999946,66.21112702000005],[-17.217355923999946,66.19424062700006],[-17.269886847999942,66.16152578300006],[-17.261463995999918,66.12970612200007],[-17.326771613999938,66.11033763200004],[-17.36465410099993,66.08344147300005],[-17.36465410099993,66.04926178600005],[-17.39199785099993,66.02944570500006],[-17.395130988999938,66.00608958500004],[-17.41934160099993,65.99404531500005],[-17.611154751999948,65.99404531500005],[-17.58690344999991,65.98578522300005],[-17.545643683999913,65.93113841400003],[-17.564076300999943,65.92511627800008],[-17.598052537999934,65.96670156500005],[-17.64085852799991,65.99721914300005],[-17.67296301999994,66.00706614800004],[-17.700510219999956,66.04926178600005],[-17.752349412999934,66.08539459800005],[-17.833973761999914,66.11749909100007],[-17.87441972599993,66.11522044500003],[-17.91274980399993,66.15228913000004],[-17.955433722999942,66.16014232000003],[-18.054066535999937,66.14956289300005],[-18.099720831999946,66.16771067900004],[-18.156239386999914,66.16771067900004],[-18.193959113999938,66.17963288000004],[-18.296986456999946,66.17963288000004],[-18.323841925999943,66.15078359600005],[-18.32921301999994,66.10394928600005],[-18.31672115799995,66.05735911700003],[-18.289540167999917,66.02944570500006],[-18.296986456999946,66.01508209800005],[-18.264068162999934,65.98484935100004],[-18.235218878999945,65.97699616100005],[-18.218739386999914,65.95355866100005],[-18.228138800999943,65.93939850500004],[-18.18700110599991,65.91583893400008],[-18.151519334999932,65.92479075700004],[-18.097767706999946,65.87791575700004],[-18.067616339999915,65.83055247600004],[-18.070423956999946,65.79604726800005],[-18.097767706999946,65.78237539300005],[-18.097767706999946,65.74823639500005],[-18.109486456999946,65.73159414300005],[-18.070423956999946,65.70600006700005],[-18.05101477799991,65.65766022300005],[-18.079904751999948,65.64760976800005],[-18.096913214999915,65.67963288000004],[-18.091542120999918,65.69301992400005],[-18.138742641999954,65.71344635600008],[-18.179839647999927,65.74042389500005],[-18.179676886999914,65.78237539300005],[-18.203684048999946,65.80792877800008],[-18.208159959999932,65.85529205900008],[-18.224964972999942,65.87140534100007],[-18.260324673999946,65.87238190300008],[-18.28734290299991,65.91510651200008],[-18.348011847999942,65.95018138200004],[-18.422230597999942,65.95465729400007],[-18.46833248599995,65.96670156500005],[-18.54002844999991,65.97418854400007],[-18.516102667999917,66.02944570500006],[-18.544545050999943,66.08274974200003],[-18.59715735599991,66.10455963700008],[-18.659535285999937,66.09023672100005],[-18.619618292999917,66.13149648600006],[-18.692250128999945,66.16998932500007],[-18.721547003999945,66.17275625200006],[-18.793568488999938,66.14207591400003],[-18.801503058999913,66.15664297100005],[-18.77684485599991,66.19330475500004],[-18.827015753999945,66.20343659100007],[-18.864898240999935,66.18577708500004],[-18.944691535999937,66.19065989800004],[-19.034047003999945,66.17719147300005],[-19.07843990799995,66.15851471600007],[-19.105132615999935,66.11131419500003],[-19.07966061099995,66.08100006700005],[-19.15973873599995,66.09764232000003],[-19.182484503999945,66.08295319200005],[-19.229318813999953,66.09764232000003],[-19.36270097599993,66.08836497600004],[-19.41982988199993,66.07583242400005],[-19.451893683999913,66.05597565300008],[-19.442209438999953,66.03131745000007],[-19.41364498599995,66.00763580900008],[-19.44749915299991,65.98777903900003],[-19.49791419199994,65.97752513200004],[-19.51040605399993,65.95302969000005],[-19.451527472999942,65.92572663000004],[-19.40583248599995,65.86017487200007],[-19.39981035099993,65.81150950700004],[-19.418039516999954,65.75385163000004],[-19.457915818999936,65.74530670800004],[-19.544300910999937,65.76284414300005],[-19.577748175999943,65.74640534100007],[-19.62877356699994,65.75275299700007],[-19.650380011999914,65.76496002800008],[-19.672759568999936,65.82322825700004],[-19.70840410099993,65.87107982000003],[-19.74478105399993,65.88279857000003],[-19.879790818999936,65.90521881700005],[-19.87759355399993,65.92357005400004],[-19.941232876999948,65.96051666900007],[-19.954823370999918,66.00145091400003],[-19.996937628999945,66.03440989800004],[-20.030588344999956,66.08344147300005],[-20.07363847599993,66.09735748900005],[-20.113433397999927,66.12287018400008],[-20.184803839999915,66.13117096600007],[-20.255930141999954,66.10390859600005],[-20.304351365999935,66.10390859600005],[-20.342152472999942,66.09023672100005],[-20.40770423099991,66.09076569200005],[-20.427845831999946,66.08344147300005],[-20.41950436099995,66.05438873900005],[-20.448882615999935,66.02944570500006],[-20.41698157499991,66.01040273600006],[-20.39989173099991,65.98094310100004],[-20.393422003999945,65.94651927300004],[-20.32258053299995,65.86945221600007],[-20.312489386999914,65.84039948100008],[-20.339100714999915,65.83014557500007],[-20.290679490999935,65.79364655200004],[-20.263295050999943,65.74453359600005],[-20.271595831999946,65.71401601800005],[-20.307850714999915,65.69318268400008],[-20.309193488999938,65.66266510600008],[-20.328521287999934,65.62783437700006],[-20.376128709999932,65.59642161700003],[-20.410715298999946,65.59674713700008],[-20.500396287999934,65.56818268400008],[-20.45466061099995,65.56659577000005],[-20.427845831999946,65.54897695500006],[-20.45343990799995,65.50145091400003],[-20.500884568999936,65.49494049700007],[-20.53502356699994,65.50739166900007],[-20.57518469999991,65.53571198100008],[-20.576771613999938,65.57664622600004],[-20.64171301999994,65.60480377800008],[-20.63385982999995,65.66502513200004],[-20.665760870999918,65.69086334800005],[-20.74372311099995,65.67251211100006],[-20.805246548999946,65.63837311400005],[-20.918080206999946,65.60138580900008],[-20.948597785999937,65.57689036700003],[-20.962228969999956,65.54148997600004],[-20.98729407499991,65.50678131700005],[-20.975005662999934,65.43756745000007],[-20.933705206999946,65.37400950700004],[-20.90762285099993,65.35101959800005],[-20.962961391999954,65.35333893400008],[-21.003244594999956,65.39191315300008],[-21.055246548999946,65.45233795800004],[-21.098866339999915,65.44546133000006],[-21.07408606699994,65.33803945500006],[-21.078968878999945,65.30878327000005],[-21.119943813999953,65.26776764500005],[-21.085682745999918,65.23362864800004],[-21.10765540299991,65.21552155200004],[-21.147816535999937,65.24115631700005],[-21.19286048099991,65.31012604400007],[-21.18895423099991,65.35553620000007],[-21.19517981699994,65.40810781500005],[-21.212147589999915,65.43817780200004],[-21.27843176999994,65.47329336100006],[-21.31541907499991,65.48069896000004],[-21.375884568999936,65.46287669500003],[-21.369862433999913,65.48712799700007],[-21.31981360599991,65.52236562700006],[-21.29869544199994,65.55890534100007],[-21.320912238999938,65.60187409100007],[-21.37336178299995,65.59690989800004],[-21.43455969999991,65.57379791900007],[-21.41120357999995,65.61269765800006],[-21.41429602799991,65.63715241100005],[-21.442982550999943,65.64337799700007],[-21.609934048999946,65.64154694200005],[-21.655873175999943,65.66266510600008],[-21.689076300999943,65.69301992400005],[-21.668527798999946,65.70766836100006],[-21.672230597999942,65.72919342700004],[-21.695301886999914,65.74823639500005],[-21.758412238999938,65.76740143400008],[-21.71670488199993,65.77643463700008],[-21.66112219999991,65.75507233300004],[-21.647206183999913,65.73261139500005],[-21.57921301999994,65.69301992400005],[-21.443511522999927,65.69049713700008],[-21.414133266999954,65.69985586100006],[-21.332142706999946,65.74079010600008],[-21.41787675699993,65.76349518400008],[-21.327788865999935,65.80902741100005],[-21.278187628999945,65.85687897300005],[-21.325917120999918,65.87791575700004],[-21.284047003999945,65.89862702000005],[-21.30174719999991,65.93598053600005],[-21.328114386999914,65.94525788000004],[-21.392689581999946,65.95205312700006],[-21.41002356699994,65.96747467700004],[-21.50377356699994,65.96051666900007],[-21.56012936099995,65.94794342700004],[-21.591623501999948,65.96039459800005],[-21.486073370999918,65.97321198100008],[-21.45929928299995,65.98232656500005],[-21.318511522999927,65.98777903900003],[-21.34870357999995,66.02069733300004],[-21.40103105399993,66.02944570500006],[-21.509388800999943,66.01707591400003],[-21.55846106699994,66.04653554900005],[-21.521229620999918,66.06452057500007],[-21.548817511999914,66.09764232000003],[-21.59325110599991,66.09638092700004],[-21.653960740999935,66.07758209800005],[-21.702748175999943,66.07037995000007],[-21.723011847999942,66.08222077000005],[-21.73033606699994,66.14411041900007],[-21.74986731699994,66.18577708500004],[-21.812652147999927,66.19944896000004],[-21.84361731699994,66.19306061400005],[-21.920725063999953,66.21967194200005],[-21.92178300699993,66.24103424700007],[-21.979318813999953,66.27496979400007],[-22.128285285999937,66.26829661700003],[-22.155181443999936,66.28261953300006],[-22.214100714999915,66.28969961100006],[-22.243397589999915,66.35712311400005],[-22.26862545499995,66.37592194200005],[-22.350005662999934,66.39419179900005],[-22.376698370999918,66.43256256700005],[-22.426625128999945,66.42792389500005],[-22.51073157499991,66.44041575700004],[-22.57453365799995,66.46588776200008],[-22.67023678299995,66.42096588700008],[-22.695871548999946,66.41917552300004],[-22.75218665299991,66.44269440300008],[-22.797271287999934,66.44969310100004],[-22.850331183999913,66.46698639500005],[-22.95030676999994,66.46698639500005],[-22.86815344999991,66.44139232000003],[-22.895090298999946,66.42601146000004],[-22.922718878999945,66.44765859600005],[-23.03229732999995,66.44025299700007],[-23.041900193999936,66.42560455900008],[-23.104725714999915,66.44281647300005],[-23.135324673999946,66.43280670800004],[-23.08462480399993,66.40326569200005],[-23.02481035099993,66.39183177300004],[-23.04674231699994,66.36066315300008],[-23.089507615999935,66.35809967700004],[-23.176258917999917,66.36456940300008],[-23.182850714999915,66.34878164300005],[-23.13931230399993,66.32778554900005],[-23.098052537999934,66.31830475500004],[-22.93968665299991,66.30145905200004],[-22.833729620999918,66.35883209800005],[-22.823109503999945,66.31679922100005],[-22.763050910999937,66.32367584800005],[-22.683949347999942,66.35830312700006],[-22.682118292999917,66.32363515800006],[-22.71007239499994,66.29564036700003],[-22.654855923999946,66.28945547100005],[-22.595692511999914,66.29173411700003],[-22.544911261999914,66.30312734600005],[-22.544911261999914,66.27521393400008],[-22.50340735599991,66.26642487200007],[-22.484486456999946,66.25116608300004],[-22.55923417899993,66.24848053600005],[-22.538726365999935,66.22736237200007],[-22.566517706999946,66.22284577000005],[-22.60655676999994,66.24677155200004],[-22.782460089999915,66.26276276200008],[-22.930897589999915,66.24640534100007],[-22.97760982999995,66.23419830900008],[-22.93586178299995,66.18976471600007],[-22.865956183999913,66.15949127800008],[-22.628163214999915,66.11749909100007],[-22.57217363199993,66.09369538000004],[-22.488392706999946,66.07672760600008],[-22.401722785999937,66.09731679900005],[-22.39281165299991,66.08405182500007],[-22.449370897999927,66.07591380400004],[-22.484201626999948,66.02944570500006],[-22.44204667899993,66.01544830900008],[-22.40843665299991,65.98777903900003],[-22.381255662999934,65.92877838700008],[-22.360707160999937,65.91209544500003],[-22.392648891999954,65.90167877800008],[-22.431630011999914,65.84589264500005],[-22.460275844999956,65.85431549700007],[-22.428863084999932,65.88475169500003],[-22.422027147999927,65.91889069200005],[-22.476714647999927,65.93939850500004],[-22.517323370999918,65.97577545800004],[-22.538726365999935,65.97418854400007],[-22.545318162999934,65.94696686400005],[-22.611683722999942,65.87384674700007],[-22.635161912999934,65.83527252800008],[-22.655425584999932,65.84218984600005],[-22.593413865999935,65.91958242400005],[-22.577381964999915,65.97308991100005],[-22.620269334999932,66.00922272300005],[-22.65851803299995,66.02586497600004],[-22.68272864499994,66.05585358300004],[-22.750477667999917,66.05841705900008],[-22.809152798999946,65.97760651200008],[-22.855539516999954,66.02850983300004],[-22.895090298999946,66.01508209800005],[-22.917225714999915,66.04612864800004],[-22.97687740799995,66.03367747600004],[-22.986073370999918,66.07953522300005],[-23.027699347999942,66.10871002800008],[-23.057972785999937,66.10761139500005],[-23.10728919199994,66.07591380400004],[-23.110218878999945,66.11253489800004],[-23.152699347999942,66.14581940300008],[-23.202504035999937,66.15509674700007],[-23.304351365999935,66.18577708500004],[-23.461415167999917,66.20416901200008],[-23.501291469999956,66.18048737200007],[-23.542713995999918,66.17963288000004],[-23.600819464999915,66.15228913000004],[-23.577056443999936,66.13768138200004],[-23.481027798999946,66.13861725500004],[-23.484120245999918,66.12759023600006],[-23.550160285999937,66.13198476800005],[-23.637318488999938,66.12848541900007],[-23.669667120999918,66.11749909100007],[-23.636057094999956,66.09821198100008],[-23.547027147999927,66.05951569200005],[-23.42251542899993,66.03587474200003],[-23.465199347999942,66.02098216400003],[-23.518299933999913,66.02667877800008],[-23.699574347999942,66.07172272300005],[-23.75137285099993,66.07656484600005],[-23.792591925999943,66.07037995000007],[-23.813221808999913,66.04222239800004],[-23.813099738999938,66.01137929900005],[-23.763295050999943,65.97992584800005],[-23.655100063999953,65.94257233300004],[-23.577870245999918,65.90501536700003],[-23.474273240999935,65.89484284100007],[-23.58189856699994,65.87693919500003],[-23.619211391999954,65.88401927300004],[-23.669667120999918,65.90521881700005],[-23.744130011999914,65.91209544500003],[-23.82054602799991,65.92572663000004],[-23.86314856699994,65.90582916900007],[-23.86660722599993,65.87555573100008],[-23.84520423099991,65.84296295800004],[-23.813099738999938,65.81586334800005],[-23.72915605399993,65.77435944200005],[-23.676503058999913,65.78237539300005],[-23.64171301999994,65.76870351800005],[-23.588775193999936,65.75975169500003],[-23.319243943999936,65.75702545800004],[-23.276275193999936,65.77313873900005],[-23.22402910099993,65.74823639500005],[-23.244536912999934,65.74038320500006],[-23.30968176999994,65.74823639500005],[-23.356190558999913,65.73623281500005],[-23.43781490799995,65.73163483300004],[-23.466908331999946,65.73969147300005],[-23.52916419199994,65.72772858300004],[-23.529408331999946,65.71149323100008],[-23.497792120999918,65.69301992400005],[-23.404123501999948,65.67519765800006],[-23.38853919199994,65.63837311400005],[-23.412668423999946,65.63523997600004],[-23.497792120999918,65.64520905200004],[-23.538807745999918,65.62128327000005],[-23.579701300999943,65.61790599200003],[-23.556385870999918,65.65468984600005],[-23.56663977799991,65.67251211100006],[-23.628081834999932,65.70600006700005],[-24.04987545499995,65.80353424700007],[-24.09284420499995,65.80475495000007],[-24.135161912999934,65.78912995000007],[-24.121571417999917,65.74079010600008],[-24.080555792999917,65.70600006700005],[-24.02285722599993,65.67617422100005],[-23.881214972999942,65.64809804900005],[-23.825917120999918,65.62592194200005],[-23.838002081999946,65.61444733300004],[-23.935943162999934,65.63837311400005],[-24.086781378999945,65.65135325700004],[-24.00999915299991,65.59943268400008],[-23.940785285999937,65.57249583500004],[-23.863636847999942,65.52301666900007],[-23.92296301999994,65.52850983300004],[-23.971302863999938,65.55866120000007],[-24.041167772999927,65.56647370000007],[-24.080555792999917,65.57689036700003],[-24.13540605399993,65.60614655200004],[-24.18065344999991,65.61237213700008],[-24.277943488999938,65.63641998900005],[-24.317534959999932,65.63613515800006],[-24.365793423999946,65.61790599200003],[-24.375396287999934,65.56940338700008],[-24.451568162999934,65.54523346600007],[-24.473784959999932,65.52822500200006],[-24.539906378999945,65.50800202000005],[-24.518299933999913,65.49135976800005],[-24.481760219999956,65.48847077000005],[-24.19668535099993,65.50116608300004],[-24.043080206999946,65.47455475500004],[-23.98619544199994,65.47418854400007],[-23.96776282499991,65.46491120000007],[-23.984364386999914,65.41864655200004],[-23.943837042999917,65.40692780200004],[-23.89753170499995,65.40460846600007],[-23.714100714999915,65.42609284100007],[-23.676503058999913,65.45335521000004],[-23.625396287999934,65.46645742400005],[-23.490345831999946,65.47793203300006],[-23.39590410099993,65.51447174700007],[-23.373036261999914,65.49819570500006],[-23.31403561099995,65.48871491100005],[-23.247425910999937,65.48541901200008],[-23.182443813999953,65.51422760600008],[-23.201324022999927,65.53473541900007],[-23.18301347599993,65.57636139500005],[-23.128651495999918,65.54975006700005],[-23.073475714999915,65.53839752800008],[-22.985218878999945,65.55121491100005],[-22.925770636999914,65.57318756700005],[-22.87995357999995,65.57648346600007],[-22.85960852799991,65.59560781500005],[-22.818714972999942,65.58454010600008],[-22.887603318999936,65.54897695500006],[-22.842274542999917,65.53082916900007],[-22.784413214999915,65.52928294500003],[-22.785227016999954,65.50800202000005],[-22.717518683999913,65.50800202000005],[-22.764027472999942,65.62409088700008],[-22.736805792999917,65.63153717700004],[-22.682118292999917,65.62409088700008],[-22.704741990999935,65.60529205900008],[-22.707630988999938,65.57221100500004],[-22.669097459999932,65.52850983300004],[-22.645334438999953,65.53188711100006],[-22.62295488199993,65.56415436400005],[-22.589182094999956,65.53558991100005],[-22.546538865999935,65.56073639500005],[-22.55695553299995,65.59788646000004],[-22.533924933999913,65.62177155200004],[-22.50145423099991,65.56964752800008],[-22.504017706999946,65.52850983300004],[-22.478627081999946,65.50800202000005],[-22.42341061099995,65.51732005400004],[-22.40371660099993,65.53384023600006],[-22.353260870999918,65.54148997600004],[-22.312489386999914,65.56199778900003],[-22.291737433999913,65.55516185100004],[-22.346994594999956,65.53534577000005],[-22.27643795499995,65.53070709800005],[-22.209828253999945,65.54897695500006],[-22.14834550699993,65.57689036700003],[-22.15371660099993,65.54588450700004],[-22.235096808999913,65.52480703300006],[-22.345814581999946,65.51471588700008],[-22.387196417999917,65.49168528900003],[-22.28066972599993,65.45132070500006],[-22.25381425699993,65.43366120000007],[-22.199208136999914,65.43231842700004],[-22.171620245999918,65.45962148600006],[-22.120269334999932,65.48509349200003],[-22.101185675999943,65.52045319200005],[-22.07323157499991,65.48749420800004],[-22.045318162999934,65.50116608300004],[-22.003163214999915,65.50389232000003],[-21.93545488199993,65.46702708500004],[-21.97642981699994,65.43976471600007],[-21.89757239499994,65.44651927300004],[-21.826242641999954,65.43976471600007],[-21.975087042999917,65.39740631700005],[-22.00682532499991,65.38125234600005],[-22.070871548999946,65.36737702000005],[-22.10700436099995,65.34833405200004],[-22.201283331999946,65.31769440300008],[-22.312855597999942,65.30434804900005],[-22.339507615999935,65.29572174700007],[-22.37804114499994,65.26776764500005],[-22.428863084999932,65.26776764500005],[-22.426177537999934,65.25145091400003],[-22.490345831999946,65.23362864800004],[-22.524037238999938,65.21499258000006],[-22.55768795499995,65.16893138200004],[-22.511870897999927,65.15420156500005],[-22.390736456999946,65.16327545800004],[-22.318959113999938,65.13841380400004],[-22.03384355399993,65.11493561400005],[-21.98387610599991,65.12376536700003],[-21.83307857999995,65.17841217700004],[-21.84048417899993,65.19269440300008],[-21.809478318999936,65.20612213700008],[-21.766102667999917,65.20795319200005],[-21.734120245999918,65.19379303600005],[-21.73741614499994,65.15916575700004],[-21.79210364499994,65.09650299700007],[-21.83657792899993,65.08242422100005],[-21.799672003999945,65.04462311400005],[-21.826242641999954,65.03506094000005],[-21.982533331999946,65.02753327000005],[-22.045318162999934,65.04189687700006],[-22.120432094999956,65.04189687700006],[-22.155181443999936,65.02757396000004],[-22.244740363999938,65.04726797100005],[-22.37999426999994,65.05878327000005],[-22.46190344999991,65.05048248900005],[-22.545399542999917,65.05219147300005],[-22.566070115999935,65.03506094000005],[-22.628163214999915,65.01459381700005],[-22.674427863999938,65.00755442900004],[-22.695871548999946,65.06232330900008],[-22.757069464999915,65.07192617400005],[-22.831857876999948,65.03912995000007],[-22.785227016999954,65.02757396000004],[-22.84471594999991,65.00991445500006],[-22.906076626999948,65.00348541900007],[-22.94286048099991,64.98663971600007],[-22.992787238999938,65.01422760600008],[-23.02139238199993,65.01300690300008],[-23.059925910999937,64.97858307500007],[-23.09984290299991,64.96678294500003],[-23.114003058999913,64.94611237200007],[-23.162587042999917,64.95311107000003],[-23.127797003999945,64.97296784100007],[-23.086822068999936,65.01459381700005],[-23.168568488999938,65.02008698100008],[-23.210194464999915,65.01691315300008],[-23.240589972999942,64.98688385600008],[-23.22402910099993,64.94940827000005],[-23.245025193999936,64.93012116100005],[-23.30223548099991,64.92951080900008],[-23.306019660999937,64.95311107000003],[-23.33975175699993,64.96263255400004],[-23.395334438999953,64.96678294500003],[-23.38109290299991,64.94562409100007],[-23.47175045499995,64.95237864800004],[-23.512847459999932,64.94635651200008],[-23.559437628999945,64.91738515800006],[-23.600819464999915,64.91836172100005],[-23.638010219999956,64.89850495000007],[-23.748117641999954,64.90692780200004],[-23.828521287999934,64.92951080900008],[-23.849761522999927,64.93056875200006],[-23.95840410099993,64.91730377800008],[-24.014271613999938,64.89337799700007],[-24.06004798099991,64.89105866100005],[-24.032460089999915,64.84853750200006],[-23.998646613999938,64.81590403900003],[-23.955881313999953,64.78668854400007],[-23.946115688999953,64.76089101800005],[-23.905873175999943,64.74396393400008],[-23.83356686099995,64.72589752800008],[-23.747873501999948,64.74022044500003],[-23.673939581999946,64.74111562700006],[-23.646595831999946,64.75088125200006],[-23.623524542999917,64.79425690300008],[-23.593495245999918,64.81098053600005],[-23.457142706999946,64.79364655200004],[-23.416493292999917,64.80638255400004],[-23.398426886999914,64.82273997600004],[-23.301665818999936,64.83222077000005],[-23.246245897999927,64.83047109600005],[-23.188099738999938,64.80414459800005],[-23.05036373599995,64.79979075700004],[-22.93114173099991,64.79979075700004],[-22.783924933999913,64.79271067900004],[-22.736887173999946,64.77480703300006],[-22.71007239499994,64.78119538000004],[-22.77839107999995,64.80231354400007],[-22.67251542899993,64.80231354400007],[-22.638050910999937,64.77777741100005],[-22.593413865999935,64.78119538000004],[-22.53189042899993,64.80914948100008],[-22.476714647999927,64.80231354400007],[-22.457020636999914,64.81533437700006],[-22.40843665299991,64.82273997600004],[-22.442534959999932,64.78119538000004],[-22.436390753999945,64.76752350500004],[-22.37214107999995,64.76874420800004],[-22.319650844999956,64.75385163000004],[-22.349232550999943,64.72308991100005],[-22.409047003999945,64.68964264500005],[-22.34316972599993,64.69920482000003],[-22.319650844999956,64.67877838700008],[-22.35460364499994,64.66278717700004],[-22.36510169199994,64.64264557500007],[-22.391021287999934,64.63784414300005],[-22.346994594999956,64.61050039300005],[-22.36119544199994,64.60024648600006],[-22.357533331999946,64.56801992400005],[-22.339507615999935,64.55585358300004],[-22.28937740799995,64.56085846600007],[-22.257639126999948,64.55585358300004],[-22.227040167999917,64.53034088700008],[-22.223500128999945,64.50063711100006],[-22.199045376999948,64.47186920800004],[-22.164784308999913,64.45966217700004],[-22.129058397999927,64.47288646000004],[-22.093088344999956,64.47272370000007],[-22.079457160999937,64.50063711100006],[-22.051584438999953,64.52456289300005],[-21.99327551999994,64.52924225500004],[-21.91120357999995,64.56321849200003],[-21.844105597999942,64.58148834800005],[-21.79210364499994,64.59003327000005],[-21.72842363199993,64.61570872600004],[-21.721831834999932,64.63882070500006],[-21.67731686099995,64.63251373900005],[-21.613352016999954,64.65827057500007],[-21.589955206999946,64.64960358300004],[-21.682240363999938,64.62417226800005],[-21.682240363999938,64.61664459800005],[-21.57921301999994,64.61664459800005],[-21.605580206999946,64.58934153900003],[-21.65143795499995,64.59251536700003],[-21.64745032499991,64.61050039300005],[-21.702748175999943,64.61050039300005],[-21.762277798999946,64.59210846600007],[-21.837269660999937,64.53900788000004],[-21.949818488999938,64.51390208500004],[-21.975087042999917,64.50372955900008],[-21.99986731699994,64.45392487200007],[-22.021473761999914,64.45600006700005],[-22.03189042899993,64.41445547100005],[-22.045318162999934,64.40509674700007],[-22.011341925999943,64.38275788000004],[-21.948882615999935,64.39496491100005],[-21.92324785099993,64.37889232000003],[-21.984771287999934,64.37026601800005],[-22.018055792999917,64.34979889500005],[-22.099964972999942,64.31500885600008],[-22.00999915299991,64.30011627800008],[-21.962635870999918,64.29730866100005],[-21.865834113999938,64.33417389500005],[-21.821156378999945,64.34007396000004],[-21.79210364499994,64.36408112200007],[-21.716542120999918,64.39386627800008],[-21.63117428299995,64.40265534100007],[-21.44945227799991,64.39765045800004],[-21.483021613999938,64.38397858300004],[-21.465402798999946,64.36180247600004],[-21.60309811099995,64.37775299700007],[-21.648548956999946,64.37148672100005],[-21.68114173099991,64.35663483300004],[-21.73741614499994,64.34979889500005],[-21.764027472999942,64.33930084800005],[-21.803212042999917,64.30174388200004],[-21.894520636999914,64.26788971600007],[-21.865712042999917,64.23956940300008],[-21.81786048099991,64.23798248900005],[-21.790760870999918,64.21841054900005],[-21.729725714999915,64.21881745000007],[-21.764149542999917,64.18842194200005],[-21.757476365999935,64.17108795800004],[-21.79824785099993,64.16486237200007],[-21.84666907499991,64.14435455900008],[-21.864857550999943,64.15631745000007],[-22.01512610599991,64.16828034100007],[-21.91502844999991,64.12392812700006],[-21.91502844999991,64.10956452000005],[-21.951771613999938,64.10423411700003],[-22.034901495999918,64.11273834800005],[-21.963368292999917,64.08226146000004],[-22.040679490999935,64.05109284100007],[-22.124175584999932,64.04389069200005],[-22.199208136999914,64.02081940300008],[-22.262074347999942,64.03335195500006],[-22.298573370999918,64.02081940300008],[-22.346058722999942,64.01727936400005],[-22.386382615999935,64.00210195500006],[-22.38117428299995,63.98672109600005],[-22.42088782499991,63.97724030200004],[-22.542795376999948,63.98273346600007],[-22.55846106699994,64.01943594000005],[-22.609974738999938,64.06159088700008],[-22.689605272999927,64.08974844000005],[-22.718739386999914,64.07050202000005],[-22.716460740999935,64.03229401200008],[-22.736805792999917,63.993475653000075],[-22.72996985599991,63.974676825000074],[-22.685902472999942,63.945746161000045],[-22.71507727799991,63.901271877000056],[-22.730580206999946,63.89109935100004],[-22.70018469999991,63.87417226800005],[-22.697417772999927,63.85187409100007],[-22.717518683999913,63.82843659100007],[-22.671254035999937,63.810980536000045],[-22.63267981699994,63.82611725500004],[-22.535878058999913,63.821193752000056],[-22.425160285999937,63.84951406500005],[-22.39439856699994,63.835923570000034],[-22.336415167999917,63.86318594000005],[-22.065785285999937,63.83588288000004],[-22.014393683999913,63.84320709800005],[-21.959462042999917,63.843736070000034],[-21.79320227799991,63.87055084800005],[-21.75999915299991,63.868394273000035],[-21.659901495999918,63.83185455900008],[-21.366932745999918,63.84955475500004],[-21.349761522999927,63.88418203300006],[-21.222035285999937,63.898871161000045],[-21.29869544199994,63.918402411000045],[-21.236439581999946,63.95335521000004],[-21.18504798099991,63.959418036000045],[-21.07412675699993,63.93427155200004],[-21.117014126999948,63.92987702000005],[-21.194935675999943,63.932074286000045],[-21.188791469999956,63.89109935100004],[-21.168324347999942,63.87555573100008],[-21.076730923999946,63.84845612200007],[-20.948841925999943,63.82721588700008],[-20.858631964999915,63.800441799000055],[-20.805287238999938,63.79474518400008],[-20.754505988999938,63.80565013200004],[-20.69831295499995,63.844183661000045],[-20.67178300699993,63.83763255400004],[-20.681792772999927,63.80731842700004],[-20.71580969999991,63.78750234600005],[-20.705230272999927,63.77440013200004],[-20.770415818999936,63.77440013200004],[-20.577504035999937,63.714178778000075],[-20.56240800699993,63.75682200700004],[-20.530181443999936,63.76434967700004],[-20.483021613999938,63.78750234600005],[-20.427845831999946,63.82221100500004],[-20.41201738199993,63.812567450000074],[-20.442128058999913,63.78750234600005],[-20.365793423999946,63.767564195000034],[-20.399647589999915,63.761053778000075],[-20.431263800999943,63.767564195000034],[-20.498524542999917,63.763006903000075],[-20.558216925999943,63.740301825000074],[-20.531605597999942,63.68891022300005],[-20.50108801999994,63.671128648000035],[-20.442128058999913,63.67137278900003],[-20.386545376999948,63.645697333000044],[-20.36709550699993,63.61981842700004],[-20.337717251999948,63.60171133000006],[-20.185699022999927,63.54441966400003],[-20.14830481699994,63.540350653000075],[-20.033802863999938,63.54238515800006],[-19.769886847999942,63.53424713700008],[-19.74795488199993,63.548895575000074],[-19.71157792899993,63.521551825000074],[-19.550933397999927,63.502020575000074],[-19.434152798999946,63.46653880400004],[-19.352162238999938,63.45913320500006],[-19.287261522999927,63.438666083000044],[-19.15265865799995,63.42572663000004],[-19.132435675999943,63.438666083000044],[-19.064116990999935,63.42438385600008],[-19.05565344999991,63.41437409100007],[-18.974761522999927,63.42438385600008],[-18.783029751999948,63.397650458000044],[-18.732045050999943,63.39671458500004],[-18.521595831999946,63.42251211100006],[-18.397694464999915,63.449204820000034],[-18.265695766999954,63.46653880400004],[-18.27676347599993,63.501166083000044],[-18.324330206999946,63.53424713700008],[-18.29637610599991,63.54645416900007],[-18.228138800999943,63.527411200000074],[-18.15062415299991,63.52631256700005],[-18.18736731699994,63.505194403000075],[-18.228138800999943,63.49323151200008],[-18.195383266999954,63.47280508000006],[-18.132923956999946,63.47931549700007],[-18.06737219999991,63.50153229400007],[-17.964955206999946,63.52252838700008],[-17.91905676999994,63.54515208500004],[-17.88304602799991,63.57611725500004],[-17.87246660099993,63.604396877000056],[-17.946929490999935,63.60248444200005],[-17.953114386999914,63.628607489000046],[-17.92642167899993,63.63666413000004],[-17.865834113999938,63.62384674700007],[-17.83975175699993,63.634100653000075],[-17.823882615999935,63.66742584800005],[-17.935902472999942,63.675767320000034],[-17.90453040299991,63.69122955900008],[-17.893055792999917,63.719956773000035],[-17.860259568999936,63.73700592700004],[-17.79674231699994,63.740301825000074],[-17.79002844999991,63.75340403900003],[-17.74046790299991,63.74599844000005],[-17.69098873599995,63.76927317900004],[-17.624826626999948,63.753973700000074],[-17.685292120999918,63.73969147300005],[-17.71117102799991,63.72736237200007],[-17.72101803299995,63.705511786000045],[-17.590646938999953,63.746527411000045],[-17.46776282499991,63.76072825700004],[-17.330555792999917,63.781236070000034],[-17.207102016999954,63.81537506700005],[-17.114857550999943,63.794907945000034],[-17.006214972999942,63.79783763200004],[-17.004750128999945,63.81708405200004],[-17.028920050999943,63.84955475500004],[-17.01854407499991,63.86200592700004],[-16.966908331999946,63.86937083500004],[-16.977040167999917,63.88882070500006],[-16.940297003999945,63.897772528000075],[-16.947010870999918,63.85639069200005],[-16.960682745999918,63.82843659100007],[-16.955637173999946,63.81012604400007],[-16.895822719999956,63.80776601800005],[-16.89549719999991,63.86277903900003],[-16.861073370999918,63.85879140800006],[-16.80296790299991,63.825384833000044],[-16.757639126999948,63.85028717700004],[-16.61119544199994,63.86937083500004],[-16.350493943999936,63.96556224200003],[-16.350493943999936,63.98672109600005],[-16.30890865799995,64.01707591400003],[-16.25487219999991,64.03510163000004],[-16.23932857999995,64.02350495000007],[-16.17926998599995,64.04193756700005],[-16.19286048099991,64.05499909100007],[-16.14362545499995,64.06354401200008],[-16.075998501999948,64.09393952000005],[-16.04478919199994,64.09979889500005],[-15.981190558999913,64.12392812700006],[-15.99205481699994,64.14447663000004],[-15.733021613999938,64.17926666900007],[-15.713612433999913,64.21662018400008],[-15.585519985999952,64.23875560100004],[-15.528065558999913,64.24115631700005],[-15.464019334999932,64.25128815300008],[-15.478016730999911,64.27513255400004],[-15.422271287999934,64.26850006700005],[-15.397816535999937,64.27411530200004],[-15.374012824999909,64.30589427300004],[-15.363636847999942,64.33681875200006],[-15.302154100999928,64.32245514500005],[-15.265777147999927,64.32054271000004],[-15.254505988999938,64.29804108300004],[-15.206613735999952,64.30141836100006],[-15.22647050699993,64.26788971600007],[-15.143950975999928,64.28148021000004],[-15.106678839999915,64.30141836100006],[-14.995472785999937,64.26260000200006],[-14.949045376999948,64.26044342700004],[-14.899973110999952,64.28164297100005],[-14.89126542899993,64.30882396000004],[-14.942209438999953,64.30467357000003],[-14.952748175999943,64.31924062700006],[-14.892079230999911,64.33466217700004],[-14.848947719999956,64.35712311400005],[-14.79360917899993,64.37254466400003],[-14.761789516999954,64.39496491100005],[-14.712513800999943,64.41412995000007],[-14.705230272999927,64.43329498900005],[-14.59406490799995,64.42560455900008],[-14.544911261999914,64.40509674700007],[-14.510487433999913,64.44232819200005],[-14.509348110999952,64.47190989800004],[-14.476673956999946,64.49689362200007],[-14.453236456999946,64.54633209800005],[-14.36156165299991,64.59446849200003],[-14.383615688999953,64.60073476800005],[-14.445383266999954,64.56069570500006],[-14.50999915299991,64.54901764500005],[-14.572092251999948,64.57697174700007],[-14.57217363199993,64.59686920800004],[-14.438832160999937,64.59686920800004],[-14.409250454999949,64.61025625200006],[-14.417713995999918,64.63100820500006],[-14.493478969999956,64.65827057500007],[-14.441965298999946,64.67572663000004],[-14.274322068999936,64.65143463700008],[-14.27017167899993,64.66950104400007],[-14.339466925999943,64.68561432500007],[-14.385243292999917,64.72085195500006],[-14.383615688999953,64.74022044500003],[-14.466135219999956,64.76129791900007],[-14.455799933999913,64.79022858300004],[-14.42056230399993,64.78961823100008],[-14.368072068999936,64.75531647300005],[-14.339466925999943,64.71629466400003],[-14.27061926999994,64.69928620000007],[-14.048451300999943,64.71971263200004],[-13.986317511999914,64.74022044500003],[-14.048451300999943,64.78864166900007],[-14.013661261999914,64.80231354400007],[-13.981922980999911,64.79677969000005],[-13.867787238999938,64.79474518400008],[-13.858021613999938,64.81683991100005],[-13.966420050999943,64.83584219000005],[-13.936594204999949,64.84707265800006],[-13.832102016999954,64.82957591400003],[-13.766916469999956,64.85382721600007],[-13.787668423999946,64.87738678600005],[-13.852284308999913,64.90029531500005],[-13.995513475999928,64.91449616100005],[-13.976063605999911,64.92519765800006],[-13.876454230999911,64.93260325700004],[-13.78892981699994,64.91437409100007],[-13.739898240999935,64.91120026200008],[-13.698312954999949,64.92519765800006],[-13.718169725999928,64.93960195500006],[-13.821278449999909,64.95994700700004],[-13.867909308999913,64.97821686400005],[-13.918039516999954,65.01459381700005],[-14.233387824999909,65.02757396000004],[-14.233387824999909,65.03506094000005],[-14.029774542999917,65.03839752800008],[-13.993763800999943,65.05548737200007],[-13.895415818999936,65.04706452000005],[-13.83031165299991,65.02960846600007],[-13.801991339999915,64.99408600500004],[-13.719471808999913,64.99408600500004],[-13.647368943999936,64.98663971600007],[-13.58267167899993,65.01203034100007],[-13.60960852799991,65.03506094000005],[-13.526966925999943,65.05548737200007],[-13.502919074999909,65.08128489800004],[-13.526966925999943,65.09650299700007],[-13.51781165299991,65.13629791900007],[-13.52017167899993,65.17218659100007],[-13.561105923999946,65.15176015800006],[-13.616607225999928,65.10700104400007],[-13.685292120999918,65.11701080900008],[-13.651193813999953,65.13743724200003],[-13.616607225999928,65.19676341400003],[-13.664173956999946,65.20014069200005],[-13.73306230399993,65.19269440300008],[-13.780873175999943,65.20636627800008],[-13.65420488199993,65.21995677300004],[-13.567982550999943,65.26097239800004],[-13.58226477799991,65.27692291900007],[-13.657378709999932,65.30255768400008],[-13.687652147999927,65.30638255400004],[-13.784250454999949,65.29572174700007],[-13.832508917999917,65.29759349200003],[-13.991118943999936,65.27269114800004],[-13.981027798999946,65.28912995000007],[-13.897531704999949,65.29572174700007],[-13.834339972999942,65.30963776200008],[-13.784535285999937,65.30980052300004],[-13.729847785999937,65.32709381700005],[-13.73306230399993,65.34349192900004],[-13.81086178299995,65.35504791900007],[-13.795806443999936,65.37311432500007],[-13.704741990999935,65.36733633000006],[-13.670969204999949,65.37763092700004],[-13.667632615999935,65.40570709800005],[-13.637196417999917,65.43475983300004],[-13.651193813999953,65.45962148600006],[-13.607980923999946,65.49213288000004],[-13.60960852799991,65.51422760600008],[-13.685292120999918,65.55516185100004],[-13.741118943999936,65.55036041900007],[-13.787668423999946,65.52167389500005],[-13.821278449999909,65.54148997600004],[-13.824370897999927,65.57538483300004],[-13.850941535999937,65.59198639500005],[-13.85968990799995,65.61416250200006],[-13.890044725999928,65.62018463700008],[-13.99990800699993,65.60858795800004],[-14.027902798999946,65.61041901200008],[-14.247629360999952,65.66502513200004],[-14.32022050699993,65.65241120000007],[-14.444081183999913,65.57514069200005],[-14.472645636999914,65.55231354400007],[-14.530181443999936,65.54547760600008],[-14.45421301999994,65.58344147300005],[-14.43390865799995,65.60480377800008],[-14.382232225999928,65.63027578300006],[-14.357818162999934,65.65607330900008],[-14.292958136999914,65.68219635600008],[-14.378977016999954,65.70718008000006],[-14.397613084999932,65.73004791900007],[-14.372873501999948,65.74038320500006],[-14.329497850999928,65.78237539300005],[-14.36156165299991,65.79677969000005],[-14.479725714999915,65.77488841400003],[-14.694406704999949,65.75079987200007],[-14.808705206999946,65.72772858300004],[-14.844105597999942,65.73517487200007],[-14.83234615799995,65.75315989800004],[-14.861439581999946,65.76203034100007],[-14.821156378999945,65.79840729400007],[-14.822987433999913,65.81586334800005],[-14.72679602799991,65.85065338700008],[-14.66201738199993,65.88393789300005],[-14.623768683999913,65.91958242400005],[-14.604725714999915,65.96393463700008],[-14.616932745999918,65.99774811400005],[-14.65147864499994,66.00674062700006],[-14.677316860999952,66.05621979400007],[-14.719349738999938,66.07037995000007],[-14.770415818999936,66.06785716400003],[-14.852691209999932,66.03717682500007],[-14.898060675999943,66.03558991100005],[-14.96320553299995,66.04804108300004],[-15.075998501999948,66.04962799700007],[-15.05577551999994,66.07037995000007],[-15.07412675699993,66.09711334800005],[-15.13345292899993,66.09870026200008],[-15.179351365999935,66.11131419500003],[-15.13463294199994,66.14325592700004],[-15.056019660999937,66.14813873900005],[-14.960357225999928,66.18577708500004],[-14.935210740999935,66.22190989800004],[-14.978260870999918,66.25950755400004],[-15.014800584999932,66.26837799700007],[-14.959136522999927,66.29645416900007],[-14.867176886999914,66.30585358300004],[-14.78819739499994,66.33038971600007],[-14.610747850999928,66.36322663000004],[-14.563628709999932,66.38450755400004]]],[[[-17.751291469999956,63.71784088700008],[-17.79015051999994,63.71320221600007],[-17.82331295499995,63.691392320000034],[-17.74315344999991,63.703192450000074],[-17.751291469999956,63.71784088700008]]],[[[-20.261870897999927,63.444647528000075],[-20.305572068999936,63.434759833000044],[-20.286773240999935,63.40648021000004],[-20.252552863999938,63.42951080900008],[-20.261870897999927,63.444647528000075]]]]}},
diff --git a/client/src/api/admin/hooks/useOrganizations.test.tsx b/client/src/api/admin/hooks/useOrganizations.test.tsx
new file mode 100644
index 000000000..70037b65a
--- /dev/null
+++ b/client/src/api/admin/hooks/useOrganizations.test.tsx
@@ -0,0 +1,51 @@
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { act, renderHook } from "@testing-library/react";
+import { ReactNode } from "react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { useRemoveUserFromOrganization } from "./useOrganizations";
+
+const mocks = vi.hoisted(() => ({ removeMember: vi.fn() }));
+
+vi.mock("../../../lib/auth", () => ({
+ authClient: { organization: { removeMember: mocks.removeMember } },
+}));
+
+function renderRemoveMember() {
+ const queryClient = new QueryClient({ defaultOptions: { mutations: { retry: false } } });
+ const wrapper = ({ children }: { children: ReactNode }) => (
+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
+ );
+ return renderHook(() => useRemoveUserFromOrganization(), { wrapper });
+}
+
+afterEach(() => {
+ vi.clearAllMocks();
+});
+
+describe("useRemoveUserFromOrganization", () => {
+ it("fails when Better Auth resolves with an error instead of throwing", async () => {
+ mocks.removeMember.mockResolvedValue({ data: null, error: { status: 403, message: "Not allowed" } });
+ const onSuccess = vi.fn();
+ const { result } = renderRemoveMember();
+
+ await act(async () => {
+ await expect(
+ result.current.mutateAsync({ memberIdOrEmail: "m1", organizationId: "org1" }, { onSuccess })
+ ).rejects.toThrow("Not allowed");
+ });
+
+ expect(onSuccess).not.toHaveBeenCalled();
+ });
+
+ it("succeeds when the member was removed", async () => {
+ mocks.removeMember.mockResolvedValue({ data: { member: { id: "m1" } }, error: null });
+ const { result } = renderRemoveMember();
+
+ await act(async () => {
+ await result.current.mutateAsync({ memberIdOrEmail: "m1", organizationId: "org1" });
+ });
+
+ expect(mocks.removeMember).toHaveBeenCalledWith({ memberIdOrEmail: "m1", organizationId: "org1" });
+ });
+});
diff --git a/client/src/api/admin/hooks/useOrganizations.ts b/client/src/api/admin/hooks/useOrganizations.ts
index 1931b756c..03161158f 100644
--- a/client/src/api/admin/hooks/useOrganizations.ts
+++ b/client/src/api/admin/hooks/useOrganizations.ts
@@ -84,10 +84,14 @@ export function useRemoveUserFromOrganization() {
return useMutation<void, Error, RemoveUserFromOrganizationInput>({
mutationFn: async ({ memberIdOrEmail, organizationId }: RemoveUserFromOrganizationInput) => {
try {
- await authClient.organization.removeMember({
+ // Better Auth reports a refused request as { error } rather than throwing.
+ const { error } = await authClient.organization.removeMember({
memberIdOrEmail,
organizationId,
});
+ if (error) {
+ throw new Error(error.message || "Failed to remove user from organization");
+ }
} catch (error) {
throw new Error(error instanceof Error ? error.message : "Failed to remove user from organization");
}
diff --git a/client/src/api/analytics/hooks/events/useGetEvents.ts b/client/src/api/analytics/hooks/events/useGetEvents.ts
index 0af25f1d0..20c64bc51 100644
--- a/client/src/api/analytics/hooks/events/useGetEvents.ts
+++ b/client/src/api/analytics/hooks/events/useGetEvents.ts
@@ -1,28 +1,53 @@
import { useQuery } from "@tanstack/react-query";
import { buildAnalyticsRequest, fetchAnalytics } from "../../analyticsRequest";
-import { CursorEventsResponse, NewEventsResponse } from "../../endpoints";
+import { CursorEventsResponse, Event } from "../../endpoints";
import { useAnalyticsContext, useAnalyticsInfiniteQuery } from "../../useAnalyticsQuery";
/**
- * Polls for new events since a given timestamp (Realtime mode).
- * The sinceTimestamp is read from a callback at query time so the query key
+ * An event as the log endpoint returns it. `event_cursor` is epoch milliseconds
+ * plus a hash of the event: it pages and polls exactly (by the whole-second
+ * `timestamp`, the other events in the boundary second were skipped) and tells
+ * the event apart from every other one.
+ */
+export type EventLogEvent = Event & { event_cursor: string };
+
+export type EventsCursorPage = Omit<CursorEventsResponse, "data"> & {
+ data: EventLogEvent[];
+ cursor: CursorEventsResponse["cursor"] & { oldestCursor?: string | null; newestCursor?: string | null };
+};
+
+/**
+ * `nextCursor` is where the next poll resumes. It trails the newest event by
+ * the window the server reads again (events can be inserted out of order), so
+ * a poll repeats recent rows; tell them apart by `event_cursor`.
+ */
+export type EventsPollResponse = {
+ data: EventLogEvent[];
+ cursor?: { nextCursor: string; hasMore: boolean };
+};
+
+/**
+ * Polls for events newer than a cursor (Realtime mode).
+ * The cursor is read from a callback at query time so the query key
* stays stable and doesn't cause refetch storms.
*/
-export function useNewEventsPoll(options: { getSinceTimestamp: () => string | null; enabled: boolean }) {
+export function useNewEventsPoll(options: { getSinceCursor: () => string | null; enabled: boolean }) {
const { site, context } = useAnalyticsContext({ useTime: false });
const request = buildAnalyticsRequest({ path: "events", unwrap: false }, context);
return useQuery({
queryKey: ["events-poll", site, request.params],
queryFn: () => {
- const since = options.getSinceTimestamp();
- if (!since) return { data: [] } as NewEventsResponse;
- return fetchAnalytics<NewEventsResponse>(site, {
+ const since = options.getSinceCursor();
+ if (!since) return { data: [] } as EventsPollResponse;
+ return fetchAnalytics<EventsPollResponse>(site, {
...request,
- params: { ...request.params, since_timestamp: since },
+ params: { ...request.params, since_cursor: since },
});
},
- refetchInterval: 2000,
+ // A backlog over the server's limit comes back a page at a time, oldest
+ // first: fetch the next page straight away.
+ refetchInterval: query => (query.state.data?.cursor?.hasMore ? 250 : 2000),
enabled: !!site && options.enabled,
});
}
@@ -35,7 +60,7 @@ export function useNewEventsPoll(options: { getSinceTimestamp: () => string | nu
export function useGetEventsCursor(options: { isRealtime: boolean; pageSize?: number }) {
const pageSize = options.pageSize ?? 50;
- return useAnalyticsInfiniteQuery<CursorEventsResponse, string | null>({
+ return useAnalyticsInfiniteQuery<EventsCursorPage, string | null>({
key: "events-cursor",
path: "events",
unwrap: false,
@@ -43,8 +68,8 @@ export function useGetEventsCursor(options: { isRealtime: boolean; pageSize?: nu
params: { page_size: pageSize },
initialPageParam: null,
staleTime: 0,
- pageParams: cursor => ({ before_timestamp: cursor ?? undefined }),
+ pageParams: cursor => ({ before_cursor: cursor ?? undefined }),
getNextPageParam: lastPage =>
- lastPage.cursor?.hasMore && lastPage.cursor.oldestTimestamp ? lastPage.cursor.oldestTimestamp : undefined,
+ lastPage.cursor?.hasMore && lastPage.cursor.oldestCursor ? lastPage.cursor.oldestCursor : undefined,
});
}
diff --git a/client/src/api/analytics/hooks/useGetUserSessions.ts b/client/src/api/analytics/hooks/useGetUserSessions.ts
index 7cba4bb74..1ce112029 100644
--- a/client/src/api/analytics/hooks/useGetUserSessions.ts
+++ b/client/src/api/analytics/hooks/useGetUserSessions.ts
@@ -1,6 +1,6 @@
import { Time } from "../../../components/DateSelector/types";
import { SESSION_PAGE_FILTERS, USER_DETAIL_PAGE_FILTERS } from "../../../lib/filterGroups";
-import { getFilteredFilters, useStore } from "../../../lib/store";
+import { getFilteredFilters } from "../../../lib/store";
import {
GetSessionsResponse,
SessionPageviewsAndEvents,
@@ -128,17 +128,15 @@ export function useGetLiveSessions(staleTime: number) {
}
export function useGetSessionDetailsInfinite(sessionId: string | null) {
- const time = useStore(state => state.time);
- // In past-minutes mode the session detail endpoint takes the window as a
- // minute count rather than a date range.
- const minutes = time.mode === "past-minutes" ? time.pastMinutesStart : undefined;
-
+ // A known session's details are the whole session, whatever window listed it.
+ // The user page lists other days and full histories, so clipping to a rolling
+ // past-minutes window (the endpoint's `minutes`) cut sessions off or 404ed them.
return useAnalyticsInfiniteQuery<SessionPageviewsAndEvents>({
key: ["session-details-infinite", sessionId],
path: `sessions/${sessionId}`,
useTime: false,
useFilters: false,
- params: { limit: 100, minutes },
+ params: { limit: 100 },
initialPageParam: 0,
pageParams: offset => ({ offset }),
getNextPageParam: lastPage =>
diff --git a/client/src/app/(home)/page.tsx b/client/src/app/(home)/page.tsx
index ca4c6b00f..494447494 100644
--- a/client/src/app/(home)/page.tsx
+++ b/client/src/app/(home)/page.tsx
@@ -96,6 +96,15 @@ export default function Home() {
const [selectedTeamFilter, setSelectedTeamFilter] = useState<string>("all");
const [page, setPage] = useState(1);
+ // Teams and tags belong to one organization: switching keeps this page mounted,
+ // and a stale filter would hide every site of the new one.
+ const [filtersOrganizationId, setFiltersOrganizationId] = useState(activeOrganization?.id);
+ if (activeOrganization?.id && activeOrganization.id !== filtersOrganizationId) {
+ setFiltersOrganizationId(activeOrganization.id);
+ setSelectedTags([]);
+ setSelectedTeamFilter("all");
+ }
+
// Filters run over the full org list, so changing them should jump back to
// the first page of results.
useEffect(() => {
diff --git a/client/src/app/[site]/components/SubHeader/Filters/const.tsx b/client/src/app/[site]/components/SubHeader/Filters/const.tsx
index b53464b4e..a49d01058 100644
--- a/client/src/app/[site]/components/SubHeader/Filters/const.tsx
+++ b/client/src/app/[site]/components/SubHeader/Filters/const.tsx
@@ -197,12 +197,9 @@ export const StringOperatorOptions = [
{ label: "Regex", value: "regex" },
{ label: "Is null", value: "is_null" },
{ label: "Is not null", value: "is_not_null" },
- { label: "Greater than", value: "greater_than" },
- { label: "Less than", value: "less_than" },
- { label: "Greater than or equal to", value: "greater_than_or_equal" },
- { label: "Less than or equal to", value: "less_than_or_equal" },
];
+// Comparisons only apply to numeric parameters; the API rejects them on text.
export const NumericOperatorOptions = [
{ label: "Equals", value: "equals" },
{ label: "Not equals", value: "not_equals" },
diff --git a/client/src/app/[site]/dashboards/[dashboardId]/page.tsx b/client/src/app/[site]/dashboards/[dashboardId]/page.tsx
index 951f05fae..186b52d43 100644
--- a/client/src/app/[site]/dashboards/[dashboardId]/page.tsx
+++ b/client/src/app/[site]/dashboards/[dashboardId]/page.tsx
@@ -7,7 +7,7 @@ import type { DashboardCard } from "@rybbit/shared";
import { ArrowLeft, Check, ChevronLeft, ChevronRight, Loader2, Pencil, Plus } from "lucide-react";
import { useParams, useRouter } from "next/navigation";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
-import { Responsive, WidthProvider, type Layout } from "react-grid-layout";
+import { Responsive, WidthProvider, type Layout, type Layouts } from "react-grid-layout";
import { useGetDashboard, useUpdateDashboard } from "../../../../api/analytics/hooks/useDashboards";
import { BucketSelection } from "../../../../components/BucketSelection";
import { DateSelector } from "../../../../components/DateSelector/DateSelector";
@@ -35,8 +35,11 @@ import type { DashboardExample } from "../examples";
import { cloneCard, createCard, createCardFromExample, MAX_CARDS_PER_DASHBOARD } from "../utils";
const ResponsiveGridLayout = WidthProvider(Responsive);
-const GRID_COLS = { lg: 12, md: 12, sm: 6, xs: 4, xxs: 2 };
+const GRID_COLS: Record<string, number> = { lg: 12, md: 12, sm: 6, xs: 4, xxs: 2 };
const BREAKPOINTS = { lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0 };
+// Stored card positions are in this many columns; narrower breakpoints show a
+// copy squeezed into fewer columns.
+const CANONICAL_COLS = 12;
export default function DashboardDetailPage() {
useSetPageTitle("Dashboard");
@@ -83,8 +86,13 @@ export default function DashboardDetailPage() {
const editingCard = workingCards.find(card => card.id === editingCardId) ?? null;
- const handleLayoutChange = (next: Layout[]) => {
+ const handleLayoutChange = (next: Layout[], allLayouts: Layouts) => {
if (!editMode) return;
+ // The grid reports the active breakpoint's layout, keyed under that
+ // breakpoint in allLayouts. Below 12 columns it is the stored layout
+ // clamped to fit, so writing it back would squeeze the desktop layout.
+ const breakpoint = Object.keys(allLayouts).find(key => allLayouts[key] === next);
+ if (!breakpoint || GRID_COLS[breakpoint] !== CANONICAL_COLS) return;
let changed = false;
const updated = workingCards.map(card => {
const item = next.find(entry => entry.i === card.id);
diff --git a/client/src/app/[site]/dashboards/components/charts/DashboardBarChart.tsx b/client/src/app/[site]/dashboards/components/charts/DashboardBarChart.tsx
index 46ea64cbf..a597e769d 100644
--- a/client/src/app/[site]/dashboards/components/charts/DashboardBarChart.tsx
+++ b/client/src/app/[site]/dashboards/components/charts/DashboardBarChart.tsx
@@ -3,13 +3,13 @@
import type { DashboardCardMapping } from "@rybbit/shared";
import * as d3 from "d3";
import { useTheme } from "next-themes";
-import { useEffect, useMemo, useRef, useState } from "react";
+import { useMemo, useState } from "react";
import { createPortal } from "react-dom";
import type { CustomQueryRow } from "@/api/analytics/endpoints";
import { useStore } from "@/lib/store";
import { formatter } from "@/lib/utils";
import { buildChartAxis, buildWideData } from "../../utils";
-import { CardLegend, ChartEmpty, DashboardTooltip, toCardSeries } from "./shared";
+import { CardLegend, ChartEmpty, DashboardTooltip, toCardSeries, useElementSize } from "./shared";
type DashboardBarChartProps = {
rows: CustomQueryRow[];
@@ -44,20 +44,7 @@ export function DashboardBarChart({ rows, mapping }: DashboardBarChartProps) {
[wide]
);
- const wrapperRef = useRef<HTMLDivElement>(null);
- const [size, setSize] = useState<{ width: number; height: number }>({ width: 0, height: 0 });
- useEffect(() => {
- if (!wrapperRef.current) return;
- const el = wrapperRef.current;
- const ro = new ResizeObserver(entries => {
- const entry = entries[0];
- if (!entry) return;
- const { width, height } = entry.contentRect;
- setSize({ width, height });
- });
- ro.observe(el);
- return () => ro.disconnect();
- }, []);
+ const [wrapperRef, size] = useElementSize<HTMLDivElement>();
const [hover, setHover] = useState<{ index: number; clientX: number; clientY: number } | null>(null);
diff --git a/client/src/app/[site]/dashboards/components/charts/DashboardPie.tsx b/client/src/app/[site]/dashboards/components/charts/DashboardPie.tsx
index 316af3782..e2d8818de 100644
--- a/client/src/app/[site]/dashboards/components/charts/DashboardPie.tsx
+++ b/client/src/app/[site]/dashboards/components/charts/DashboardPie.tsx
@@ -2,11 +2,11 @@
import type { DashboardCardMapping } from "@rybbit/shared";
import * as d3 from "d3";
-import { useEffect, useMemo, useRef, useState } from "react";
+import { useMemo, useState } from "react";
import { createPortal } from "react-dom";
import type { CustomQueryRow } from "@/api/analytics/endpoints";
import { buildPieData, formatValue } from "../../utils";
-import { CardLegend, cardColor, ChartEmpty, DashboardTooltip, type CardSeries } from "./shared";
+import { CardLegend, cardColor, ChartEmpty, DashboardTooltip, type CardSeries, useElementSize } from "./shared";
type DashboardPieProps = {
rows: CustomQueryRow[];
@@ -24,18 +24,7 @@ export function DashboardPie({ rows, mapping }: DashboardPieProps) {
const total = useMemo(() => slices.reduce((sum, slice) => sum + slice.value, 0), [slices]);
- const wrapperRef = useRef<HTMLDivElement>(null);
- const [size, setSize] = useState({ width: 0, height: 0 });
- useEffect(() => {
- if (!wrapperRef.current) return;
- const el = wrapperRef.current;
- const ro = new ResizeObserver(entries => {
- const rect = entries[0]?.contentRect;
- if (rect) setSize({ width: rect.width, height: rect.height });
- });
- ro.observe(el);
- return () => ro.disconnect();
- }, []);
+ const [wrapperRef, size] = useElementSize<HTMLDivElement>();
const [hover, setHover] = useState<{ index: number; clientX: number; clientY: number } | null>(null);
diff --git a/client/src/app/[site]/dashboards/components/charts/DashboardStat.tsx b/client/src/app/[site]/dashboards/components/charts/DashboardStat.tsx
index ac5daccd0..b91141a65 100644
--- a/client/src/app/[site]/dashboards/components/charts/DashboardStat.tsx
+++ b/client/src/app/[site]/dashboards/components/charts/DashboardStat.tsx
@@ -2,10 +2,10 @@
import type { DashboardCardMapping } from "@rybbit/shared";
import NumberFlow from "@number-flow/react";
-import { useEffect, useMemo, useRef, useState } from "react";
+import { useMemo } from "react";
import type { CustomQueryRow } from "@/api/analytics/endpoints";
import { formatValue, getStatValue } from "../../utils";
-import { ChartEmpty } from "./shared";
+import { ChartEmpty, useElementSize } from "./shared";
type DashboardStatProps = {
rows: CustomQueryRow[];
@@ -18,18 +18,7 @@ export function DashboardStat({ rows, mapping }: DashboardStatProps) {
const format = mapping.valueFormat ?? "number";
// Scale the figure to the card: track size, derive a font size from it.
- const ref = useRef<HTMLDivElement>(null);
- const [size, setSize] = useState({ width: 0, height: 0 });
- useEffect(() => {
- if (!ref.current) return;
- const el = ref.current;
- const ro = new ResizeObserver(entries => {
- const rect = entries[0]?.contentRect;
- if (rect) setSize({ width: rect.width, height: rect.height });
- });
- ro.observe(el);
- return () => ro.disconnect();
- }, []);
+ const [ref, size] = useElementSize<HTMLDivElement>();
if (!stat) {
return <ChartEmpty message="No numeric value to display. Return a single number, e.g. SELECT count() AS total." />;
diff --git a/client/src/app/[site]/dashboards/components/charts/shared.tsx b/client/src/app/[site]/dashboards/components/charts/shared.tsx
index cb21f0da3..ec21cfbdc 100644
--- a/client/src/app/[site]/dashboards/components/charts/shared.tsx
+++ b/client/src/app/[site]/dashboards/components/charts/shared.tsx
@@ -1,5 +1,6 @@
"use client";
+import { useCallback, useState } from "react";
import { ChartTooltip } from "@/components/charts/ChartTooltip";
import { formatter } from "@/lib/utils";
import { CARD_PALETTE } from "../../utils";
@@ -65,6 +66,26 @@ export function CardLegend({ series }: { series: CardSeries[] }) {
);
}
+/**
+ * Tracks an element's content size. Returns a callback ref rather than taking a
+ * ref object: cards render <ChartEmpty /> until their data can be drawn, so the
+ * measured element may mount after the first render, and a mount-time effect
+ * would never observe it.
+ */
+export function useElementSize<T extends HTMLElement>() {
+ const [size, setSize] = useState({ width: 0, height: 0 });
+ const ref = useCallback((el: T | null) => {
+ if (!el) return;
+ const ro = new ResizeObserver(entries => {
+ const rect = entries[0]?.contentRect;
+ if (rect) setSize({ width: rect.width, height: rect.height });
+ });
+ ro.observe(el);
+ return () => ro.disconnect();
+ }, []);
+ return [ref, size] as const;
+}
+
export function ChartEmpty({ message = "Configure chart columns to render this card" }: { message?: string }) {
return (
<div className="flex h-full items-center justify-center px-3 text-center text-xs text-neutral-500">
diff --git a/client/src/app/[site]/dashboards/components/charts/useElementSize.test.tsx b/client/src/app/[site]/dashboards/components/charts/useElementSize.test.tsx
new file mode 100644
index 000000000..ddd478856
--- /dev/null
+++ b/client/src/app/[site]/dashboards/components/charts/useElementSize.test.tsx
@@ -0,0 +1,63 @@
+import { act, cleanup, render } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { DashboardPie } from "./DashboardPie";
+
+type Callback = (entries: Array<{ contentRect: { width: number; height: number } }>) => void;
+
+type ObserverRecord = { callback: Callback; elements: Element[]; disconnected: boolean };
+
+const observers: ObserverRecord[] = [];
+
+beforeEach(() => {
+ observers.length = 0;
+ vi.stubGlobal(
+ "ResizeObserver",
+ class {
+ private record: ObserverRecord;
+ constructor(callback: Callback) {
+ this.record = { callback, elements: [], disconnected: false };
+ observers.push(this.record);
+ }
+ observe(element: Element) {
+ this.record.elements.push(element);
+ }
+ unobserve() {}
+ disconnect() {
+ this.record.disconnected = true;
+ }
+ }
+ );
+});
+
+afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+});
+
+const mapping = { xColumn: "label", valueColumn: "visits" };
+
+describe("dashboard card sizing", () => {
+ it("measures a chart whose first render was the empty state", () => {
+ // Zero-valued rows render <ChartEmpty /> first, so the measured wrapper is not mounted yet.
+ const { container, rerender } = render(<DashboardPie rows={[{ label: "a", visits: 0 }]} mapping={mapping} />);
+ expect(container.querySelector("svg")).toBeNull();
+ expect(observers).toHaveLength(0);
+
+ rerender(<DashboardPie rows={[{ label: "a", visits: 5 }]} mapping={mapping} />);
+ const live = observers.filter(observer => !observer.disconnected);
+ expect(live).toHaveLength(1);
+ expect(live[0].elements).toHaveLength(1);
+
+ act(() => live[0].callback([{ contentRect: { width: 200, height: 200 } }]));
+ expect(container.querySelector("svg path")).not.toBeNull();
+ });
+
+ it("stops observing when the chart falls back to the empty state", () => {
+ const { rerender } = render(<DashboardPie rows={[{ label: "a", visits: 5 }]} mapping={mapping} />);
+ expect(observers).toHaveLength(1);
+
+ rerender(<DashboardPie rows={[{ label: "a", visits: 0 }]} mapping={mapping} />);
+ expect(observers[0].disconnected).toBe(true);
+ });
+});
diff --git a/client/src/app/[site]/errors/page.test.tsx b/client/src/app/[site]/errors/page.test.tsx
index c5c08f1eb..f5439230d 100644
--- a/client/src/app/[site]/errors/page.test.tsx
+++ b/client/src/app/[site]/errors/page.test.tsx
@@ -1,5 +1,5 @@
import React from "react";
-import { cleanup, render, screen } from "@testing-library/react";
+import { act, cleanup, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ErrorNameItem, ErrorNamesPaginatedResponse } from "@/api/analytics/endpoints";
import { useStore } from "@/lib/store";
@@ -13,18 +13,28 @@ type QueryState = {
error: Error | null;
};
-const mocks = vi.hoisted(() => ({ query: {} as QueryState }));
+const mocks = vi.hoisted(() => ({
+ query: {} as QueryState,
+ page: 0,
+ setPageIndex: (_index: number) => {},
+}));
vi.mock("next-intl", () => ({ useExtracted: () => (message: string) => message }));
-vi.mock("@/api/analytics/hooks/errors/useGetErrorNames", () => ({ useGetErrorNamesPaginated: () => mocks.query }));
+vi.mock("@/api/analytics/hooks/errors/useGetErrorNames", () => ({
+ useGetErrorNamesPaginated: ({ page }: { page: number }) => {
+ mocks.page = page;
+ return mocks.query;
+ },
+}));
vi.mock("@/hooks/useSetPageTitle", () => ({ useSetPageTitle: () => {} }));
vi.mock("@/components/DisabledOverlay", () => ({
DisabledOverlay: ({ children }: { children: React.ReactNode }) => <>{children}</>,
}));
vi.mock("@/components/pagination", () => ({
- Pagination: ({ isLoading }: { isLoading?: boolean }) => (
- <div data-testid="pagination">{isLoading ? "pagination loading" : "pagination idle"}</div>
- ),
+ Pagination: ({ isLoading, table }: { isLoading?: boolean; table: { setPageIndex: (index: number) => void } }) => {
+ mocks.setPageIndex = table.setPageIndex;
+ return <div data-testid="pagination">{isLoading ? "pagination loading" : "pagination idle"}</div>;
+ },
}));
vi.mock("../components/SubHeader/SubHeader", () => ({ SubHeader: () => null }));
vi.mock("./components/EnableErrorTracking", () => ({ EnableErrorTracking: () => null }));
@@ -94,3 +104,18 @@ describe("Errors page loading states", () => {
expect(screen.getByText("No error events found")).toBeTruthy();
});
});
+
+describe("Errors page pagination", () => {
+ it("starts every scope on page one, including one returned to", () => {
+ mocks.query = { ...settled, data: { ...response(["a is undefined"]), totalCount: 100 } };
+ render(<Errors />);
+ act(() => mocks.setPageIndex(3));
+ expect(mocks.page).toBe(4);
+
+ const chrome = { parameter: "browser" as const, type: "equals" as const, value: ["Chrome"] };
+ act(() => useStore.setState({ filters: [chrome] }));
+ expect(mocks.page).toBe(1);
+ act(() => useStore.setState({ filters: [] }));
+ expect(mocks.page).toBe(1);
+ });
+});
diff --git a/client/src/app/[site]/errors/page.tsx b/client/src/app/[site]/errors/page.tsx
index b6c5a2bdb..de55ddaa9 100644
--- a/client/src/app/[site]/errors/page.tsx
+++ b/client/src/app/[site]/errors/page.tsx
@@ -5,7 +5,7 @@ import { useGetErrorNamesPaginated } from "@/api/analytics/hooks/errors/useGetEr
import { ErrorNameItem } from "@/api/analytics/endpoints";
import { Pagination } from "@/components/pagination";
import { useSetPageTitle } from "@/hooks/useSetPageTitle";
-import { useStore } from "@/lib/store";
+import { useStore, useTimezone } from "@/lib/store";
import { cn } from "@/lib/utils";
import { useEffect, useState } from "react";
import { DisabledOverlay } from "../../../components/DisabledOverlay";
@@ -20,12 +20,22 @@ const PAGE_SIZE = 10;
export default function Errors() {
const t = useExtracted();
- const { site } = useStore();
+ const { site, time, filters } = useStore();
+ const timeZone = useTimezone();
- const [pagination, setPagination] = useState({
- pageIndex: 0,
- pageSize: PAGE_SIZE,
- });
+ // Anything that changes which errors are listed sends the pager back to page one, and keeps it there:
+ // returning to an earlier scope starts on page one too, not on the page it was left at.
+ const scope = JSON.stringify([site, time, filters, timeZone]);
+ const [pageState, setPageState] = useState({ scope, pageIndex: 0 });
+ const [pageSize, setPageSize] = useState(PAGE_SIZE);
+ if (pageState.scope !== scope) {
+ setPageState({ scope, pageIndex: 0 });
+ }
+ const pagination = { pageIndex: pageState.scope === scope ? pageState.pageIndex : 0, pageSize };
+ const setPagination = (next: { pageIndex: number; pageSize: number }) => {
+ setPageState({ scope, pageIndex: next.pageIndex });
+ setPageSize(next.pageSize);
+ };
const [totalPages, setTotalPages] = useState(0);
useSetPageTitle("Errors");
@@ -70,7 +80,13 @@ export default function Errors() {
useEffect(() => {
if (!isLoadingErrors && !isPlaceholderData && !isFetching) {
if (totalCount !== undefined) {
- setTotalPages(Math.ceil(totalCount / pagination.pageSize));
+ const pageCount = Math.ceil(totalCount / pagination.pageSize);
+ setTotalPages(pageCount);
+ // The list can shrink under the current page (a refetch, a rolling window): step back to the last
+ // page that has rows instead of showing an empty one.
+ if (pagination.pageIndex > 0 && pagination.pageIndex >= pageCount) {
+ setPageState({ scope, pageIndex: Math.max(pageCount - 1, 0) });
+ }
} else if (errorsDataArray && errorsDataArray.length === 0 && pagination.pageIndex === 0) {
// Fallback if totalCount is somehow undefined but we have an empty array on page 1
setTotalPages(0);
@@ -91,6 +107,7 @@ export default function Errors() {
isFetching,
pagination.pageIndex,
pagination.pageSize,
+ scope,
]);
if (!site) {
diff --git a/client/src/app/[site]/events/components/EventLog/eventLogUtils.test.ts b/client/src/app/[site]/events/components/EventLog/eventLogUtils.test.ts
index 0eda233c8..e7c9b19e3 100644
--- a/client/src/app/[site]/events/components/EventLog/eventLogUtils.test.ts
+++ b/client/src/app/[site]/events/components/EventLog/eventLogUtils.test.ts
@@ -1,6 +1,13 @@
import { describe, expect, it } from "vitest";
import { Event } from "../../../../../api/analytics/endpoints";
-import { formatPropertyValue, getEventRowData, matchesEventSearch, parseEventProperties } from "./eventLogUtils";
+import {
+ compareEventsNewestFirst,
+ formatPropertyValue,
+ getEventKey,
+ getEventRowData,
+ matchesEventSearch,
+ parseEventProperties,
+} from "./eventLogUtils";
const event = (overrides: Partial<Event> = {}): Event => ({
timestamp: "2026-09-30 14:32:22",
@@ -31,6 +38,36 @@ const event = (overrides: Partial<Event> = {}): Event => ({
...overrides,
});
+describe("getEventKey", () => {
+ it("tells apart two clicks in the same second", () => {
+ const first = { ...event(), event_cursor: "1790778742100_62316EA00DD5E0A3" };
+ const second = { ...event(), event_cursor: "1790778742900_62316EA00DD5E0A3" };
+
+ expect(getEventKey(first)).not.toBe(getEventKey(second));
+ });
+});
+
+describe("compareEventsNewestFirst", () => {
+ const at = (event_cursor: string) => ({ ...event(), event_cursor });
+
+ it("orders by millisecond, then tie-breaker, newest first", () => {
+ const rows = [
+ at("1790778742100_0000000000000001"),
+ at("1790778742900_0000000000000000"),
+ at("1790778742100_00000000000000FF"),
+ // Before 2001-09-09 the milliseconds have one digit fewer.
+ at("999999999999_FFFFFFFFFFFFFFFF"),
+ ];
+
+ expect(rows.sort(compareEventsNewestFirst).map(row => row.event_cursor)).toEqual([
+ "1790778742900_0000000000000000",
+ "1790778742100_00000000000000FF",
+ "1790778742100_0000000000000001",
+ "999999999999_FFFFFFFFFFFFFFFF",
+ ]);
+ });
+});
+
describe("formatPropertyValue", () => {
it("keeps strings as they are and prints everything else on one line", () => {
expect(formatPropertyValue("google")).toBe("google");
diff --git a/client/src/app/[site]/events/components/EventLog/eventLogUtils.tsx b/client/src/app/[site]/events/components/EventLog/eventLogUtils.tsx
index 46895f5b1..f184f1445 100644
--- a/client/src/app/[site]/events/components/EventLog/eventLogUtils.tsx
+++ b/client/src/app/[site]/events/components/EventLog/eventLogUtils.tsx
@@ -1,9 +1,22 @@
"use client";
import { Event } from "../../../../../api/analytics/endpoints";
+import type { EventLogEvent } from "../../../../../api/analytics/hooks/events/useGetEvents";
-export function getEventKey(event: Event) {
- return `${event.timestamp}-${event.session_id}-${event.user_id}-${event.type}-${event.event_name ?? ""}-${event.pathname}`;
+/**
+ * The event's cursor, which tells it apart from every other event. A key built
+ * from the whole-second timestamp merged two clicks within the same second.
+ */
+export function getEventKey(event: EventLogEvent) {
+ return event.event_cursor;
+}
+
+/** Newest first, in the endpoint's order: by millisecond, then tie-breaker. */
+export function compareEventsNewestFirst(a: EventLogEvent, b: EventLogEvent) {
+ // The milliseconds have no leading zeros, so a longer cursor is a later one.
+ const x = a.event_cursor;
+ const y = b.event_cursor;
+ return y.length - x.length || (y < x ? -1 : y > x ? 1 : 0);
}
export function parseEventProperties(event: Event): Record<string, any> {
diff --git a/client/src/app/[site]/events/components/EventLog/useEventLogState.ts b/client/src/app/[site]/events/components/EventLog/useEventLogState.ts
index 8cd55ab6b..ac6592e58 100644
--- a/client/src/app/[site]/events/components/EventLog/useEventLogState.ts
+++ b/client/src/app/[site]/events/components/EventLog/useEventLogState.ts
@@ -1,9 +1,12 @@
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { type Arrivals, NO_ARRIVALS, recordArrivals } from "@/components/interior/use-arrival-highlight";
-import { Event } from "../../../../../api/analytics/endpoints";
-import { useGetEventsCursor, useNewEventsPoll } from "../../../../../api/analytics/hooks/events/useGetEvents";
+import {
+ type EventLogEvent,
+ useGetEventsCursor,
+ useNewEventsPoll,
+} from "../../../../../api/analytics/hooks/events/useGetEvents";
import { useStore } from "../../../../../lib/store";
-import { getEventKey } from "./eventLogUtils";
+import { compareEventsNewestFirst, getEventKey } from "./eventLogUtils";
const MAX_EVENTS = 10_000;
const PAGE_SIZE = 100;
@@ -17,9 +20,11 @@ export function useEventLogState(
const [isRealtime, setIsRealtime] = useState(true);
// --- Prepended events from polling (realtime mode) ---
- const [prependedEvents, setPrependedEvents] = useState<Event[]>([]);
+ const [prependedEvents, setPrependedEvents] = useState<EventLogEvent[]>([]);
const seenKeysRef = useRef(new Set<string>());
- const latestTimestampRef = useRef<string | null>(null);
+ // Where the next poll resumes, as the endpoint's cursor: seeded from the newest loaded event, then
+ // whatever each poll returns.
+ const latestCursorRef = useRef<string | null>(null);
// --- Pause / buffer state ---
const [isLive, setIsLive] = useState(true);
@@ -27,12 +32,12 @@ export function useEventLogState(
isLiveRef.current = isLive;
const isRealtimeRef = useRef(true);
isRealtimeRef.current = isRealtime;
- const bufferedEventsRef = useRef<Event[]>([]);
+ const bufferedEventsRef = useRef<EventLogEvent[]>([]);
const [bufferedCount, setBufferedCount] = useState(0);
// --- Rows that just entered the list (event key β†’ arrival time), for a brief highlight ---
const [arrivals, setArrivals] = useState<Arrivals>(NO_ARRIVALS);
- const markArrived = useCallback((events: Event[]) => {
+ const markArrived = useCallback((events: EventLogEvent[]) => {
const now = performance.now();
const keys = events.map(getEventKey);
setArrivals(prev => recordArrivals(prev, keys, now));
@@ -55,15 +60,18 @@ export function useEventLogState(
// --- Derive cursor events from query data ---
const cursorEvents = useMemo(() => cursorData?.pages.flatMap(p => p.data) ?? [], [cursorData]);
+ const newestCursor = cursorData?.pages[0]?.cursor?.newestCursor ?? null;
// --- Combined event list ---
// The cursor query refetches (staleTime 0, window refocus) and then returns events that already
- // arrived live, so drop those from the prepended rows.
+ // arrived live, so drop those from the prepended rows. A live event can also be older than rows
+ // already shown (the poll re-reads recent events that were inserted late), so put each one in its
+ // place. Both lists are nearly in order already, which the sort handles in close to linear time.
const mergedEvents = useMemo(() => {
if (prependedEvents.length === 0) return cursorEvents;
const cursorKeys = new Set(cursorEvents.map(getEventKey));
const liveOnly = prependedEvents.filter(ev => !cursorKeys.has(getEventKey(ev)));
- return [...liveOnly, ...cursorEvents].slice(0, MAX_EVENTS);
+ return [...liveOnly, ...cursorEvents].sort(compareEventsNewestFirst).slice(0, MAX_EVENTS);
}, [prependedEvents, cursorEvents]);
// --- Client-side type filter ---
@@ -80,7 +88,7 @@ export function useEventLogState(
const resetKey = `${isRealtime}|${site}|${filtersKey}`;
const resetKeyRef = useRef(resetKey);
- // --- Rebuild seenKeys + set latestTimestamp when cursor data changes, resetting live state first on a new key ---
+ // --- Rebuild seenKeys + set latestCursor when cursor data changes, resetting live state first on a new key ---
// One effect, so a reset always re-seeds the poll cursor from the rows on screen and never re-adds the
// keys of live rows it just discarded.
useEffect(() => {
@@ -88,7 +96,7 @@ export function useEventLogState(
resetKeyRef.current = resetKey;
if (isReset) {
setPrependedEvents([]);
- latestTimestampRef.current = null;
+ latestCursorRef.current = null;
bufferedEventsRef.current = [];
setBufferedCount(0);
setIsLive(true);
@@ -97,29 +105,37 @@ export function useEventLogState(
seenKeysRef.current = new Set(cursorEvents.map(getEventKey));
if (!isReset) {
- for (const ev of prependedEvents) {
+ // Buffered rows too: every poll returns recent events again.
+ for (const ev of [...prependedEvents, ...bufferedEventsRef.current]) {
seenKeysRef.current.add(getEventKey(ev));
}
}
- if (cursorEvents.length > 0 && !latestTimestampRef.current) {
- latestTimestampRef.current = cursorEvents[0].timestamp;
+ if (newestCursor && !latestCursorRef.current) {
+ latestCursorRef.current = newestCursor;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [cursorEvents, resetKey]);
// --- Poll query (realtime only) ---
- const getSinceTimestamp = useCallback(() => latestTimestampRef.current, []);
+ const getSinceCursor = useCallback(() => latestCursorRef.current, []);
const { data: pollData } = useNewEventsPoll({
- getSinceTimestamp,
+ getSinceCursor,
enabled: isRealtime,
});
// --- Handle poll results (realtime only) ---
useEffect(() => {
- if (!isRealtime || !pollData?.data?.length) return;
+ if (!isRealtime || !pollData) return;
+
+ // Taken as is, even when it is older than the cursor sent: it trails the newest event by the
+ // window the server reads again, for events inserted out of order.
+ if (pollData.cursor) {
+ latestCursorRef.current = pollData.cursor.nextCursor;
+ }
+ if (!pollData.data.length) return;
const incoming = pollData.data;
- const newEvents: Event[] = [];
+ const newEvents: EventLogEvent[] = [];
for (const ev of incoming) {
const key = getEventKey(ev);
if (!seenKeysRef.current.has(key)) {
@@ -129,12 +145,6 @@ export function useEventLogState(
}
if (newEvents.length === 0) return;
- // Update latest timestamp
- const newestTs = newEvents[0].timestamp;
- if (!latestTimestampRef.current || newestTs > latestTimestampRef.current) {
- latestTimestampRef.current = newestTs;
- }
-
if (isLiveRef.current) {
setPrependedEvents(prev => {
const combined = [...newEvents, ...prev];
diff --git a/client/src/app/[site]/experiments/components/CreateExperimentWizard.tsx b/client/src/app/[site]/experiments/components/CreateExperimentWizard.tsx
index 66aee2e3f..1a2acee67 100644
--- a/client/src/app/[site]/experiments/components/CreateExperimentWizard.tsx
+++ b/client/src/app/[site]/experiments/components/CreateExperimentWizard.tsx
@@ -1,10 +1,21 @@
"use client";
-import type { Experiment, FeatureFlagVariant, GoalType as AnyGoalType } from "@/api/analytics/endpoints";
+import type {
+ Experiment,
+ FeatureFlag,
+ FeatureFlagPayload,
+ FeatureFlagVariant,
+ GoalType as AnyGoalType,
+} from "@/api/analytics/endpoints";
import { useCreateExperiment, useUpdateExperiment } from "@/api/analytics/hooks/experiments/useExperiments";
-import { useCreateFeatureFlag, useFeatureFlags } from "@/api/analytics/hooks/featureFlags/useFeatureFlags";
-import { useCreateGoal } from "@/api/analytics/hooks/goals/useCreateGoal";
+import {
+ useCreateFeatureFlag,
+ useFeatureFlags,
+ useUpdateFeatureFlag,
+} from "@/api/analytics/hooks/featureFlags/useFeatureFlags";
+import { type CreateGoalRequest, useCreateGoal } from "@/api/analytics/hooks/goals/useCreateGoal";
import { useGetGoals } from "@/api/analytics/hooks/goals/useGetGoals";
+import { useUpdateGoal } from "@/api/analytics/hooks/goals/useUpdateGoal";
import { useMetric } from "@/api/analytics/hooks/useGetMetric";
import { CodeSnippet } from "@/components/CodeSnippet";
import { Button } from "@/components/ui/button";
@@ -19,7 +30,7 @@ import { useStore } from "@/lib/store";
import { cn } from "@/lib/utils";
import { Check, FlaskConical, Flag, MousePointerClick, Plus, Target, Trash2 } from "lucide-react";
import { useExtracted } from "next-intl";
-import { useEffect, useMemo, useState } from "react";
+import { useEffect, useMemo, useRef, useState } from "react";
import type { ReactNode } from "react";
type WizardStep = "basics" | "assignment" | "goal" | "implementation";
@@ -49,6 +60,12 @@ type VariantSourceFlag = {
}>;
};
+// What an earlier, failed save already created, with the request that created it.
+type CreatedResources = {
+ flag?: { request: string; flag: FeatureFlag };
+ goal?: { request: string; goalId: number };
+};
+
type ImplementationState = {
mode: "created" | "updated";
experiment: Experiment;
@@ -86,12 +103,16 @@ function createId() {
: `${Date.now()}-${Math.random().toString(36).slice(2)}`;
}
+// Flag and variant keys must match the server's /^[A-Za-z][A-Za-z0-9_.:-]*$/ (max 100 chars), so a key
+// never starts with a digit or separator: "2026 pricing test" becomes "pricing_test".
function slugify(value: string) {
return value
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, "_")
- .replace(/^_+|_+$/g, "");
+ .replace(/^[^a-z]+/, "")
+ .slice(0, 100)
+ .replace(/_+$/, "");
}
function getVariantDraftsFromFlag(flag: VariantSourceFlag): VariantDraft[] {
@@ -339,7 +360,9 @@ export function CreateExperimentWizard({
const { data: pathsData } = useMetric({ parameter: "pathname", limit: 1000, useFilters: false });
const { data: eventsData } = useMetric({ parameter: "event_name", limit: 1000, useFilters: false });
const createFeatureFlagMutation = useCreateFeatureFlag();
+ const updateFeatureFlagMutation = useUpdateFeatureFlag();
const createGoalMutation = useCreateGoal();
+ const updateGoalMutation = useUpdateGoal();
const createExperimentMutation = useCreateExperiment();
const updateExperimentMutation = useUpdateExperiment();
const [internalOpen, setInternalOpen] = useState(false);
@@ -349,6 +372,10 @@ export function CreateExperimentWizard({
const [savedImplementationState, setSavedImplementationState] = useState<ImplementationState | null>(null);
const [flagKeyTouched, setFlagKeyTouched] = useState(false);
const [goalNameTouched, setGoalNameTouched] = useState(false);
+ // A save is up to three requests (flag, goal, experiment) that commit separately. When a later one
+ // fails, the retry reuses what the earlier ones created, updated in place if the form changed since,
+ // instead of creating duplicates: a second flag with the same key is a 409, a second goal an orphan.
+ const createdRef = useRef<CreatedResources>({});
const isEditing = !!experiment;
const usedFlagIds = useMemo(
@@ -382,7 +409,9 @@ export function CreateExperimentWizard({
const activeStepIndex = STEPS.indexOf(step);
const isSaving =
createFeatureFlagMutation.isPending ||
+ updateFeatureFlagMutation.isPending ||
createGoalMutation.isPending ||
+ updateGoalMutation.isPending ||
createExperimentMutation.isPending ||
updateExperimentMutation.isPending;
@@ -393,6 +422,7 @@ export function CreateExperimentWizard({
setSavedImplementationState(null);
setFlagKeyTouched(!!experiment);
setGoalNameTouched(!!experiment);
+ createdRef.current = {};
}, [experiment?.experimentId, open]);
useEffect(() => {
@@ -539,6 +569,64 @@ export function CreateExperimentWizard({
const implementationState = savedImplementationState || (isEditing ? buildImplementationState("updated") : null);
+ const saveNewFlag = async (): Promise<FeatureFlag> => {
+ const variants: FeatureFlagVariant[] = form.variants.map(variant => ({
+ key: variant.key.trim(),
+ name: variant.name.trim() || undefined,
+ rolloutPercentage: Number(variant.rolloutPercentage),
+ }));
+ const payload: FeatureFlagPayload = {
+ key: form.flagKey.trim(),
+ description: form.flagDescription.trim() || `Assignment flag for ${form.name.trim()}`,
+ enabled: true,
+ runtime: "client",
+ flagType: "multivariate",
+ payload: null,
+ variants: [],
+ rolloutPercentage: 100,
+ rules: [],
+ conditionSets: [
+ {
+ name: "Default",
+ rules: [],
+ variants,
+ },
+ ],
+ };
+ const request = JSON.stringify(payload);
+ const previous = createdRef.current.flag;
+ if (previous?.request === request) return previous.flag;
+
+ const { data: flag } = previous
+ ? await updateFeatureFlagMutation.mutateAsync({ flagId: previous.flag.flagId, payload })
+ : await createFeatureFlagMutation.mutateAsync(payload);
+ createdRef.current.flag = { request, flag };
+ return flag;
+ };
+
+ const saveNewGoal = async (): Promise<number> => {
+ const goal: CreateGoalRequest = {
+ siteId: Number(site),
+ name: form.goalName.trim() || `${form.name.trim()} conversion`,
+ goalType: form.goalType,
+ config:
+ form.goalType === "path" ? { pathPattern: form.pathPattern.trim() } : { eventName: form.eventName.trim() },
+ };
+ const request = JSON.stringify(goal);
+ const previous = createdRef.current.goal;
+ if (previous?.request === request) return previous.goalId;
+
+ let goalId: number;
+ if (previous) {
+ await updateGoalMutation.mutateAsync({ ...goal, goalId: previous.goalId });
+ goalId = previous.goalId;
+ } else {
+ goalId = (await createGoalMutation.mutateAsync(goal)).goalId;
+ }
+ createdRef.current.goal = { request, goalId };
+ return goalId;
+ };
+
const submitExperiment = async () => {
const validationError = validateExperimentConfiguration();
if (validationError) {
@@ -552,34 +640,10 @@ export function CreateExperimentWizard({
let variantKeys = selectedFlag ? getVariantKeys(selectedFlag) : form.variants.map(variant => variant.key.trim());
if (form.assignmentMode === "new") {
- const variants: FeatureFlagVariant[] = form.variants.map(variant => ({
- key: variant.key.trim(),
- name: variant.name.trim() || undefined,
- rolloutPercentage: Number(variant.rolloutPercentage),
- }));
-
- const createdFlag = await createFeatureFlagMutation.mutateAsync({
- key: form.flagKey.trim(),
- description: form.flagDescription.trim() || `Assignment flag for ${form.name.trim()}`,
- enabled: true,
- runtime: "client",
- flagType: "multivariate",
- payload: null,
- variants: [],
- rolloutPercentage: 100,
- rules: [],
- conditionSets: [
- {
- name: "Default",
- rules: [],
- variants,
- },
- ],
- });
-
- featureFlagId = createdFlag.data.flagId;
- flagKey = createdFlag.data.key;
- variantKeys = getVariantKeys(createdFlag.data);
+ const createdFlag = await saveNewFlag();
+ featureFlagId = createdFlag.flagId;
+ flagKey = createdFlag.key;
+ variantKeys = getVariantKeys(createdFlag);
}
let primaryGoalId: number | null = form.goalMode === "existing" ? Number(form.existingGoalId) : null;
@@ -587,15 +651,7 @@ export function CreateExperimentWizard({
let goalLabel = form.goalMode === "none" ? undefined : (goalDisplayPattern(selectedGoal) ?? form.goalName.trim());
if (form.goalMode === "new") {
- const createdGoal = await createGoalMutation.mutateAsync({
- siteId: Number(site),
- name: form.goalName.trim() || `${form.name.trim()} conversion`,
- goalType: form.goalType,
- config:
- form.goalType === "path" ? { pathPattern: form.pathPattern.trim() } : { eventName: form.eventName.trim() },
- });
-
- primaryGoalId = createdGoal.goalId;
+ primaryGoalId = await saveNewGoal();
goalType = form.goalType;
goalLabel = form.goalType === "path" ? form.pathPattern.trim() : form.eventName.trim();
}
@@ -608,6 +664,8 @@ export function CreateExperimentWizard({
primaryGoalId,
status: "draft",
});
+ // The experiment now owns them; a later save must not reuse or rewrite them.
+ createdRef.current = {};
setSavedImplementationState({
mode: "created",
@@ -640,34 +698,10 @@ export function CreateExperimentWizard({
let variantKeys = selectedFlag ? getVariantKeys(selectedFlag) : form.variants.map(variant => variant.key.trim());
if (form.assignmentMode === "new") {
- const variants: FeatureFlagVariant[] = form.variants.map(variant => ({
- key: variant.key.trim(),
- name: variant.name.trim() || undefined,
- rolloutPercentage: Number(variant.rolloutPercentage),
- }));
-
- const createdFlag = await createFeatureFlagMutation.mutateAsync({
- key: form.flagKey.trim(),
- description: form.flagDescription.trim() || `Assignment flag for ${form.name.trim()}`,
- enabled: true,
- runtime: "client",
- flagType: "multivariate",
- payload: null,
- variants: [],
- rolloutPercentage: 100,
- rules: [],
- conditionSets: [
- {
- name: "Default",
- rules: [],
- variants,
- },
- ],
- });
-
- featureFlagId = createdFlag.data.flagId;
- flagKey = createdFlag.data.key;
- variantKeys = getVariantKeys(createdFlag.data);
+ const createdFlag = await saveNewFlag();
+ featureFlagId = createdFlag.flagId;
+ flagKey = createdFlag.key;
+ variantKeys = getVariantKeys(createdFlag);
}
let primaryGoalId: number | null = form.goalMode === "existing" ? Number(form.existingGoalId) : null;
@@ -675,15 +709,7 @@ export function CreateExperimentWizard({
let goalLabel = form.goalMode === "none" ? undefined : (goalDisplayPattern(selectedGoal) ?? form.goalName.trim());
if (form.goalMode === "new") {
- const createdGoal = await createGoalMutation.mutateAsync({
- siteId: Number(site),
- name: form.goalName.trim() || `${form.name.trim()} conversion`,
- goalType: form.goalType,
- config:
- form.goalType === "path" ? { pathPattern: form.pathPattern.trim() } : { eventName: form.eventName.trim() },
- });
-
- primaryGoalId = createdGoal.goalId;
+ primaryGoalId = await saveNewGoal();
goalType = form.goalType;
goalLabel = form.goalType === "path" ? form.pathPattern.trim() : form.eventName.trim();
}
@@ -698,6 +724,7 @@ export function CreateExperimentWizard({
primaryGoalId,
},
});
+ createdRef.current = {};
setSavedImplementationState({
mode: "updated",
diff --git a/client/src/app/[site]/experiments/lib/experimentHelpers.test.ts b/client/src/app/[site]/experiments/lib/experimentHelpers.test.ts
new file mode 100644
index 000000000..627ebdee9
--- /dev/null
+++ b/client/src/app/[site]/experiments/lib/experimentHelpers.test.ts
@@ -0,0 +1,66 @@
+import { DateTime, Settings } from "luxon";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+
+import type { ExperimentVariantResult } from "@/api/analytics/endpoints";
+
+import { formatRelativeTime, getVariantConfidence } from "./experimentHelpers";
+
+const arm = (overrides: Partial<ExperimentVariantResult>): ExperimentVariantResult => ({
+ variant: "control",
+ sessions: 0,
+ exposures: 0,
+ conversions: 0,
+ conversionRate: 0,
+ lift: null,
+ isControl: false,
+ ...overrides,
+});
+
+describe("getVariantConfidence", () => {
+ const control = arm({ variant: "control", isControl: true, sessions: 100, exposures: 100, conversions: 3 });
+ control.conversionRate = control.conversions / control.sessions;
+
+ it("uses sessions, not exposures, as a zero-conversion arm's sample size", () => {
+ const variant = arm({ variant: "b", sessions: 100, exposures: 100 });
+ const repeatedExposures = arm({ variant: "b", sessions: 100, exposures: 5000 });
+
+ const baseline = getVariantConfidence(control, variant);
+ expect(baseline).not.toBeNull();
+ // Repeat flag evaluations in the same sessions add no evidence.
+ expect(getVariantConfidence(control, repeatedExposures)).toEqual(baseline);
+ expect(baseline!.isSignificant).toBe(false);
+ });
+
+ it("returns null until both arms have enough sessions", () => {
+ const variant = arm({ variant: "b", sessions: 20, exposures: 500 });
+ expect(getVariantConfidence(control, variant)).toBeNull();
+ });
+});
+
+describe("formatRelativeTime", () => {
+ const now = DateTime.fromISO("2026-10-24T12:00:00Z").toMillis();
+
+ beforeEach(() => {
+ Settings.now = () => now;
+ Settings.defaultZone = "America/New_York";
+ });
+
+ afterEach(() => {
+ Settings.now = () => Date.now();
+ Settings.defaultZone = "system";
+ });
+
+ it("parses Postgres timestamps as UTC", () => {
+ expect(formatRelativeTime("2026-10-24 09:30:00")).toBe("2 hours ago");
+ expect(formatRelativeTime("2026-10-24 09:30:00.123456")).toBe("2 hours ago");
+ });
+
+ it("still parses ISO timestamps", () => {
+ expect(formatRelativeTime("2026-10-24T09:30:00.000Z")).toBe("2 hours ago");
+ });
+
+ it("returns null for missing or unparseable values", () => {
+ expect(formatRelativeTime(null)).toBeNull();
+ expect(formatRelativeTime("not a date")).toBeNull();
+ });
+});
diff --git a/client/src/app/[site]/experiments/lib/experimentHelpers.ts b/client/src/app/[site]/experiments/lib/experimentHelpers.ts
index 20a45febd..26fc65a81 100644
--- a/client/src/app/[site]/experiments/lib/experimentHelpers.ts
+++ b/client/src/app/[site]/experiments/lib/experimentHelpers.ts
@@ -57,9 +57,12 @@ export function formatCompactNumber(value: number): string {
return `${(value / 1_000_000).toFixed(1)}M`;
}
-export function formatRelativeTime(iso: string | null): string | null {
- if (!iso) return null;
- const dt = DateTime.fromISO(iso);
+// Experiment timestamps come from Postgres `timestamp` columns as UTC wall time
+// ("2026-10-24 12:34:56.789"), which fromISO rejects; ISO strings still parse.
+export function formatRelativeTime(timestamp: string | null): string | null {
+ if (!timestamp) return null;
+ const sql = DateTime.fromSQL(timestamp, { zone: "utc" });
+ const dt = sql.isValid ? sql : DateTime.fromISO(timestamp, { zone: "utc" });
return dt.isValid ? dt.toRelative() : null;
}
@@ -83,16 +86,6 @@ function normalCdf(z: number): number {
return z >= 0 ? 1 - p : p;
}
-// Derive the denominator consistent with the displayed conversion rate so the
-// z-test never contradicts the rate shown to the user.
-function effectiveSampleSize(result: ExperimentVariantResult): number {
- if (result.conversionRate > 0) {
- const derived = Math.round(result.conversions / result.conversionRate);
- if (Number.isFinite(derived) && derived >= result.conversions) return derived;
- }
- return result.exposures;
-}
-
export type VariantConfidence = {
confidence: number; // two-sided, 0..1
isSignificant: boolean; // >= 0.95
@@ -108,8 +101,11 @@ export function getVariantConfidence(
): VariantConfidence | null {
if (!control || control.variant === variant.variant) return null;
- const nControl = effectiveSampleSize(control);
- const nVariant = effectiveSampleSize(variant);
+ // The displayed conversion rate is conversions / sessions, so sessions is the
+ // sample size. Exposures count every flag evaluation, repeats in a session
+ // included, and would overstate confidence (most visibly for 0-conversion arms).
+ const nControl = control.sessions;
+ const nVariant = variant.sessions;
if (nControl < MIN_SAMPLE_FOR_STATS || nVariant < MIN_SAMPLE_FOR_STATS) return null;
const pControl = control.conversions / nControl;
diff --git a/client/src/app/[site]/feature-flags/components/FeatureFlagDialog.test.tsx b/client/src/app/[site]/feature-flags/components/FeatureFlagDialog.test.tsx
new file mode 100644
index 000000000..5117047aa
--- /dev/null
+++ b/client/src/app/[site]/feature-flags/components/FeatureFlagDialog.test.tsx
@@ -0,0 +1,70 @@
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import type { FeatureFlag } from "@/api/analytics/endpoints";
+
+import { FeatureFlagDialog } from "./FeatureFlagDialog";
+
+vi.mock("next-intl", () => ({
+ useExtracted: () => (message: string, values?: Record<string, string | number>) =>
+ values ? message.replace(/\{(\w+)\}/g, (_, key) => String(values[key] ?? `{${key}}`)) : message,
+}));
+
+vi.mock("@/api/analytics/hooks/featureFlags/useFeatureFlags", () => ({
+ useCreateFeatureFlag: () => ({ mutateAsync: vi.fn(), isPending: false }),
+ useUpdateFeatureFlag: () => ({ mutateAsync: vi.fn(), isPending: false }),
+}));
+
+vi.mock("./ConditionSetsEditor", () => ({ ConditionSetsEditor: () => null }));
+vi.mock("@/components/CodeSnippet", () => ({ CodeSnippet: () => null }));
+vi.mock("@/components/ui/sonner", () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
+
+const flag: FeatureFlag = {
+ flagId: 42,
+ siteId: 1,
+ key: "new-checkout",
+ description: "Original",
+ enabled: true,
+ runtime: "client",
+ flagType: "boolean",
+ payload: null,
+ variants: [],
+ rolloutPercentage: 50,
+ rules: [],
+ conditionSets: [{ rules: [], rolloutPercentage: 50 }],
+ version: 3,
+ createdAt: "2026-09-01 10:00:00",
+ updatedAt: "2026-09-20 10:00:00",
+ stats: [],
+};
+
+const description = () => screen.getByLabelText("Description") as HTMLTextAreaElement;
+
+afterEach(() => {
+ cleanup();
+});
+
+describe("FeatureFlagDialog", () => {
+ it("keeps unsaved edits when the list refetches the same flag", () => {
+ const { rerender } = render(<FeatureFlagDialog flag={flag} open onOpenChange={() => {}} />);
+ fireEvent.change(description(), { target: { value: "Edited" } });
+
+ // A focus refetch hands back a new object for the same flag, with fresh stats.
+ rerender(<FeatureFlagDialog flag={{ ...flag, stats: [] }} open onOpenChange={() => {}} />);
+
+ expect(description().value).toBe("Edited");
+ });
+
+ it("loads another flag, and reloads on reopen", () => {
+ const { rerender } = render(<FeatureFlagDialog flag={flag} open onOpenChange={() => {}} />);
+ fireEvent.change(description(), { target: { value: "Edited" } });
+
+ rerender(<FeatureFlagDialog flag={{ ...flag, flagId: 43, description: "Other" }} open onOpenChange={() => {}} />);
+ expect(description().value).toBe("Other");
+
+ fireEvent.change(description(), { target: { value: "Edited again" } });
+ rerender(<FeatureFlagDialog flag={{ ...flag, flagId: 43, description: "Other" }} open={false} />);
+ rerender(<FeatureFlagDialog flag={{ ...flag, flagId: 43, description: "Other" }} open onOpenChange={() => {}} />);
+ expect(description().value).toBe("Other");
+ });
+});
diff --git a/client/src/app/[site]/feature-flags/components/FeatureFlagDialog.tsx b/client/src/app/[site]/feature-flags/components/FeatureFlagDialog.tsx
index 37a35b8f1..ae8d23aec 100644
--- a/client/src/app/[site]/feature-flags/components/FeatureFlagDialog.tsx
+++ b/client/src/app/[site]/feature-flags/components/FeatureFlagDialog.tsx
@@ -176,12 +176,17 @@ export function FeatureFlagDialog({
const isEditing = !!flag;
const isSaving = createMutation.isPending || updateMutation.isPending;
+ // Initialize when the dialog opens or switches to another flag, not on every new `flag` object: the
+ // list refetches (on focus, with fresh stats) while the dialog is open, and resetting then would wipe
+ // unsaved edits and flip the post-save snippet back to the form.
+ const flagId = flag?.flagId;
useEffect(() => {
if (open) {
setForm(toFormState(flag));
setView("form");
}
- }, [flag, open]);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [flagId, open]);
const updateField = <K extends keyof FlagFormState>(key: K, value: FlagFormState[K]) => {
setForm(current => ({ ...current, [key]: value }));
diff --git a/client/src/app/[site]/funnels/components/FunnelForm.tsx b/client/src/app/[site]/funnels/components/FunnelForm.tsx
index ff912ff0c..b6e49405d 100644
--- a/client/src/app/[site]/funnels/components/FunnelForm.tsx
+++ b/client/src/app/[site]/funnels/components/FunnelForm.tsx
@@ -458,17 +458,22 @@ export function FunnelForm({
newUseProperties[index] = enabled;
setUseProperties(newUseProperties);
- // Clear property fields if disabling
- if (!enabled) {
- const newSteps = [...steps];
+ // Disabling clears the step's filters but keeps the draft rows, so re-enabling shows them again; put
+ // them back on the step too, or the rows on screen would not be the filters that get saved. Rows
+ // without a key are fresh placeholders that never reached the step (see updateFilterRows).
+ const newSteps = [...steps];
+ if (enabled) {
+ const rows = (stepPropertyFilters[index] || []).filter(row => row.key !== "");
+ newSteps[index] = { ...newSteps[index], propertyFilters: rows.length ? rows : undefined };
+ } else {
newSteps[index] = {
...newSteps[index],
eventPropertyKey: undefined,
eventPropertyValue: undefined,
propertyFilters: undefined,
};
- setSteps(newSteps);
}
+ setSteps(newSteps);
};
// Update a step's property-filter draft rows; syncToStep is false when adding
diff --git a/client/src/app/[site]/globe/3d/hooks/timelineLayer/useTimelineSessions.test.tsx b/client/src/app/[site]/globe/3d/hooks/timelineLayer/useTimelineSessions.test.tsx
new file mode 100644
index 000000000..d9d3ae268
--- /dev/null
+++ b/client/src/app/[site]/globe/3d/hooks/timelineLayer/useTimelineSessions.test.tsx
@@ -0,0 +1,84 @@
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { cleanup, renderHook, waitFor } from "@testing-library/react";
+import type { ReactNode } from "react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import type { GetSessionsResponse } from "@/api/analytics/endpoints";
+
+import { useTimelineStore } from "../../../timelineStore";
+import { useTimelineSessions } from "./useTimelineSessions";
+
+const mocks = vi.hoisted(() => ({
+ site: "1",
+ fetchAnalytics: vi.fn<(site: string) => Promise<unknown>>(),
+}));
+
+vi.mock("@/api/analytics/useAnalyticsQuery", () => ({
+ useAnalyticsContext: () => ({ site: mocks.site, context: { timeZone: "UTC" } }),
+}));
+
+vi.mock("@/api/analytics/analyticsRequest", () => ({
+ buildAnalyticsRequest: () => ({ path: "sessions", params: {} }),
+ fetchAnalytics: (site: string) => mocks.fetchAnalytics(site),
+}));
+
+vi.mock("@/lib/store", () => ({ getFilteredFilters: () => [] }));
+
+const sessions = [
+ { session_id: "a", session_start: "2026-10-24 10:00:00", session_end: "2026-10-24 10:05:00" },
+ { session_id: "b", session_start: "2026-10-24 11:00:00", session_end: "2026-10-24 11:30:00" },
+] as unknown as GetSessionsResponse;
+
+function renderTimeline() {
+ const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
+ const wrapper = ({ children }: { children: ReactNode }) => (
+ <QueryClientProvider client={client}>{children}</QueryClientProvider>
+ );
+ return renderHook(() => useTimelineSessions(), { wrapper });
+}
+
+beforeEach(() => {
+ mocks.site = "1";
+ mocks.fetchAnalytics.mockReset();
+ useTimelineStore.getState().reset();
+});
+
+afterEach(() => {
+ cleanup();
+});
+
+describe("useTimelineSessions", () => {
+ it("does not keep the previous site's sessions when the next site has none", async () => {
+ mocks.fetchAnalytics.mockImplementation(async site => (site === "1" ? sessions : []));
+ const { rerender } = renderTimeline();
+ await waitFor(() => expect(useTimelineStore.getState().allSessions).toHaveLength(2));
+ expect(useTimelineStore.getState().timeRange).not.toBeNull();
+
+ mocks.site = "2";
+ rerender();
+ await waitFor(() => expect(mocks.fetchAnalytics).toHaveBeenCalledWith("2"));
+ await waitFor(() => expect(useTimelineStore.getState().isLoading).toBe(false));
+
+ const state = useTimelineStore.getState();
+ expect(state.allSessions).toHaveLength(0);
+ expect(state.timeRange).toBeNull();
+ expect(state.currentTime).toBeNull();
+ });
+
+ it("clears the old sessions while the next site loads", async () => {
+ let resolveNext: (value: unknown) => void = () => {};
+ mocks.fetchAnalytics.mockImplementation(site =>
+ site === "1" ? Promise.resolve(sessions) : new Promise(resolve => (resolveNext = resolve))
+ );
+ const { rerender } = renderTimeline();
+ await waitFor(() => expect(useTimelineStore.getState().allSessions).toHaveLength(2));
+
+ mocks.site = "2";
+ rerender();
+ await waitFor(() => expect(useTimelineStore.getState().isLoading).toBe(true));
+ expect(useTimelineStore.getState().allSessions).toHaveLength(0);
+
+ resolveNext(sessions.slice(0, 1));
+ await waitFor(() => expect(useTimelineStore.getState().allSessions).toHaveLength(1));
+ });
+});
diff --git a/client/src/app/[site]/globe/3d/hooks/timelineLayer/useTimelineSessions.ts b/client/src/app/[site]/globe/3d/hooks/timelineLayer/useTimelineSessions.ts
index b01af9b2d..058397df6 100644
--- a/client/src/app/[site]/globe/3d/hooks/timelineLayer/useTimelineSessions.ts
+++ b/client/src/app/[site]/globe/3d/hooks/timelineLayer/useTimelineSessions.ts
@@ -12,16 +12,18 @@ import { calculateWindowSize } from "../../../timelineUtils";
import { MAX_PAGES, PAGE_SIZE } from "./timelineLayerConstants";
export function useTimelineSessions() {
- const { manualWindowSize, setTimeRange, setWindowSize, setAllSessions, setLoading, setError } = useTimelineStore(
- useShallow(s => ({
- manualWindowSize: s.manualWindowSize,
- setTimeRange: s.setTimeRange,
- setWindowSize: s.setWindowSize,
- setAllSessions: s.setAllSessions,
- setLoading: s.setLoading,
- setError: s.setError,
- }))
- );
+ const { manualWindowSize, setTimeRange, setWindowSize, setAllSessions, clearSessions, setLoading, setError } =
+ useTimelineStore(
+ useShallow(s => ({
+ manualWindowSize: s.manualWindowSize,
+ setTimeRange: s.setTimeRange,
+ setWindowSize: s.setWindowSize,
+ setAllSessions: s.setAllSessions,
+ clearSessions: s.clearSessions,
+ setLoading: s.setLoading,
+ setError: s.setError,
+ }))
+ );
const filteredFilters = getFilteredFilters(SESSION_PAGE_FILTERS);
const { site, context } = useAnalyticsContext({
useFilters: filteredFilters.length > 0,
@@ -72,12 +74,16 @@ export function useTimelineSessions() {
setError(isError);
}, [isError, setError]);
- // Update store with allSessions
+ // Mirror this scope's result into the store. The store outlives the query key, so anything but a
+ // non-empty result for the current site/range/filters (still loading, failed, or successfully empty)
+ // clears it; otherwise the previous scope's sessions and markers would stay on the globe.
useEffect(() => {
if (allSessions.length > 0) {
setAllSessions(allSessions, data?.hasMoreData || false);
+ } else {
+ clearSessions();
}
- }, [allSessions, data?.hasMoreData, setAllSessions]);
+ }, [allSessions, data?.hasMoreData, setAllSessions, clearSessions]);
// Calculate time range from fetched sessions and initialize timeline
useEffect(() => {
diff --git a/client/src/app/[site]/globe/timelineStore.ts b/client/src/app/[site]/globe/timelineStore.ts
index 26eca5dff..5f794b0aa 100644
--- a/client/src/app/[site]/globe/timelineStore.ts
+++ b/client/src/app/[site]/globe/timelineStore.ts
@@ -19,6 +19,7 @@ interface TimelineStore {
setWindowSize: (size: number) => void;
setManualWindowSize: (size: number | null) => void;
setAllSessions: (sessions: GetSessionsResponse, hasMoreData: boolean) => void;
+ clearSessions: () => void;
setLoading: (loading: boolean) => void;
setError: (error: boolean) => void;
reset: () => void;
@@ -38,6 +39,8 @@ export const useTimelineStore = create<TimelineStore>(set => ({
setWindowSize: size => set({ windowSize: size }),
setManualWindowSize: size => set({ manualWindowSize: size, windowSize: size || 60 }),
setAllSessions: (sessions, hasMoreData) => set({ allSessions: sessions, hasMoreData }),
+ // Drops the loaded sessions and the timeline built from them, keeping the user's window size.
+ clearSessions: () => set({ allSessions: [], hasMoreData: false, timeRange: null, currentTime: null }),
setLoading: loading => set({ isLoading: loading }),
setError: error => set({ isError: error }),
reset: () =>
diff --git a/client/src/app/[site]/main/components/MainSection/Overview.tsx b/client/src/app/[site]/main/components/MainSection/Overview.tsx
index fb7be672d..e06a35d0c 100644
--- a/client/src/app/[site]/main/components/MainSection/Overview.tsx
+++ b/client/src/app/[site]/main/components/MainSection/Overview.tsx
@@ -9,6 +9,7 @@ import { percentDelta } from "@/lib/delta";
import { cn, formatSecondsAsMinutesAndSeconds } from "@/lib/utils";
import NumberFlow from "@number-flow/react";
import { AlertCircle, ChevronDown, ChevronUp, RefreshCcw } from "lucide-react";
+import { DateTime } from "luxon";
import { useExtracted } from "next-intl";
import { useState } from "react";
import { useGetOverview } from "../../../../../api/analytics/hooks/useGetOverview";
@@ -68,6 +69,7 @@ const Stat = ({
flash: FlashInputs;
}) => {
const { selectedStat, setSelectedStat, site, bucket, time } = useStore();
+ const timeZone = useTimezone();
const [isHovering, setIsHovering] = useState(false);
const displayValue = decimals ? Number(value.toFixed(decimals)) : value;
@@ -83,9 +85,10 @@ const Stat = ({
?.filter(d => {
// For past-minutes mode, ensure we only show data within the specified time range
if (time.mode === "past-minutes") {
- const timestamp = new Date(d.time);
- const now = new Date();
- const startTime = new Date(now.getTime() - time.pastMinutesStart * 60 * 1000);
+ // Bucket times are in the dashboard's timezone; the browser's would shift the window.
+ const timestamp = DateTime.fromSQL(d.time, { zone: timeZone });
+ const now = DateTime.now();
+ const startTime = now.minus({ minutes: time.pastMinutesStart });
return timestamp >= startTime && timestamp <= now;
}
return true;
diff --git a/client/src/app/[site]/main/components/MainSection/OverviewLite.tsx b/client/src/app/[site]/main/components/MainSection/OverviewLite.tsx
index 56d918a09..0d717cea1 100644
--- a/client/src/app/[site]/main/components/MainSection/OverviewLite.tsx
+++ b/client/src/app/[site]/main/components/MainSection/OverviewLite.tsx
@@ -4,11 +4,12 @@ import { Skeleton } from "@/components/ui/skeleton";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { cn, formatSecondsAsMinutesAndSeconds } from "@/lib/utils";
import NumberFlow from "@number-flow/react";
+import { DateTime } from "luxon";
import { useExtracted } from "next-intl";
import { useState } from "react";
import { useGetOverview } from "../../../../../api/analytics/hooks/useGetOverview";
import { useGetOverviewBucketed } from "../../../../../api/analytics/hooks/useGetOverviewBucketed";
-import { StatType, useStore } from "../../../../../lib/store";
+import { StatType, useStore, useTimezone } from "../../../../../lib/store";
import { SparklinesChart } from "./SparklinesChart";
// Lite variant of Overview: no previous-period queries, no comparison arrows.
@@ -47,6 +48,7 @@ const Stat = ({
postfix?: string;
}) => {
const { selectedStat, setSelectedStat, site, bucket, time } = useStore();
+ const timeZone = useTimezone();
const [isHovering, setIsHovering] = useState(false);
const isSelectable = id !== "total_time";
@@ -56,9 +58,10 @@ const Stat = ({
data
?.filter(d => {
if (time.mode === "past-minutes") {
- const timestamp = new Date(d.time);
- const now = new Date();
- const startTime = new Date(now.getTime() - time.pastMinutesStart * 60 * 1000);
+ // Bucket times are in the dashboard's timezone; the browser's would shift the window.
+ const timestamp = DateTime.fromSQL(d.time, { zone: timeZone });
+ const now = DateTime.now();
+ const startTime = now.minus({ minutes: time.pastMinutesStart });
return timestamp >= startTime && timestamp <= now;
}
return true;
diff --git a/client/src/app/[site]/main/components/MainSection/SparklinesChart.tsx b/client/src/app/[site]/main/components/MainSection/SparklinesChart.tsx
index 875c56b77..594ef3320 100644
--- a/client/src/app/[site]/main/components/MainSection/SparklinesChart.tsx
+++ b/client/src/app/[site]/main/components/MainSection/SparklinesChart.tsx
@@ -1,5 +1,6 @@
"use client";
import { useNivoTheme } from "@/lib/nivo";
+import { useTimezone } from "@/lib/store";
import { ResponsiveLine } from "@nivo/line";
import { DateTime } from "luxon";
@@ -11,18 +12,22 @@ export function SparklinesChart({
isHovering: boolean;
}) {
const nivoTheme = useNivoTheme();
+ const timeZone = useTimezone();
const formattedData = data
- ?.map((e, i) => {
+ ?.map(e => {
+ // Buckets are wall-clock times in the dashboard's timezone, not the browser's.
+ const time = DateTime.fromSQL(e.time, { zone: timeZone });
+
// filter out dates from the future
- if (DateTime.fromSQL(e.time).toUTC() > DateTime.now()) {
+ if (time > DateTime.now()) {
return null;
}
return {
- x: DateTime.fromSQL(e.time).toUTC().toFormat("yyyy-MM-dd HH:mm:ss"),
+ x: time.toUTC().toFormat("yyyy-MM-dd HH:mm:ss"),
y: e.value,
- currentTime: DateTime.fromSQL(e.time),
+ currentTime: time,
};
})
.filter(e => e !== null);
diff --git a/client/src/app/[site]/users/components/UsersTable.test.tsx b/client/src/app/[site]/users/components/UsersTable.test.tsx
new file mode 100644
index 000000000..9301a79bf
--- /dev/null
+++ b/client/src/app/[site]/users/components/UsersTable.test.tsx
@@ -0,0 +1,87 @@
+import { act, cleanup, render } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { useStore } from "@/lib/store";
+
+type ListState = {
+ data?: { data: never[]; totalCount: number };
+ isFetching: boolean;
+ isPlaceholderData: boolean;
+ isError: boolean;
+};
+
+const mocks = vi.hoisted(() => ({
+ // The users query's state for each page; the table asks for one page at a time.
+ pages: {} as Record<number, ListState>,
+ requestedPage: 0,
+ onPageChange: (_page: number) => {},
+}));
+
+vi.mock("next-intl", () => ({ useExtracted: () => (message: string) => message }));
+vi.mock("@/api/analytics/hooks/useGetUsers", () => ({
+ useGetUsers: ({ page }: { page: number }) => {
+ mocks.requestedPage = page;
+ return { ...mocks.pages[page], refetch: () => {} };
+ },
+}));
+vi.mock("@/api/analytics/hooks/useGetUserTraits", () => ({
+ useGetUserTraitKeys: () => ({ data: { keys: [] }, isError: false }),
+ useGetUserTraitBreakdown: () => ({ data: undefined, isError: false, refetch: () => {} }),
+}));
+vi.mock("@/components/pagination", () => ({
+ Pagination: ({ onPageChange }: { onPageChange: (page: number) => void }) => {
+ mocks.onPageChange = onPageChange;
+ return null;
+ },
+}));
+vi.mock("./ColumnPicker", () => ({ ColumnPicker: () => null }));
+vi.mock("./UserRow", () => ({ UserRow: () => null }));
+
+import { UsersTable } from "./UsersTable";
+
+const settled = (totalCount: number): ListState => ({
+ data: { data: [], totalCount },
+ isFetching: false,
+ isPlaceholderData: false,
+ isError: false,
+});
+
+const table = <UsersTable breakdown={null} narrowing={{}} ready comparisonEnabled={false} />;
+
+beforeEach(() => {
+ useStore.setState({ site: "1", filters: [] });
+});
+afterEach(cleanup);
+
+describe("UsersTable pagination", () => {
+ it("does not step back on a cached total while the page it was asked for is refetching", () => {
+ // 50 users a page. Page 3 was last seen when the period had 100 users; it has 150 now.
+ mocks.pages = { 1: settled(150), 3: { ...settled(100), isFetching: true } };
+ const view = render(table);
+ act(() => mocks.onPageChange(3));
+ expect(mocks.requestedPage).toBe(3);
+
+ mocks.pages[3] = settled(150);
+ view.rerender(table);
+ expect(mocks.requestedPage).toBe(3);
+ });
+
+ it("steps back to the last page with rows once the list has settled smaller", () => {
+ mocks.pages = { 1: settled(150), 2: settled(100), 3: settled(100) };
+ render(table);
+ act(() => mocks.onPageChange(3));
+ expect(mocks.requestedPage).toBe(2);
+ });
+
+ it("starts every scope on page one, including one returned to", () => {
+ mocks.pages = { 1: settled(500), 4: settled(500) };
+ render(table);
+ act(() => mocks.onPageChange(4));
+ expect(mocks.requestedPage).toBe(4);
+
+ const chrome = { parameter: "browser" as const, type: "equals" as const, value: ["Chrome"] };
+ act(() => useStore.setState({ filters: [chrome] }));
+ expect(mocks.requestedPage).toBe(1);
+ act(() => useStore.setState({ filters: [] }));
+ expect(mocks.requestedPage).toBe(1);
+ });
+});
diff --git a/client/src/app/[site]/users/components/UsersTable.tsx b/client/src/app/[site]/users/components/UsersTable.tsx
index a12c348c2..8a31fbbdb 100644
--- a/client/src/app/[site]/users/components/UsersTable.tsx
+++ b/client/src/app/[site]/users/components/UsersTable.tsx
@@ -13,7 +13,7 @@ import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Skeleton } from "@/components/ui/skeleton";
-import { useStore } from "@/lib/store";
+import { useStore, useTimezone } from "@/lib/store";
import { cn } from "@/lib/utils";
import {
BuiltInColumn,
@@ -120,6 +120,9 @@ export function UsersTable({
const t = useExtracted();
const site = useStore(state => state.site);
const privateKey = useStore(state => state.privateKey);
+ const time = useStore(state => state.time);
+ const filters = useStore(state => state.filters);
+ const timeZone = useTimezone();
const [searchTerm, setSearchTerm] = useState("");
const [searchField, setSearchField] = useState<SearchField>("name");
@@ -141,9 +144,13 @@ export function UsersTable({
const sortBy = sort.column;
const sortOrder = sort.descending ? "desc" : "asc";
- // Anything that changes which users are listed sends the pager back to page one.
- const scope = JSON.stringify([fullNarrowing, sortBy, sortOrder, pageSize]);
+ // Anything that changes which users are listed sends the pager back to page one, and keeps it there:
+ // returning to an earlier scope starts on page one too, not on the page it was left at.
+ const scope = JSON.stringify([site, time, filters, timeZone, fullNarrowing, sortBy, sortOrder, pageSize]);
const page = pageState.scope === scope ? pageState.page : 1;
+ if (pageState.scope !== scope) {
+ setPageState({ scope, page: 1 });
+ }
const list = useGetUsers({
...fullNarrowing,
@@ -258,6 +265,14 @@ export function UsersTable({
const users = list.data?.data ?? [];
const total = isGrouped ? current?.totals?.users : list.data?.totalCount;
const searchLimited = isGrouped ? current?.searchLimited : list.data?.searchLimited;
+ const pageCount = Math.max(Math.ceil((list.data?.totalCount ?? 0) / pageSize), 1);
+
+ // A refetch can shrink the list under the current page (a rolling window losing users): step back to the
+ // last page that has rows instead of showing an empty one. Only once this page's query has settled: a
+ // cached total from an earlier visit may be stale, and clamping on it would cancel the page's own fetch.
+ if (!isGrouped && list.data && !list.isPlaceholderData && !list.isFetching && page > pageCount) {
+ setPageState({ scope, page: pageCount });
+ }
const summary =
total === undefined || isLoading
@@ -466,7 +481,7 @@ export function UsersTable({
<Pagination
page={page}
pageSize={pageSize}
- pageCount={Math.max(Math.ceil((list.data?.totalCount ?? 0) / pageSize), 1)}
+ pageCount={pageCount}
totalItems={list.data?.totalCount ?? 0}
onPageChange={next => setPageState({ scope, page: next })}
onPageSizeChange={setPageSize}
diff --git a/client/src/app/as/callback/page.test.tsx b/client/src/app/as/callback/page.test.tsx
new file mode 100644
index 000000000..7036bc239
--- /dev/null
+++ b/client/src/app/as/callback/page.test.tsx
@@ -0,0 +1,54 @@
+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ user: { id: "user-1" } as { id: string } | null,
+ createOrganization: vi.fn(),
+ fetch: vi.fn(),
+}));
+
+vi.mock("next/navigation", () => ({
+ useRouter: () => ({ push: vi.fn() }),
+ useSearchParams: () => new URLSearchParams("code=one-use-code"),
+}));
+vi.mock("@/lib/auth", () => ({
+ authClient: {
+ useActiveOrganization: () => ({ data: null, isPending: false }),
+ organization: { create: mocks.createOrganization, setActive: vi.fn().mockResolvedValue({}) },
+ },
+}));
+vi.mock("@/lib/userStore", () => ({
+ userStore: Object.assign(() => ({ user: mocks.user, isPending: false }), { setState: vi.fn() }),
+}));
+vi.mock("@/hooks/useSetPageTitle", () => ({ useSetPageTitle: () => {} }));
+vi.mock("@/components/RybbitLogo", () => ({ RybbitTextLogo: () => null }));
+vi.mock("@/components/auth/Turnstile", () => ({ Turnstile: () => null }));
+
+import AppSumoSignupPage from "./page";
+
+beforeEach(() => {
+ mocks.createOrganization.mockReset().mockResolvedValue({ data: { id: "org-1" }, error: null });
+ // The code is single-use: the first activation succeeds, any later one is refused.
+ mocks.fetch.mockReset().mockResolvedValueOnce({ ok: true, json: async () => ({}) });
+ mocks.fetch.mockResolvedValue({ ok: false, status: 409, json: async () => ({ error: "License already activated" }) });
+ vi.stubGlobal("fetch", mocks.fetch);
+});
+afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+});
+
+describe("AppSumo activation for a signed-in user without an organization", () => {
+ it("activates the code once and reports success", async () => {
+ render(<AppSumoSignupPage />);
+ fireEvent.change(await screen.findByLabelText("Organization Name"), { target: { value: "Acme" } });
+ await act(async () => fireEvent.click(screen.getByRole("button", { name: /Continue & Activate License/ })));
+
+ expect(await screen.findByText("Add your site (optional)")).toBeTruthy();
+ // Let the existing-user effect, which now sees the new organization, run its course.
+ await act(async () => {});
+ expect(mocks.fetch).toHaveBeenCalledOnce();
+ expect(mocks.fetch.mock.calls[0][0]).toMatch(/\/as\/activate$/);
+ expect(screen.queryByText(/License activation failed/)).toBeNull();
+ });
+});
diff --git a/client/src/app/as/callback/page.tsx b/client/src/app/as/callback/page.tsx
index f970cf061..052c0d5e8 100644
--- a/client/src/app/as/callback/page.tsx
+++ b/client/src/app/as/callback/page.tsx
@@ -11,14 +11,15 @@ import { Label } from "@/components/ui/label";
import { ArrowRight, Check, Loader2 } from "lucide-react";
import Link from "next/link";
import { useRouter, useSearchParams } from "next/navigation";
-import React, { Suspense, useEffect, useState } from "react";
+import React, { Suspense, useEffect, useRef, useState } from "react";
import { addSite } from "../../../api/admin/endpoints";
import { RybbitTextLogo } from "../../../components/RybbitLogo";
import { useSetPageTitle } from "../../../hooks/useSetPageTitle";
import { authClient } from "../../../lib/auth";
-import { IS_CLOUD } from "../../../lib/const";
+import { BACKEND_URL, IS_CLOUD } from "../../../lib/const";
import { userStore } from "../../../lib/userStore";
import { cn, isValidDomain, normalizeDomain } from "../../../lib/utils";
+import { slugFromName } from "../../signup/orgSlug";
// Client component to handle AppSumo code from URL params
function AppSumoCodeHandler({
@@ -64,7 +65,6 @@ export default function AppSumoSignupPage() {
// Step 2: Organization creation
const [orgName, setOrgName] = useState("");
- const [orgSlug, setOrgSlug] = useState("");
// Step 3: Website addition
const [organizationId, setOrganizationId] = useState("");
@@ -117,24 +117,25 @@ export default function AppSumoSignupPage() {
activateForExistingUser();
}, [isExistingUser, appsumoCode, organizationId, licenseActivated, isInitializing]);
- // Handle organization name change and generate slug
- const handleOrgNameChange = (value: string) => {
- setOrgName(value);
- if (value) {
- const generatedSlug = value
- .toLowerCase()
- .replace(/\s+/g, "-")
- .replace(/[^a-z0-9-]/g, "");
- setOrgSlug(generatedSlug);
- }
- };
+ // A signed-in user without an organization creates one here, and then both the submit
+ // handler and the existing-user effect reach activation. The code is single-use, so they
+ // share one request: a second one would fail and report the successful activation as failed.
+ const activation = useRef<{ key: string; result: Promise<boolean> } | null>(null);
// Helper function to activate AppSumo license for an organization
- const activateLicenseForOrg = async (orgId: string, code: string): Promise<boolean> => {
- if (!code) return true; // No code to activate, consider it success
+ const activateLicenseForOrg = (orgId: string, code: string): Promise<boolean> => {
+ if (!code) return Promise.resolve(true); // No code to activate, consider it success
+
+ const key = `${orgId}:${code}`;
+ if (activation.current?.key !== key) {
+ activation.current = { key, result: requestLicenseActivation(orgId, code) };
+ }
+ return activation.current.result;
+ };
+ const requestLicenseActivation = async (orgId: string, code: string): Promise<boolean> => {
try {
- const response = await fetch("/api/as/activate", {
+ const response = await fetch(`${BACKEND_URL}/as/activate`, {
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -146,24 +147,17 @@ export default function AppSumoSignupPage() {
}),
});
- // if (!response.ok) {
- // const errorData = await response.json();
-
- // // If license is already activated, treat it as success (user can proceed)
- // if (response.status === 409 && errorData.error === "License already activated") {
- // console.log("AppSumo license was already activated");
- // return true;
- // }
-
- // throw new Error(errorData.error || "Failed to activate AppSumo license");
- // }
+ if (!response.ok) {
+ // The server names the failure in `error`, with detail in `message` for some.
+ const errorData = await response.json().catch(() => ({}));
+ throw new Error([errorData.error, errorData.message].filter(Boolean).join(": ") || `HTTP ${response.status}`);
+ }
- // const licenseData = await response.json();
- // console.log("AppSumo license activated:", licenseData);
return true;
} catch (licenseError) {
console.error("Error activating AppSumo license:", licenseError);
- setError("License activation failed. Please contact support with your license key.");
+ const reason = licenseError instanceof Error ? licenseError.message : String(licenseError);
+ setError(`License activation failed (${reason}). Please contact support with your license key.`);
return false;
}
};
@@ -222,7 +216,7 @@ export default function AppSumoSignupPage() {
// Create organization
const { data, error } = await authClient.organization.create({
name: orgName,
- slug: orgSlug,
+ slug: slugFromName(orgName),
});
if (error) {
@@ -370,7 +364,7 @@ export default function AppSumoSignupPage() {
type="text"
placeholder="Acme Inc."
value={orgName}
- onChange={e => handleOrgNameChange(e.target.value)}
+ onChange={e => setOrgName(e.target.value)}
required
className="h-10 transition-all bg-neutral-100 dark:bg-neutral-800/50 border-neutral-200 dark:border-neutral-700"
/>
@@ -379,7 +373,7 @@ export default function AppSumoSignupPage() {
<Button
className="w-full transition-all duration-300 h-11 bg-emerald-600 hover:bg-emerald-500 text-white"
onClick={handleOrganizationSubmit}
- disabled={isLoading || !orgName || !orgSlug}
+ disabled={isLoading || !orgName}
variant="success"
>
Continue & Activate License
diff --git a/client/src/app/rollup/page.tsx b/client/src/app/rollup/page.tsx
index 51045b861..5e14c0948 100644
--- a/client/src/app/rollup/page.tsx
+++ b/client/src/app/rollup/page.tsx
@@ -115,6 +115,15 @@ export default function RollupPage() {
const [selectedTeamFilter, setSelectedTeamFilter] = useState<string>("all");
const [selectedTags, setSelectedTags] = useState<string[]>([]);
+ // Teams and tags belong to one organization: switching keeps this page mounted,
+ // and a stale filter would hide every site of the new one.
+ const [filtersOrganizationId, setFiltersOrganizationId] = useState(activeOrganization?.id);
+ if (activeOrganization?.id && activeOrganization.id !== filtersOrganizationId) {
+ setFiltersOrganizationId(activeOrganization.id);
+ setSelectedTeamFilter("all");
+ setSelectedTags([]);
+ }
+
// Track what the user switched OFF rather than on: narrowing by team or tag
// then shows every site in the new set, and widening again brings back
// exactly what they had.
diff --git a/client/src/app/signup/components/SetupStep.tsx b/client/src/app/signup/components/SetupStep.tsx
index 86eac5d83..71e82657e 100644
--- a/client/src/app/signup/components/SetupStep.tsx
+++ b/client/src/app/signup/components/SetupStep.tsx
@@ -12,7 +12,6 @@ interface SetupStepProps {
domain: string;
setDomain: (v: string) => void;
orgName: string;
- orgSlug: string;
handleOrgNameChange: (v: string) => void;
referralSource: string;
setReferralSource: (v: string) => void;
@@ -24,7 +23,6 @@ export function SetupStep({
domain,
setDomain,
orgName,
- orgSlug,
handleOrgNameChange,
referralSource,
setReferralSource,
@@ -32,7 +30,7 @@ export function SetupStep({
onSubmit,
}: SetupStepProps) {
const t = useExtracted();
- const canSubmit = !!orgName && !!orgSlug && !!domain && isValidDomain(domain) && !(IS_CLOUD && !referralSource);
+ const canSubmit = !!orgName && !!domain && isValidDomain(domain) && !(IS_CLOUD && !referralSource);
return (
<div>
diff --git a/client/src/app/signup/orgSlug.test.ts b/client/src/app/signup/orgSlug.test.ts
new file mode 100644
index 000000000..85d26d4b8
--- /dev/null
+++ b/client/src/app/signup/orgSlug.test.ts
@@ -0,0 +1,30 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { slugFromName } from "./orgSlug";
+
+afterEach(() => {
+ vi.restoreAllMocks();
+});
+
+describe("slugFromName", () => {
+ it("keeps the name's letters and digits and adds a random suffix", () => {
+ vi.spyOn(Math, "random").mockReturnValue(0.123456789);
+
+ expect(slugFromName("Acme Inc.")).toBe(`acme-inc-${(0.123456789).toString(36).slice(2, 6)}`);
+ });
+
+ it("gives the same name a different slug each time, so common names don't collide", () => {
+ vi.spyOn(Math, "random").mockReturnValueOnce(0.111111).mockReturnValueOnce(0.999999);
+
+ expect(slugFromName("Test")).not.toBe(slugFromName("Test"));
+ });
+
+ it("is never empty, even for a name with no ASCII letters or digits", () => {
+ expect(slugFromName("桋试公司")).toMatch(/^org-[a-z0-9]+$/);
+ expect(slugFromName("ζ ͺ式会瀾 γƒ©γƒ“γƒƒγƒˆ")).toMatch(/^org-[a-z0-9]+$/);
+ });
+
+ it("collapses punctuation and trims leading and trailing separators", () => {
+ expect(slugFromName(" --Hello, World!-- ")).toMatch(/^hello-world-[a-z0-9]+$/);
+ });
+});
diff --git a/client/src/app/signup/orgSlug.ts b/client/src/app/signup/orgSlug.ts
new file mode 100644
index 000000000..6d8bb8664
--- /dev/null
+++ b/client/src/app/signup/orgSlug.ts
@@ -0,0 +1,13 @@
+/**
+ * The slug for a new organization named `name`. Slugs are unique across every
+ * organization, so a bare "test" collides with someone else's, and a name with
+ * no ASCII letters or digits ("ζ΅‹θ―•") strips to nothing. A fallback base and a
+ * short random suffix keep it valid; the display name is saved as typed.
+ */
+export function slugFromName(name: string): string {
+ const base = name
+ .toLowerCase()
+ .replace(/[^a-z0-9]+/g, "-")
+ .replace(/^-+|-+$/g, "");
+ return `${base || "org"}-${Math.random().toString(36).slice(2, 6)}`;
+}
diff --git a/client/src/app/signup/page.test.tsx b/client/src/app/signup/page.test.tsx
new file mode 100644
index 000000000..08a50783c
--- /dev/null
+++ b/client/src/app/signup/page.test.tsx
@@ -0,0 +1,103 @@
+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { NuqsTestingAdapter } from "nuqs/adapters/testing";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+type Session = { data: { session: { activeOrganizationId: string | null } } | null; isPending: boolean };
+type Subscription = { data?: { trialEligible?: boolean }; isLoading: boolean };
+
+const mocks = vi.hoisted(() => ({
+ session: { data: null, isPending: true } as Session,
+ subscription: { isLoading: false } as Subscription,
+ subscriptionOrganizationId: undefined as string | undefined,
+ fetch: vi.fn(),
+}));
+
+vi.mock("next-intl", () => ({ useExtracted: () => (message: string) => message }));
+vi.mock("next/navigation", () => ({ useRouter: () => ({ push: vi.fn() }) }));
+vi.mock("@/lib/const", async importOriginal => ({ ...(await importOriginal<object>()), IS_CLOUD: true }));
+vi.mock("@/lib/auth", () => ({
+ authClient: {
+ useSession: () => mocks.session,
+ getSession: () => new Promise(() => {}),
+ organization: { create: vi.fn(), setActive: vi.fn() },
+ },
+}));
+vi.mock("@/lib/configs", () => ({ useConfigs: () => ({ configs: {}, isLoading: false }) }));
+vi.mock("@/lib/subscription/useStripeSubscription", () => ({
+ useStripeSubscription: (organizationId?: string) => {
+ mocks.subscriptionOrganizationId = organizationId;
+ return organizationId ? mocks.subscription : { isLoading: false };
+ },
+}));
+vi.mock("@/hooks/useSetPageTitle", () => ({ useSetPageTitle: () => {} }));
+vi.mock("@/components/RybbitLogo", () => ({ RybbitLogo: () => null, RybbitTextLogo: () => null }));
+vi.mock("@/components/subscription/components/CheckoutModal", () => ({ CheckoutModal: () => null }));
+// Radix's slider measures itself with ResizeObserver, which jsdom lacks.
+vi.mock("@/components/ui/slider", () => ({ Slider: () => null }));
+vi.mock("./components/SetupStep", () => ({ SetupStep: () => <div data-testid="setup-step" /> }));
+
+import SignupPage from "./page";
+
+function renderPlanStepAfterReload() {
+ return render(
+ <NuqsTestingAdapter searchParams="?step=3">
+ <SignupPage />
+ </NuqsTestingAdapter>
+ );
+}
+
+const checkoutButton = () =>
+ screen.getByRole("button", { name: /Loading\.\.\.|Start free trial|Continue to checkout/ });
+
+beforeEach(() => {
+ mocks.session = { data: null, isPending: true };
+ mocks.subscription = { isLoading: false };
+ mocks.fetch.mockReset().mockResolvedValue({ ok: true, json: async () => ({ clientSecret: "secret" }) });
+ vi.stubGlobal("fetch", mocks.fetch);
+});
+afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+});
+
+describe("signup plan step after a reload", () => {
+ it("waits for the session instead of sending the visitor back to create another organization", () => {
+ const view = renderPlanStepAfterReload();
+ expect((checkoutButton() as HTMLButtonElement).disabled).toBe(true);
+ fireEvent.click(checkoutButton());
+ expect(screen.queryByTestId("setup-step")).toBeNull();
+
+ mocks.session = { data: { session: { activeOrganizationId: "org-1" } }, isPending: false };
+ mocks.subscription = { data: { trialEligible: true }, isLoading: false };
+ view.rerender(
+ <NuqsTestingAdapter searchParams="?step=3">
+ <SignupPage />
+ </NuqsTestingAdapter>
+ );
+ expect(mocks.subscriptionOrganizationId).toBe("org-1");
+ expect((checkoutButton() as HTMLButtonElement).disabled).toBe(false);
+ });
+
+ it("offers no trial to a restored organization that already had one, and checks out that organization", async () => {
+ mocks.session = { data: { session: { activeOrganizationId: "org-1" } }, isPending: false };
+ mocks.subscription = { data: { trialEligible: false }, isLoading: false };
+ renderPlanStepAfterReload();
+
+ expect(screen.queryByText("Start your 7-day free trial")).toBeNull();
+ expect(screen.getByText("You'll be charged when you subscribe. You can cancel anytime.")).toBeTruthy();
+ expect(checkoutButton().textContent).toContain("Continue to checkout");
+
+ await act(async () => fireEvent.click(checkoutButton()));
+ expect(mocks.fetch).toHaveBeenCalledOnce();
+ expect(JSON.parse(mocks.fetch.mock.calls[0][1].body)).toMatchObject({ organizationId: "org-1" });
+ });
+
+ it("still sends a visitor with no organization back to the setup step", () => {
+ mocks.session = { data: { session: { activeOrganizationId: null } }, isPending: false };
+ renderPlanStepAfterReload();
+ expect(screen.getByText("Start your 7-day free trial")).toBeTruthy();
+
+ fireEvent.click(checkoutButton());
+ expect(screen.getByTestId("setup-step")).toBeTruthy();
+ });
+});
diff --git a/client/src/app/signup/page.tsx b/client/src/app/signup/page.tsx
index 8d7bfd6ea..d4075c945 100644
--- a/client/src/app/signup/page.tsx
+++ b/client/src/app/signup/page.tsx
@@ -17,17 +17,20 @@ import { authClient } from "../../lib/auth";
import { useConfigs } from "../../lib/configs";
import { BACKEND_URL, IS_CLOUD } from "../../lib/const";
import { trackAdEvent } from "../../lib/trackAdEvent";
+import { useStripeSubscription } from "../../lib/subscription/useStripeSubscription";
import { userStore } from "../../lib/userStore";
import { cn, isValidDomain, normalizeDomain } from "../../lib/utils";
import { EVENT_TIERS, findPriceForTier } from "../subscribe/components/utils";
import { AccountStep } from "./components/AccountStep";
import { PlanStep } from "./components/PlanStep";
import { SetupStep } from "./components/SetupStep";
+import { slugFromName } from "./orgSlug";
function SignupPageContent() {
const { configs, isLoading: isLoadingConfigs } = useConfigs();
useSetPageTitle("Signup");
const t = useExtracted();
+ const { data: sessionData, isPending: isSessionPending } = authClient.useSession();
const maxStep = IS_CLOUD ? 3 : 2;
const [stepParam, setStepParam] = useQueryState("step", parseAsInteger);
@@ -55,21 +58,9 @@ function SignupPageContent() {
// Setup: Organization + website
const [orgName, setOrgName] = useState("");
- const [orgSlug, setOrgSlug] = useState("");
const [referralSource, setReferralSource] = useState("");
const [domain, setDomain] = useState("");
- const handleOrgNameChange = (value: string) => {
- setOrgName(value);
- if (value) {
- const generatedSlug = value
- .toLowerCase()
- .replace(/\s+/g, "-")
- .replace(/[^a-z0-9-]/g, "");
- setOrgSlug(generatedSlug);
- }
- };
-
// Step 1: Account creation submission
const handleAccountSubmit = async () => {
setIsLoading(true);
@@ -115,6 +106,15 @@ function SignupPageContent() {
// Step 2: Setup submission β€” create org + site, then advance (cloud) or redirect (self-hosted)
const [siteId, setSiteId] = useState<number | null>(null);
const [organizationId, setOrganizationId] = useState<string | null>(null);
+ // A reload on the plan step restores it from ?step= but loses the ids the setup
+ // step kept; the organization it created is still the session's active one.
+ const checkoutOrganizationId = organizationId ?? sessionData?.session.activeOrganizationId ?? undefined;
+ // One trial per organization: one restored on the plan step may have had it already,
+ // and checkout then charges straight away.
+ const { data: subscription, isLoading: isLoadingSubscription } = useStripeSubscription(checkoutOrganizationId);
+ const trialEligible = subscription?.trialEligible !== false;
+ // Until the session answers, "no organization" is unknown rather than true.
+ const isResolvingCheckout = isSessionPending || (!!checkoutOrganizationId && isLoadingSubscription);
const handleSetupSubmit = async () => {
setIsLoading(true);
@@ -127,35 +127,41 @@ function SignupPageContent() {
return;
}
- // Create organization
- const { data, error } = await authClient.organization.create({
- name: orgName,
- slug: orgSlug,
- });
+ // Create organization, unless an earlier attempt already did and then failed
+ // to add the site: creating it again would fail.
+ let orgId = organizationId;
+ if (!orgId) {
+ const { data, error } = await authClient.organization.create({
+ name: orgName,
+ slug: slugFromName(orgName),
+ });
- if (error) {
- throw new Error(error.message || t("Failed to create organization"));
- }
+ if (error) {
+ throw new Error(error.message || t("Failed to create organization"));
+ }
- if (!data?.id) {
- throw new Error(t("No organization ID returned"));
- }
+ if (!data?.id) {
+ throw new Error(t("No organization ID returned"));
+ }
- await authClient.organization.setActive({ organizationId: data.id });
+ orgId = data.id;
+ setOrganizationId(orgId);
- if (IS_CLOUD && referralSource && userStore.getState().user?.id) {
- window.rybbit?.identify(userStore.getState().user?.id || "", {
- source: referralSource,
- });
+ await authClient.organization.setActive({ organizationId: orgId });
+
+ if (IS_CLOUD && referralSource && userStore.getState().user?.id) {
+ window.rybbit?.identify(userStore.getState().user?.id || "", {
+ source: referralSource,
+ });
+ }
}
// Add website
const normalizedDomain = normalizeDomain(domain);
- const response = await addSite(normalizedDomain, normalizedDomain, data.id);
+ const response = await addSite(normalizedDomain, normalizedDomain, orgId);
if (IS_CLOUD) {
setSiteId(response.siteId);
- setOrganizationId(data.id);
setCurrentStep(3);
} else {
router.push(`/${response.siteId}`);
@@ -187,8 +193,14 @@ function SignupPageContent() {
return;
}
+ if (isResolvingCheckout) return;
+ if (!checkoutOrganizationId) {
+ setCurrentStep(2);
+ return;
+ }
+
const baseUrl = window.location.origin;
- const returnUrl = `${baseUrl}/${siteId}?session_id={CHECKOUT_SESSION_ID}`;
+ const returnUrl = `${baseUrl}/${siteId ?? ""}?session_id={CHECKOUT_SESSION_ID}`;
const checkoutResponse = await fetch(`${BACKEND_URL}/stripe/create-checkout-session`, {
method: "POST",
@@ -197,7 +209,7 @@ function SignupPageContent() {
body: JSON.stringify({
priceId: selectedTierPrice.priceId,
returnUrl,
- organizationId,
+ organizationId: checkoutOrganizationId,
referral: (window as any).Rewardful?.referral || undefined,
}),
});
@@ -250,8 +262,7 @@ function SignupPageContent() {
domain={domain}
setDomain={setDomain}
orgName={orgName}
- orgSlug={orgSlug}
- handleOrgNameChange={handleOrgNameChange}
+ handleOrgNameChange={setOrgName}
referralSource={referralSource}
setReferralSource={setReferralSource}
isLoading={isLoading}
@@ -268,7 +279,8 @@ function SignupPageContent() {
selectedPlan={selectedPlan}
setSelectedPlan={setSelectedPlan}
onSubscribe={handleSubscribe}
- isLoading={isLoading}
+ isLoading={isLoading || isResolvingCheckout}
+ trialEligible={trialEligible}
/>
);
default:
@@ -317,7 +329,7 @@ function SignupPageContent() {
<div className="flex-1 flex flex-col justify-center w-full max-w-[550px] mx-auto">
<div className="mb-8">
<h1 className="text-3xl font-medium">
- {IS_CLOUD ? t("Start your 7-day free trial") : t("Get started with Rybbit")}
+ {IS_CLOUD && trialEligible ? t("Start your 7-day free trial") : t("Get started with Rybbit")}
</h1>
{IS_CLOUD && (
<p className="text-sm text-neutral-500 dark:text-neutral-400 mt-3">
diff --git a/client/src/app/subscribe/components/PricingCards.tsx b/client/src/app/subscribe/components/PricingCards.tsx
index cabb20ac3..78ec531dd 100644
--- a/client/src/app/subscribe/components/PricingCards.tsx
+++ b/client/src/app/subscribe/components/PricingCards.tsx
@@ -100,13 +100,16 @@ export function PricingCards({ isLoggedIn }: { isLoggedIn: boolean }) {
}),
});
- const data = await response.json();
- trackAdEvent("checkout", { tier: selectedTierPrice.name });
+ // The server refuses with a JSON { error } (409 for an organization that is
+ // already subscribed); a body that isn't JSON still gets the generic message.
+ const data = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(data.error || t("Failed to create checkout session."));
}
+ trackAdEvent("checkout", { tier: selectedTierPrice.name });
+
if (data.clientSecret) {
setCheckoutClientSecret(data.clientSecret);
} else {
diff --git a/client/src/app/subscribe/page.test.tsx b/client/src/app/subscribe/page.test.tsx
new file mode 100644
index 000000000..ffb97d22d
--- /dev/null
+++ b/client/src/app/subscribe/page.test.tsx
@@ -0,0 +1,66 @@
+import { cleanup, render, screen } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import Subscribe from "./page";
+
+const mocks = vi.hoisted(() => ({
+ replace: vi.fn(),
+ subscription: undefined as { id: string | null; status: string } | undefined,
+}));
+
+vi.mock("next-intl", () => ({ useExtracted: () => (message: string) => message }));
+vi.mock("next/navigation", () => ({ useRouter: () => ({ replace: mocks.replace, push: vi.fn() }) }));
+vi.mock("nuqs", () => ({ useQueryState: () => [null, vi.fn()] }));
+vi.mock("@/lib/auth", () => ({
+ authClient: {
+ useSession: () => ({ data: { user: { id: "u1" } } }),
+ useActiveOrganization: () => ({ data: { id: "org1" } }),
+ },
+}));
+vi.mock("@/lib/subscription/useStripeSubscription", () => ({
+ useStripeSubscription: () => ({ data: mocks.subscription }),
+}));
+vi.mock("@/components/StandardPage", () => ({
+ StandardPage: ({ children }: { children: React.ReactNode }) => <>{children}</>,
+}));
+vi.mock("@/components/UsageChart", () => ({ UsageChart: () => null }));
+vi.mock("./components/PricingHeader", () => ({ PricingHeader: () => null }));
+vi.mock("./components/PricingCards", () => ({ PricingCards: () => <div>pricing cards</div> }));
+
+beforeEach(() => {
+ mocks.subscription = undefined;
+});
+
+afterEach(() => {
+ cleanup();
+ vi.clearAllMocks();
+});
+
+describe("/subscribe", () => {
+ it.each(["active", "trialing"])("sends an organization with a %s Stripe subscription to Billing", status => {
+ mocks.subscription = { id: "sub_123", status };
+
+ render(<Subscribe />);
+
+ expect(mocks.replace).toHaveBeenCalledWith("/settings/billing");
+ expect(screen.queryByText("pricing cards")).toBeNull();
+ });
+
+ it("offers checkout to an organization without a subscription", () => {
+ mocks.subscription = { id: null, status: "free" };
+
+ render(<Subscribe />);
+
+ screen.getByText("pricing cards");
+ expect(mocks.replace).not.toHaveBeenCalled();
+ });
+
+ it("offers checkout to an AppSumo organization, which has no Stripe subscription to change", () => {
+ mocks.subscription = { id: null, status: "active" };
+
+ render(<Subscribe />);
+
+ screen.getByText("pricing cards");
+ expect(mocks.replace).not.toHaveBeenCalled();
+ });
+});
diff --git a/client/src/app/subscribe/page.tsx b/client/src/app/subscribe/page.tsx
index 88dfde8db..e74acaa37 100644
--- a/client/src/app/subscribe/page.tsx
+++ b/client/src/app/subscribe/page.tsx
@@ -3,7 +3,7 @@
import { authClient } from "@/lib/auth";
import { useExtracted } from "next-intl";
import { useRouter } from "next/navigation";
-import { Suspense } from "react";
+import { Suspense, useEffect } from "react";
import { StandardPage } from "../../components/StandardPage";
import { UsageChart } from "../../components/UsageChart";
import { useStripeSubscription } from "../../lib/subscription/useStripeSubscription";
@@ -19,14 +19,25 @@ function SubscribeContent() {
const [siteId, setSiteId] = useQueryState("siteId");
- // Redirect if already subscribed
- // if (subscription?.status === "active" || subscription?.status === "trialing") {
- // router.push("/settings/billing");
- // }
+ // An organization with a live Stripe subscription changes plan in Billing
+ // (PlanDialog, which updates that subscription). A checkout from here would
+ // start a second subscription, billed alongside the first.
+ const hasStripeSubscription =
+ !!subscription?.id && (subscription.status === "active" || subscription.status === "trialing");
+
+ useEffect(() => {
+ if (hasStripeSubscription) {
+ router.replace("/settings/billing");
+ }
+ }, [hasStripeSubscription, router]);
// Get the active organization ID
const organizationId = activeOrg?.id;
+ if (hasStripeSubscription) {
+ return null;
+ }
+
if (siteId) {
return (
<StandardPage>
diff --git a/client/src/components/DisabledOverlay.test.tsx b/client/src/components/DisabledOverlay.test.tsx
new file mode 100644
index 000000000..766a559d5
--- /dev/null
+++ b/client/src/components/DisabledOverlay.test.tsx
@@ -0,0 +1,76 @@
+import { cleanup, render, screen } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { DisabledOverlay } from "./DisabledOverlay";
+
+type Subscription = { eventLimit: number; planName: string; status: string } | undefined;
+
+const mocks = vi.hoisted(() => ({ subscription: undefined as Subscription }));
+
+vi.mock("next-intl", () => ({
+ useExtracted: () => (message: string, values?: Record<string, string>) =>
+ message.replace(/\{(\w+)\}/g, (_, key) => values?.[key] ?? ""),
+}));
+vi.mock("next/link", () => ({
+ default: ({ href, children }: { href: string; children: React.ReactNode }) => <a href={href}>{children}</a>,
+}));
+vi.mock("@/lib/const", async importOriginal => ({ ...(await importOriginal<object>()), IS_CLOUD: true }));
+vi.mock("@/api/admin/hooks/useSites", () => ({
+ useCurrentSite: () => ({ site: { organizationId: "org1" }, subscription: mocks.subscription }),
+}));
+vi.mock("@/hooks/usePermissions", () => ({ useCanInOrg: () => true }));
+vi.mock("@/lib/auth", () => ({
+ authClient: {
+ useSession: () => ({ data: { user: { role: "user" } } }),
+ // Created after the Pro grandfathering cutoff.
+ useActiveOrganization: () => ({ data: { createdAt: new Date("2026-01-01") } }),
+ },
+}));
+
+function renderOverlay(requiredPlan?: "pro" | "standard" | "basic") {
+ return render(
+ <DisabledOverlay requiredPlan={requiredPlan} message="funnels">
+ <div>paid feature</div>
+ </DisabledOverlay>
+ );
+}
+
+beforeEach(() => {
+ mocks.subscription = undefined;
+});
+
+afterEach(cleanup);
+
+describe("DisabledOverlay", () => {
+ it("shows a Pro feature unlocked while the subscription hasn't loaded", () => {
+ const { container } = renderOverlay("pro");
+
+ screen.getByText("paid feature");
+ expect(screen.queryByText(/Upgrade/)).toBeNull();
+ expect(container.querySelector("[style*='blur']")).toBeNull();
+ });
+
+ it("shows a Pro feature to a Pro subscriber", () => {
+ mocks.subscription = { eventLimit: 100_000, planName: "pro100k", status: "active" };
+
+ renderOverlay("pro");
+
+ expect(screen.queryByText(/Upgrade/)).toBeNull();
+ });
+
+ it("sends a paying subscriber to Billing to change plan, not to a new checkout", () => {
+ mocks.subscription = { eventLimit: 100_000, planName: "standard100k", status: "active" };
+
+ renderOverlay("pro");
+
+ expect(screen.getByRole("link", { name: /Upgrade/ }).getAttribute("href")).toBe("/settings/billing");
+ });
+
+ it("sends a free organization to /subscribe", () => {
+ mocks.subscription = { eventLimit: 3_000, planName: "free", status: "free" };
+
+ renderOverlay();
+
+ expect(screen.getByRole("link", { name: /Upgrade/ }).getAttribute("href")).toBe("/subscribe");
+ });
+});
diff --git a/client/src/components/DisabledOverlay.tsx b/client/src/components/DisabledOverlay.tsx
index 78b232d52..692661949 100644
--- a/client/src/components/DisabledOverlay.tsx
+++ b/client/src/components/DisabledOverlay.tsx
@@ -21,7 +21,17 @@ interface DisabledOverlayProps {
requiredPlan?: "pro" | "standard" | "basic";
}
-function OwnerMessage({ message, featurePath, requiredPlan }: { message: string; featurePath?: string; requiredPlan?: "pro" | "standard" | "basic" }) {
+function OwnerMessage({
+ message,
+ featurePath,
+ requiredPlan,
+ upgradeHref,
+}: {
+ message: string;
+ featurePath?: string;
+ requiredPlan?: "pro" | "standard" | "basic";
+ upgradeHref: string;
+}) {
const t = useExtracted();
const planName = requiredPlan === "pro" ? t("Pro") : requiredPlan === "basic" ? t("Basic") : t("Standard");
return (
@@ -44,7 +54,7 @@ function OwnerMessage({ message, featurePath, requiredPlan }: { message: string;
)}
</div>
<Button asChild size="sm" variant="success">
- <Link href="/subscribe">
+ <Link href={upgradeHref}>
{t("Upgrade")} <ArrowRight className="h-3 w-3" />
</Link>
</Button>
@@ -103,17 +113,19 @@ export const DisabledOverlay: React.FC<DisabledOverlayProps> = ({
const [overlayRemoved, setOverlayRemoved] = useState(false);
const disabled = useMemo(() => {
- if (!IS_CLOUD) {
+ // Nothing is decided until the subscription has loaded: locking while it is
+ // loading, or after the request failed, would blur paid features for paying users.
+ if (!IS_CLOUD || !subscription) {
return false;
}
- const isFree = subscription?.eventLimit === DEFAULT_EVENT_LIMIT;
- const isBasic = subscription?.planName?.includes("basic") ?? false;
+ const isFree = subscription.eventLimit === DEFAULT_EVENT_LIMIT;
+ const isBasic = subscription.planName?.includes("basic") ?? false;
if (requiredPlan === "pro") {
if (organization?.createdAt && DateTime.fromJSDate(organization?.createdAt) < DateTime.fromISO("2025-09-19")) {
return false;
}
- return !subscription?.planName.includes("pro");
+ return !subscription.planName?.includes("pro");
}
if (requiredPlan === "basic") {
// Basic and above can access β€” only free is blocked
@@ -195,6 +207,9 @@ export const DisabledOverlay: React.FC<DisabledOverlayProps> = ({
}
const borderRadiusStyle = borderRadius > 0 ? { borderRadius: `${borderRadius}px` } : {};
+ // A paying organization changes plan in Billing; /subscribe would start a second subscription.
+ const upgradeHref =
+ subscription?.status === "active" || subscription?.status === "trialing" ? "/settings/billing" : "/subscribe";
return (
<div ref={containerRef} className="relative" style={style}>
@@ -212,7 +227,16 @@ export const DisabledOverlay: React.FC<DisabledOverlayProps> = ({
>
{showMessage && (
<div className="flex items-center justify-center">
- {canUpgrade ? <OwnerMessage message={message} featurePath={featurePath} requiredPlan={requiredPlan} /> : <UserMessage message={message} featurePath={featurePath} />}
+ {canUpgrade ? (
+ <OwnerMessage
+ message={message}
+ featurePath={featurePath}
+ requiredPlan={requiredPlan}
+ upgradeHref={upgradeHref}
+ />
+ ) : (
+ <UserMessage message={message} featurePath={featurePath} />
+ )}
</div>
)}
</div>
diff --git a/client/src/components/EventUsageChart.tsx b/client/src/components/EventUsageChart.tsx
index b45bcda9f..73b7d35ba 100644
--- a/client/src/components/EventUsageChart.tsx
+++ b/client/src/components/EventUsageChart.tsx
@@ -23,6 +23,18 @@ const CHART_COLORS = [
"#f472b6", // pink-400 (input change)
];
+/**
+ * A row's `event_date` is the wall-clock start of a day in the zone the counts
+ * were requested in. Read it as UTC so it keeps that calendar day: parsing it in
+ * the browser's zone and converting moved every day one back east of UTC. The
+ * chart's time scale and labels are UTC to match.
+ */
+export function parseUsageDay(eventDate: string): DateTime {
+ return DateTime.fromSQL(eventDate, { zone: "utc" });
+}
+
+const UTC_DAY: Intl.DateTimeFormatOptions = { month: "short", day: "numeric", timeZone: "UTC" };
+
export interface EventUsageChartProps {
data: EventCountRow[] | undefined;
isLoading: boolean;
@@ -46,13 +58,20 @@ export function EventUsageChart({
const maxTicks = Math.round((width ?? Infinity) / 200);
+ // No day after today is real. The admin charts count in the viewer's zone, so
+ // today is the later of the UTC and local dates.
+ const utcToday = DateTime.utc().toFormat("yyyy-MM-dd");
+ const localToday = DateTime.local().toFormat("yyyy-MM-dd");
+ const lastDay = utcToday > localToday ? utcToday : localToday;
+
const mapEventData = (key: keyof EventCountRow) =>
data
?.map((e) => {
- const timestamp = DateTime.fromSQL(e.event_date).toUTC();
- if (timestamp > DateTime.now()) return null;
+ const timestamp = parseUsageDay(e.event_date);
+ const day = timestamp.toFormat("yyyy-MM-dd");
+ if (day > lastDay) return null;
return {
- x: timestamp.toFormat("yyyy-MM-dd"),
+ x: day,
y: e[key] as number,
currentTime: timestamp,
};
@@ -161,7 +180,7 @@ export function EventUsageChart({
truncateTickAt: 0,
tickValues: Math.min(maxTicks, maxTickCount),
format: (value) => {
- return new Intl.DateTimeFormat(locale, { month: "short", day: "numeric" }).format(value);
+ return new Intl.DateTimeFormat(locale, UTC_DAY).format(value);
},
}}
axisLeft={{
@@ -192,7 +211,7 @@ export function EventUsageChart({
<ChartTooltip>
<div className="p-3 min-w-[100px]">
<div className="font-medium mb-1">
- {formatDateTime(currentTime, { month: "short", day: "numeric", year: "numeric" })}
+ {formatDateTime(currentTime, { ...UTC_DAY, year: "numeric" })}
</div>
{slice.points
.sort(
diff --git a/client/src/components/SiteSettings/UsageTab.tsx b/client/src/components/SiteSettings/UsageTab.tsx
index 3be815c61..efb44a4ae 100644
--- a/client/src/components/SiteSettings/UsageTab.tsx
+++ b/client/src/components/SiteSettings/UsageTab.tsx
@@ -9,6 +9,7 @@ import { useGetSiteUsage } from "@/api/admin/hooks/useSites";
import { SiteEventCountPoint } from "@/api/analytics/endpoints";
import { useGetSiteEventCountRange } from "@/api/analytics/hooks/useGetSiteEventCountRange";
import { EventUsageChart } from "@/components/EventUsageChart";
+import { getPeriodDates, PERIODS, PeriodValue } from "@/components/UsageChart";
import { Skeleton } from "@/components/ui/skeleton";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { cn } from "@/lib/utils";
@@ -19,23 +20,6 @@ interface UsageTabProps {
siteId: number;
}
-const PERIODS = [
- { value: "7", label: "7D" },
- { value: "14", label: "14D" },
- { value: "30", label: "30D" },
- { value: "60", label: "60D" },
- { value: "all", label: "All" },
-] as const;
-
-type PeriodValue = (typeof PERIODS)[number]["value"];
-
-function getPeriodDates(period: PeriodValue): { startDate?: string; endDate?: string } {
- if (period === "all") return {};
- const end = DateTime.now().toFormat("yyyy-MM-dd");
- const start = DateTime.now().minus({ days: Number(period) }).toFormat("yyyy-MM-dd");
- return { startDate: start, endDate: end };
-}
-
const EMPTY_COUNTS = {
pageview_count: 0,
custom_event_count: 0,
diff --git a/client/src/components/UsageChart.test.tsx b/client/src/components/UsageChart.test.tsx
new file mode 100644
index 000000000..fc43887a3
--- /dev/null
+++ b/client/src/components/UsageChart.test.tsx
@@ -0,0 +1,50 @@
+import { DateTime, Settings } from "luxon";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { parseUsageDay } from "./EventUsageChart";
+import { getPeriodDates } from "./UsageChart";
+
+const systemZone = Settings.defaultZone;
+
+afterEach(() => {
+ Settings.defaultZone = systemZone;
+ vi.useRealTimers();
+});
+
+describe("getPeriodDates", () => {
+ it("covers exactly `period` days, today included", () => {
+ vi.useFakeTimers();
+ vi.setSystemTime(new Date("2026-10-24T12:00:00Z"));
+
+ const { startDate, endDate } = getPeriodDates("7");
+
+ expect({ startDate, endDate }).toEqual({ startDate: "2026-10-18", endDate: "2026-10-24" });
+ const days = DateTime.fromISO(endDate!).diff(DateTime.fromISO(startDate!), "days").days + 1;
+ expect(days).toBe(7);
+ });
+
+ it("uses dates in the requested zone, not the browser's", () => {
+ // 08:00 on the 24th in Tokyo is still the 23rd in UTC.
+ Settings.defaultZone = "Asia/Tokyo";
+ vi.useFakeTimers();
+ vi.setSystemTime(new Date("2026-10-23T23:00:00Z"));
+
+ expect(getPeriodDates("7")).toEqual({ startDate: "2026-10-17", endDate: "2026-10-23" });
+ expect(getPeriodDates("7", "Asia/Tokyo")).toEqual({ startDate: "2026-10-18", endDate: "2026-10-24" });
+ });
+
+ it("is unbounded for all time", () => {
+ expect(getPeriodDates("all")).toEqual({});
+ });
+});
+
+describe("parseUsageDay", () => {
+ it.each(["Asia/Tokyo", "America/Los_Angeles", "UTC"])("keeps the row's calendar day in %s", zone => {
+ Settings.defaultZone = zone;
+
+ const day = parseUsageDay("2026-10-23 00:00:00");
+
+ expect(day.toFormat("yyyy-MM-dd")).toBe("2026-10-23");
+ expect(new Intl.DateTimeFormat("en-US", { day: "numeric", timeZone: "UTC" }).format(day.toJSDate())).toBe("23");
+ });
+});
diff --git a/client/src/components/UsageChart.tsx b/client/src/components/UsageChart.tsx
index ba868cca5..ae75c35f8 100644
--- a/client/src/components/UsageChart.tsx
+++ b/client/src/components/UsageChart.tsx
@@ -18,10 +18,15 @@ export const PERIODS = [
export type PeriodValue = (typeof PERIODS)[number]["value"];
-export function getPeriodDates(period: PeriodValue): { startDate?: string; endDate?: string } {
+/**
+ * The last `period` calendar days, today included, as dates in `timeZone`: the
+ * zone the counts are requested in, so the range's days are the chart's days.
+ */
+export function getPeriodDates(period: PeriodValue, timeZone = "UTC"): { startDate?: string; endDate?: string } {
if (period === "all") return {};
- const end = DateTime.now().toFormat("yyyy-MM-dd");
- const start = DateTime.now().minus({ days: Number(period) }).toFormat("yyyy-MM-dd");
+ const today = DateTime.now().setZone(timeZone);
+ const end = today.toFormat("yyyy-MM-dd");
+ const start = today.minus({ days: Number(period) - 1 }).toFormat("yyyy-MM-dd");
return { startDate: start, endDate: end };
}
@@ -34,7 +39,7 @@ export function UsageChart({ organizationId, timeZone = "UTC" }: UsageChartProps
const t = useExtracted();
const [period, setPeriod] = useState<PeriodValue>("30");
- const { startDate, endDate } = getPeriodDates(period);
+ const { startDate, endDate } = getPeriodDates(period, timeZone);
const { data, isLoading, error } = useGetOrgEventCount({
organizationId,
diff --git a/client/src/components/replay/player/hooks/useReplayPlayer.test.tsx b/client/src/components/replay/player/hooks/useReplayPlayer.test.tsx
new file mode 100644
index 000000000..2ac1db4f7
--- /dev/null
+++ b/client/src/components/replay/player/hooks/useReplayPlayer.test.tsx
@@ -0,0 +1,63 @@
+import { cleanup, render } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { useReplayStore } from "../../replayStore";
+import { useReplayPlayer } from "./useReplayPlayer";
+
+const players = vi.hoisted(() => [] as any[]);
+vi.mock("rrweb-player", () => ({
+ default: class {
+ replayer = { destroy: vi.fn(), getCurrentTime: () => 0, config: { speed: 1 } };
+ pause = vi.fn();
+ addEventListener = vi.fn();
+ getMetaData = vi.fn(() => ({ totalTime: 1000 }));
+ getReplayer = () => this.replayer;
+ $set = vi.fn();
+ triggerResize = vi.fn();
+ $destroy = vi.fn();
+ constructor() {
+ players.push(this);
+ }
+ },
+}));
+
+function Player({ data }: { data: { events: any[] } }) {
+ const { playerContainerRef } = useReplayPlayer({ data, width: 800, height: 600 });
+ return <div ref={playerContainerRef} />;
+}
+
+const initialState = useReplayStore.getState();
+
+beforeEach(() => {
+ players.length = 0;
+ useReplayStore.setState(initialState, true);
+});
+afterEach(() => {
+ cleanup();
+ vi.useRealTimers();
+});
+
+describe("useReplayPlayer teardown", () => {
+ it("destroys the previous player and its replayer when the recording changes or unmounts", () => {
+ const view = render(<Player data={{ events: [] }} />);
+ view.rerender(<Player data={{ events: [] }} />);
+
+ expect(players).toHaveLength(2);
+ expect(players[0].replayer.destroy).toHaveBeenCalledOnce();
+ expect(players[0].$destroy).toHaveBeenCalledOnce();
+ expect(players[1].$destroy).not.toHaveBeenCalled();
+ expect(useReplayStore.getState().player).toBe(players[1]);
+
+ view.unmount();
+ expect(players[1].$destroy).toHaveBeenCalledOnce();
+ expect(useReplayStore.getState().player).toBeNull();
+ });
+
+ it("does not read the duration from a player destroyed before its first tick", () => {
+ vi.useFakeTimers();
+ const view = render(<Player data={{ events: [] }} />);
+ view.unmount();
+
+ vi.runAllTimers();
+ expect(players[0].getMetaData).not.toHaveBeenCalled();
+ });
+});
diff --git a/client/src/components/replay/player/hooks/useReplayPlayer.ts b/client/src/components/replay/player/hooks/useReplayPlayer.ts
index 26aafd45d..abb02bf3a 100644
--- a/client/src/components/replay/player/hooks/useReplayPlayer.ts
+++ b/client/src/components/replay/player/hooks/useReplayPlayer.ts
@@ -37,6 +37,7 @@ export const useReplayPlayer = ({ data, width, height }: UseReplayPlayerProps) =
let newPlayer: any = null;
let handleVisibilityChange: (() => void) | null = null;
+ let durationTimeout: ReturnType<typeof setTimeout> | undefined;
try {
// Initialize rrweb player
@@ -87,7 +88,7 @@ export const useReplayPlayer = ({ data, width, height }: UseReplayPlayerProps) =
});
// Get the initial duration from the player
- setTimeout(() => {
+ durationTimeout = setTimeout(() => {
const playerDuration = newPlayer.getMetaData().totalTime;
if (playerDuration && isActive()) {
setDuration(playerDuration);
@@ -132,18 +133,27 @@ export const useReplayPlayer = ({ data, width, height }: UseReplayPlayerProps) =
}
return () => {
- // Proper cleanup
- if (newPlayer) {
- newPlayer.pause();
- }
- if (playerContainerRef.current) {
- playerContainerRef.current.innerHTML = "";
- }
+ // A destroyed player has no methods left for the duration read to call.
+ clearTimeout(durationTimeout);
if (handleVisibilityChange) {
document.removeEventListener("visibilitychange", handleVisibilityChange);
}
playerRef.current = null;
unregisterPlayer(newPlayer);
+ // rrweb-player's fullscreen listeners on document hold the component,
+ // its Replayer and the whole recording until $destroy removes them, so
+ // emptying the container alone leaks every replay that was opened.
+ if (newPlayer) {
+ try {
+ newPlayer.getReplayer().destroy();
+ } catch (error) {
+ console.error("Failed to destroy rrweb replayer:", error);
+ }
+ newPlayer.$destroy();
+ }
+ if (playerContainerRef.current) {
+ playerContainerRef.current.innerHTML = "";
+ }
};
}
}, [data, registerPlayer, unregisterPlayer, setCurrentTime, setIsPlaying, setDuration]);
diff --git a/client/src/lib/siteRoute.ts b/client/src/lib/siteRoute.ts
index 6a1723227..a5de95828 100644
--- a/client/src/lib/siteRoute.ts
+++ b/client/src/lib/siteRoute.ts
@@ -16,8 +16,20 @@ export const SYNCED_ANALYTICS_ROUTES = new Set([
"replay",
"feature-flags",
"experiments",
+ "bots",
+ "user",
+ "dashboards",
]);
+/**
+ * Synced routes usually opened from a link without a query string: a profile
+ * from a list of users, a dashboard from the dashboards list, the next user in
+ * the stepper. An address with no analytics state keeps the period and filters
+ * the page was opened with instead of resetting them; a refreshed or shared
+ * address restores its own like every other synced route.
+ */
+const CARRY_OVER_ANALYTICS_ROUTES = new Set(["user", "dashboards"]);
+
export type SiteRouteContext = {
siteId: string | null;
privateKey: string | null;
@@ -40,6 +52,10 @@ export function isSyncedAnalyticsRoute(route: string | null | undefined) {
return !!route && SYNCED_ANALYTICS_ROUTES.has(route);
}
+export function isCarryOverAnalyticsRoute(route: string | null | undefined) {
+ return !!route && CARRY_OVER_ANALYTICS_ROUTES.has(route);
+}
+
export function getMainDashboardPath(pathname: string | null | undefined) {
const { siteId, privateKey } = getSiteRouteContext(pathname);
if (!siteId || isNaN(Number(siteId))) return null;
diff --git a/client/src/lib/store.test.tsx b/client/src/lib/store.test.tsx
index d1179d220..408975bee 100644
--- a/client/src/lib/store.test.tsx
+++ b/client/src/lib/store.test.tsx
@@ -1,5 +1,6 @@
// The .tsx suffix selects Vitest's jsdom project for localStorage and window.history coverage.
import { Filter } from "@rybbit/shared";
+import { DateTime } from "luxon";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { DEFAULT_COMPARISON } from "../components/DateSelector/types";
import { addFilter, getFilteredFilters, getTimezone, removeFilter, updateFilter, useStore } from "./store";
@@ -127,6 +128,26 @@ describe("site context", () => {
expect(state.comparison).toEqual(DEFAULT_COMPARISON);
});
+ it("resolves the default period in the selected timezone rather than the browser's", () => {
+ // 16:00 UTC is already the 30th at UTC+14 and still the 29th at UTC-11, so
+ // one of the two disagrees with whatever zone the tests run in.
+ const systemDay = DateTime.now().toISODate();
+ const zone =
+ DateTime.now().setZone("Pacific/Kiritimati").toISODate() === systemDay
+ ? "Pacific/Pago_Pago"
+ : "Pacific/Kiritimati";
+ useStore.setState({ timezone: zone });
+
+ useStore.getState().setSiteContext("84", null);
+
+ expect(useStore.getState().time).toEqual({
+ mode: "day",
+ day: DateTime.now().setZone(zone).toISODate(),
+ wellKnown: "today",
+ });
+ expect(useStore.getState().time).not.toMatchObject({ day: systemDay });
+ });
+
it("preserves selections whose URL parameters are waiting to hydrate", () => {
window.history.replaceState({}, "", "/84/main?timeMode=day&bucket=month&stat=bounce_rate&filters=%5B%5D");
useStore.setState({
diff --git a/client/src/lib/store.ts b/client/src/lib/store.ts
index 2a8a44591..c7740abba 100644
--- a/client/src/lib/store.ts
+++ b/client/src/lib/store.ts
@@ -71,7 +71,9 @@ type PersistedStore = Pick<Store, "timezone">;
const getUrlParams = () => (typeof window !== "undefined" ? new URLSearchParams(globalThis.location.search) : null);
-const getDefaultTime = (): Time => getStoredDashboardDefaultTime(getSystemTimezone());
+// Presets resolve in the selected timezone: "Today" is a different calendar day
+// in Los Angeles than in a Tokyo browser for part of every day.
+const getDefaultTime = (): Time => getStoredDashboardDefaultTime(getTimezone());
const getDefaultTimeState = () => getTimeState(getDefaultTime());
diff --git a/client/src/lib/urlParams.test.tsx b/client/src/lib/urlParams.test.tsx
index 7824cc5e9..926ec6ca5 100644
--- a/client/src/lib/urlParams.test.tsx
+++ b/client/src/lib/urlParams.test.tsx
@@ -117,3 +117,70 @@ describe("homepage time URL persistence", () => {
await waitFor(() => expect(useStore.getState().time).toMatchObject({ pastMinutesStart: 60 }));
});
});
+
+describe("site detail pages", () => {
+ const chrome = { parameter: "browser" as const, type: "equals" as const, value: ["Chrome"] };
+ const sharedSearch = `?timeMode=range&startDate=2026-09-01&endDate=2026-09-07&filters=${encodeURIComponent(
+ JSON.stringify([chrome])
+ )}`;
+
+ it.each(["/42/bots", "/42/user/abc", "/42/dashboards/5"])(
+ "restores a shared or refreshed address on %s",
+ async path => {
+ useStore.setState({ site: "42" });
+ mount(sharedSearch, path);
+ await waitFor(() =>
+ expect(useStore.getState()).toMatchObject({
+ time: { mode: "range", startDate: "2026-09-01", endDate: "2026-09-07" },
+ filters: [chrome],
+ })
+ );
+ }
+ );
+
+ it("keeps the period and filters a profile was opened with, and writes them to its address", async () => {
+ const time = { mode: "range" as const, startDate: "2026-08-01", endDate: "2026-08-30" };
+ useStore.getState().setTime(time);
+ useStore.setState({ site: "42", filters: [chrome] });
+ mount("", "/42/user/abc");
+ await waitFor(() => expect(new URLSearchParams(navigation.search).get("startDate")).toBe("2026-08-01"));
+ expect(useStore.getState()).toMatchObject({ time, filters: [chrome] });
+ expect(JSON.parse(new URLSearchParams(navigation.search).get("filters") ?? "null")).toEqual([chrome]);
+ });
+
+ it("still resets list pages opened without a query", async () => {
+ useStore.setState({ site: "42", filters: [chrome] });
+ mount("", "/42/bots");
+ await waitFor(() => expect(useStore.getState().filters).toEqual([]));
+ });
+});
+
+describe("client-side navigation", () => {
+ const chrome = { parameter: "browser" as const, type: "equals" as const, value: ["Chrome"] };
+
+ // A client-side navigation: the router's address changes at once, nuqs's values a commit later.
+ function navigate(view: ReturnType<typeof mount>, search: string, pathname: string) {
+ navigation.pathname = pathname;
+ navigation.search = search;
+ view.rerender(
+ <NuqsTestingAdapter searchParams={search} onUrlUpdate={onUrlUpdate} hasMemory>
+ <Sync />
+ </NuqsTestingAdapter>
+ );
+ }
+
+ it.each([
+ ["a page that doesn't sync", "", "/42/query"],
+ ["a synced page with its own period", "?timeMode=range&startDate=2026-08-01&endDate=2026-08-30", "/42/main"],
+ ])("restores the destination's own address when arriving from %s", async (_from, fromSearch, fromPath) => {
+ useStore.setState({ site: "42", filters: [chrome] });
+ const view = mount(fromSearch, fromPath);
+ await waitFor(() => expect(useStore.getState().filters).toEqual(fromPath === "/42/main" ? [] : [chrome]));
+
+ navigate(view, "?timeMode=day&day=2026-09-01", "/42/user/abc");
+ await waitFor(() => expect(useStore.getState().time).toMatchObject({ mode: "day", day: "2026-09-01" }));
+ expect(useStore.getState().filters).toEqual([]);
+ expect(new URLSearchParams(navigation.search).get("day")).toBe("2026-09-01");
+ expect(new URLSearchParams(navigation.search).get("startDate")).toBeNull();
+ });
+});
diff --git a/client/src/lib/urlParams.ts b/client/src/lib/urlParams.ts
index 528edf523..49b14c164 100644
--- a/client/src/lib/urlParams.ts
+++ b/client/src/lib/urlParams.ts
@@ -2,14 +2,19 @@
import { DEFAULT_COMPARISON } from "@/components/DateSelector/types";
import { usePathname, useSearchParams } from "next/navigation";
-import { useQueryStates } from "nuqs";
+import { createLoader, useQueryStates } from "nuqs";
import React, { useEffect } from "react";
import { getStoredDashboardDefaultTime } from "./defaultTimeRange";
import { analyticsParsers } from "./parsers";
-import { getSiteRouteContext, isSyncedAnalyticsRoute } from "./siteRoute";
+import { getSiteRouteContext, isCarryOverAnalyticsRoute, isSyncedAnalyticsRoute } from "./siteRoute";
import { getTimezone, useStore } from "./store";
import { comparisonToUrlParams, timeToUrlParams, urlParamsToComparison, urlParamsToTime } from "./time";
+// The parameters that carry dashboard state; embed flags and foreign params don't.
+const STATE_PARAMS = ["timeMode", "wellKnown", "compare", "bucket", "stat", "filters", "segment"] as const;
+
+const loadAnalyticsParams = createLoader(analyticsParsers);
+
// Hook to sync store state with URL
export const useSyncStateWithUrl = () => {
const pathname = usePathname();
@@ -34,19 +39,23 @@ export const useSyncStateWithUrl = () => {
const isHomepage = pathname === "/";
const shouldSyncUrl = isHomepage || isSyncedAnalyticsRoute(routeContext.route);
const isRouteReady = isHomepage || site === routeContext.siteId;
+ const search = searchParams.toString();
const hydrationKey = shouldSyncUrl
- ? `${routeContext.siteId ?? ""}:${routeContext.privateKey ?? ""}:${routeContext.route ?? ""}?${searchParams.toString()}`
+ ? `${routeContext.siteId ?? ""}:${routeContext.privateKey ?? ""}:${routeContext.route ?? ""}?${search}`
: null;
const [hydratedUrlKey, setHydratedUrlKey] = React.useState<string | null>(null);
+ // The page the store was last hydrated on, while it stays synced.
+ const hydratedPathname = React.useRef<string | null>(null);
useEffect(() => {
if (!hydrationKey && hydratedUrlKey) {
+ hydratedPathname.current = null;
setHydratedUrlKey(null);
}
}, [hydrationKey, hydratedUrlKey]);
// Get URL params using nuqs
- const [urlParams, setUrlParams] = useQueryStates(analyticsParsers, {
+ const [queryState, setUrlParams] = useQueryStates(analyticsParsers, {
history: "replace",
shallow: true,
});
@@ -55,6 +64,20 @@ export const useSyncStateWithUrl = () => {
useEffect(() => {
if (!hydrationKey || !isRouteReady || hydratedUrlKey === hydrationKey) return;
+ // Arriving on a page, the address is read as it is now: nuqs's values catch up with a
+ // client-side navigation a commit later, and restoring the previous page's would put its
+ // period and filters here and then write them over this address. Staying on the page,
+ // nuqs's values are the current ones: they include writes the address has yet to show.
+ const urlParams = hydratedPathname.current === pathname ? queryState : loadAnalyticsParams(search);
+ hydratedPathname.current = pathname;
+
+ // Arriving without a query keeps what the previous page had; the write
+ // below then puts it in the address so a refresh restores it.
+ if (isCarryOverAnalyticsRoute(routeContext.route) && STATE_PARAMS.every(key => urlParams[key] === null)) {
+ setHydratedUrlKey(hydrationKey);
+ return;
+ }
+
// The comparison is restored before the period so the window it resolves
// to is the shared link's, not the default period's.
setComparison(urlParamsToComparison(urlParams) ?? DEFAULT_COMPARISON);
@@ -86,13 +109,16 @@ export const useSyncStateWithUrl = () => {
hydratedUrlKey,
isHomepage,
isRouteReady,
+ pathname,
+ routeContext.route,
+ search,
setTime,
setBucket,
setSelectedStat,
setFilters,
setSegmentId,
setComparison,
- urlParams,
+ queryState,
]);
// Update URL when state changes
diff --git a/server/public/script-full.js b/server/public/script-full.js
index ec7f7bedd..74ad20f2b 100644
--- a/server/public/script-full.js
+++ b/server/public/script-full.js
@@ -55,6 +55,11 @@
return false;
}
}
+ function truncateText(value, maxLength) {
+ if (value.length <= maxLength) return value;
+ const lastCode = value.charCodeAt(maxLength - 1);
+ return value.slice(0, lastCode >= 55296 && lastCode <= 56319 ? maxLength - 1 : maxLength);
+ }
function parseJsonSafely(value, fallback) {
if (!value) return fallback;
try {
@@ -285,8 +290,205 @@
return resolvedConfig;
}
+ // ../../../shared/src/botSignalContract.ts
+ var CLIENT_BOT_SIGNAL_MASKS = {
+ automationApi: 1 << 0,
+ zeroOuterDimensions: 1 << 1,
+ missingChrome: 1 << 2,
+ swiftShader: 1 << 3,
+ emptyPlugins: 1 << 4,
+ defaultViewport800x600: 1 << 5,
+ defaultViewport1024x768: 1 << 6,
+ impossibleDimensions: 1 << 7,
+ outerDimensionsWeird: 1 << 8,
+ pluginApiAbsence: 1 << 9,
+ defaultViewport1280x1200: 1 << 10,
+ squareScreen: 1 << 11,
+ missingScreenDimensions: 1 << 12
+ };
+ var CLIENT_BOT_SIGNAL_NAMES = Object.keys(CLIENT_BOT_SIGNAL_MASKS);
+ var CLIENT_BOT_SIGNAL_WEIGHTS = {
+ automationApi: 3,
+ zeroOuterDimensions: 2,
+ missingChrome: 1,
+ swiftShader: 1,
+ emptyPlugins: 1,
+ defaultViewport800x600: 3,
+ defaultViewport1024x768: 3,
+ impossibleDimensions: 3,
+ outerDimensionsWeird: 2,
+ pluginApiAbsence: 0,
+ defaultViewport1280x1200: 3,
+ squareScreen: 3,
+ missingScreenDimensions: 1
+ };
+ var ALL_CLIENT_BOT_SIGNAL_BITS = CLIENT_BOT_SIGNAL_NAMES.reduce(
+ (mask, name) => mask | CLIENT_BOT_SIGNAL_MASKS[name],
+ 0
+ );
+ var STRONG_CLIENT_BOT_SIGNAL_BITS = CLIENT_BOT_SIGNAL_MASKS.automationApi | CLIENT_BOT_SIGNAL_MASKS.impossibleDimensions | CLIENT_BOT_SIGNAL_MASKS.defaultViewport800x600 | CLIENT_BOT_SIGNAL_MASKS.defaultViewport1024x768 | CLIENT_BOT_SIGNAL_MASKS.defaultViewport1280x1200 | CLIENT_BOT_SIGNAL_MASKS.squareScreen;
+ var MAX_CLIENT_BOT_SCORE = 10;
+ var MIN_PLAUSIBLE_SCREEN_DIMENSION = 200;
+ var MAX_PLAUSIBLE_SCREEN_DIMENSION = 8192;
+ var IMPLAUSIBLE_DESKTOP_VIEWPORTS = [
+ { width: 800, height: 600, signal: "defaultViewport800x600" },
+ { width: 1024, height: 768, signal: "defaultViewport1024x768" },
+ { width: 1280, height: 1200, signal: "defaultViewport1280x1200" }
+ ];
+ function isPlausibleScreenDimensions(width, height) {
+ return Number.isFinite(width) && Number.isFinite(height) && width >= MIN_PLAUSIBLE_SCREEN_DIMENSION && height >= MIN_PLAUSIBLE_SCREEN_DIMENSION && width <= MAX_PLAUSIBLE_SCREEN_DIMENSION && height <= MAX_PLAUSIBLE_SCREEN_DIMENSION;
+ }
+ function isDesktopUserAgent(userAgent) {
+ return /Windows NT|Macintosh|X11|Linux x86_64/.test(userAgent) && !/Mobile|Android|iPhone|iPad/.test(userAgent);
+ }
+ function getScreenDimensionSignals(width, height, userAgent) {
+ if (!isPlausibleScreenDimensions(width, height)) {
+ return ["impossibleDimensions"];
+ }
+ const signals = [];
+ if (width === height) {
+ signals.push("squareScreen");
+ }
+ if (isDesktopUserAgent(userAgent)) {
+ for (const viewport of IMPLAUSIBLE_DESKTOP_VIEWPORTS) {
+ if (width === viewport.width && height === viewport.height) {
+ signals.push(viewport.signal);
+ }
+ }
+ }
+ return signals;
+ }
+
+ // botSignals.ts
+ var cachedBotSignals = null;
+ function getBotScore() {
+ return getBotSignals().score;
+ }
+ function getBotSignalMask() {
+ return getBotSignals().mask;
+ }
+ function isPrerendering() {
+ return document.prerendering === true;
+ }
+ function getBotSignals() {
+ if (isPrerendering()) {
+ return calculateBotSignals();
+ }
+ cachedBotSignals ?? (cachedBotSignals = calculateBotSignals());
+ return cachedBotSignals;
+ }
+ function calculateBotSignals() {
+ let score = 0;
+ let mask = 0;
+ function addSignal(name) {
+ const signalMask = CLIENT_BOT_SIGNAL_MASKS[name];
+ if ((mask & signalMask) !== 0) {
+ return;
+ }
+ mask |= signalMask;
+ score += CLIENT_BOT_SIGNAL_WEIGHTS[name];
+ }
+ try {
+ const userAgent = navigator.userAgent;
+ const isChromeLike = /Chrome\//.test(userAgent) && !/\bwv\b|; wv\)/.test(userAgent);
+ const screenWidth = Number(window.screen?.width);
+ const screenHeight = Number(window.screen?.height);
+ const outerWidth = Number(window.outerWidth);
+ const outerHeight = Number(window.outerHeight);
+ const innerWidth = Number(window.innerWidth);
+ const innerHeight = Number(window.innerHeight);
+ const automationGlobalNames = [
+ "__webdriver_evaluate",
+ "__selenium_evaluate",
+ "__webdriver_script_function",
+ "__webdriver_script_func",
+ "__webdriver_script_fn",
+ "__fxdriver_evaluate",
+ "__driver_unwrapped",
+ "__webdriver_unwrapped",
+ "__driver_evaluate",
+ "__selenium_unwrapped",
+ "__fxdriver_unwrapped",
+ "_phantom",
+ "callPhantom",
+ "__nightmare",
+ "domAutomation",
+ "domAutomationController"
+ ];
+ const hasAutomationGlobal = automationGlobalNames.some((name) => name in window || name in document);
+ if (navigator.webdriver === true || hasAutomationGlobal) {
+ addSignal("automationApi");
+ }
+ if ((outerHeight === 0 || outerWidth === 0) && !isPrerendering()) {
+ addSignal("zeroOuterDimensions");
+ }
+ for (const signal of getScreenDimensionSignals(screenWidth, screenHeight, userAgent)) {
+ addSignal(signal);
+ }
+ if (Number.isFinite(outerWidth) && Number.isFinite(outerHeight) && Number.isFinite(innerWidth) && Number.isFinite(innerHeight) && outerWidth > 0 && outerHeight > 0 && innerWidth > 0 && innerHeight > 0 && (outerWidth + 8 < innerWidth || outerHeight + 8 < innerHeight)) {
+ addSignal("outerDimensionsWeird");
+ }
+ let hasPluginOrApiAbsence = false;
+ if (!window.chrome && isChromeLike) {
+ addSignal("missingChrome");
+ hasPluginOrApiAbsence = true;
+ }
+ try {
+ const canvas = document.createElement("canvas");
+ const gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
+ if (gl) {
+ try {
+ const rendererParts = [];
+ const rendererRaw = gl.getParameter(gl.RENDERER);
+ if (typeof rendererRaw === "string") {
+ rendererParts.push(rendererRaw);
+ }
+ try {
+ const debugInfo = gl.getExtension("WEBGL_debug_renderer_info");
+ if (debugInfo) {
+ const unmaskedRaw = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
+ if (typeof unmaskedRaw === "string") {
+ rendererParts.push(unmaskedRaw);
+ }
+ }
+ } catch {
+ }
+ if (rendererParts.join(" ").toLowerCase().includes("swiftshader")) {
+ addSignal("swiftShader");
+ }
+ } finally {
+ releaseWebGlContext(canvas, gl);
+ }
+ }
+ } catch {
+ }
+ if ((!navigator.plugins || navigator.plugins.length === 0) && isChromeLike) {
+ addSignal("emptyPlugins");
+ hasPluginOrApiAbsence = true;
+ }
+ if (hasPluginOrApiAbsence) {
+ addSignal("pluginApiAbsence");
+ }
+ } catch (e2) {
+ }
+ return {
+ score: Math.min(score, MAX_CLIENT_BOT_SCORE),
+ mask
+ };
+ }
+ function releaseWebGlContext(canvas, gl) {
+ try {
+ const loseContextExt = gl.getExtension("WEBGL_lose_context");
+ loseContextExt?.loseContext?.();
+ } catch {
+ }
+ canvas.width = 0;
+ canvas.height = 0;
+ }
+
// sessionReplay.ts
var SAMPLE_STORAGE_KEY = "rybbit-replay-sampled";
+ var FULL_SNAPSHOT_REQUEST_INTERVAL_MS = 1e4;
function shouldSampleSession(sampleRate) {
if (sampleRate >= 100) return true;
if (sampleRate <= 0) return false;
@@ -309,9 +511,20 @@
// stays off for the rest of this page.
this.capped = false;
this.eventBuffer = [];
+ // Batches whose send failed, each kept as it was built so a retry after
+ // identify(), clearUserId() or a navigation still carries the identity and
+ // page it was recorded under.
+ this.failedBatches = [];
+ this.lastFullSnapshotRequestAt = -Infinity;
this.config = config;
this.userId = userId;
this.sendBatch = sendBatch;
+ document.addEventListener("visibilitychange", () => {
+ if (document.visibilityState === "hidden") {
+ void this.flushEvents();
+ }
+ });
+ window.addEventListener("pagehide", () => void this.flushEvents());
}
async initialize() {
if (!this.config.enableSessionReplay) {
@@ -424,7 +637,11 @@
}
this.isRecording = false;
this.clearBatchTimer();
- if (this.eventBuffer.length > 0) {
+ if (this.deferredSnapshotTimer !== void 0) {
+ clearTimeout(this.deferredSnapshotTimer);
+ this.deferredSnapshotTimer = void 0;
+ }
+ if (this.eventBuffer.length > 0 || this.failedBatches.length > 0) {
this.flushEvents();
}
}
@@ -440,7 +657,7 @@
setupBatchTimer() {
this.clearBatchTimer();
this.batchTimer = window.setInterval(() => {
- if (this.eventBuffer.length > 0) {
+ if (this.eventBuffer.length > 0 || this.failedBatches.length > 0) {
this.flushEvents();
}
}, this.config.sessionReplayBatchInterval);
@@ -451,30 +668,77 @@
this.batchTimer = void 0;
}
}
+ // Sends the buffered events, and retries the batches that failed before.
async flushEvents() {
- if (this.eventBuffer.length === 0) {
- return;
+ const batches = this.failedBatches;
+ this.failedBatches = [];
+ if (this.eventBuffer.length > 0) {
+ batches.push({
+ userId: this.userId,
+ events: this.eventBuffer,
+ metadata: {
+ pageUrl: window.location.href,
+ viewportWidth: screen.width,
+ viewportHeight: screen.height,
+ language: navigator.language
+ },
+ _bsm: getBotSignalMask()
+ });
+ this.eventBuffer = [];
}
- const events = [...this.eventBuffer];
- this.eventBuffer = [];
- const batch = {
- userId: this.userId,
- events,
- metadata: {
- pageUrl: window.location.href,
- viewportWidth: screen.width,
- viewportHeight: screen.height,
- language: navigator.language
- }
- };
+ await Promise.all(batches.map((batch) => this.sendOrRequeue(batch)));
+ }
+ async sendOrRequeue(batch) {
try {
const result = await this.sendBatch(batch);
if (result?.stopRecording) {
this.stopForSizeCap();
+ } else if (result?.takeFullSnapshot) {
+ this.takeFullSnapshot();
+ } else if (this.deferredSnapshotTimer !== void 0 && batch.events.some((event) => event.type === 2)) {
+ clearTimeout(this.deferredSnapshotTimer);
+ this.deferredSnapshotTimer = void 0;
}
} catch (error) {
- this.eventBuffer.unshift(...events);
+ if (!this.capped) {
+ this.requeue(batch);
+ }
+ }
+ }
+ // A failure from the same identity and page as the last queued one joins it,
+ // so an outage retries one batch per flush rather than one per failed send.
+ requeue(batch) {
+ const last = this.failedBatches[this.failedBatches.length - 1];
+ if (last && last.userId === batch.userId && JSON.stringify(last.metadata) === JSON.stringify(batch.metadata)) {
+ last.events = last.events.concat(batch.events);
+ return;
+ }
+ this.failedBatches.push(batch);
+ }
+ // The server keeps the replay in its previous session until a batch opens
+ // the visitor's new one with a full snapshot, without which a replay can't
+ // be played. Events recorded before the snapshot go first, to the old one.
+ takeFullSnapshot() {
+ if (!this.isRecording) {
+ return;
+ }
+ const wait = this.lastFullSnapshotRequestAt + FULL_SNAPSHOT_REQUEST_INTERVAL_MS - Date.now();
+ if (wait > 0) {
+ if (this.deferredSnapshotTimer === void 0) {
+ this.deferredSnapshotTimer = window.setTimeout(() => {
+ this.deferredSnapshotTimer = void 0;
+ this.takeFullSnapshot();
+ }, wait);
+ }
+ return;
+ }
+ this.lastFullSnapshotRequestAt = Date.now();
+ void this.flushEvents();
+ try {
+ window.rrweb?.record.takeFullSnapshot?.(true);
+ } catch {
}
+ void this.flushEvents();
}
// Unlike stopRecording, drops what's buffered: the server refuses it anyway.
stopForSizeCap() {
@@ -486,6 +750,7 @@
this.isRecording = false;
this.clearBatchTimer();
this.eventBuffer = [];
+ this.failedBatches = [];
}
// Update user ID when it changes
updateUserId(userId) {
@@ -509,210 +774,21 @@
}
};
- // ../../../shared/src/botSignalContract.ts
- var CLIENT_BOT_SIGNAL_MASKS = {
- automationApi: 1 << 0,
- zeroOuterDimensions: 1 << 1,
- missingChrome: 1 << 2,
- swiftShader: 1 << 3,
- emptyPlugins: 1 << 4,
- defaultViewport800x600: 1 << 5,
- defaultViewport1024x768: 1 << 6,
- impossibleDimensions: 1 << 7,
- outerDimensionsWeird: 1 << 8,
- pluginApiAbsence: 1 << 9,
- defaultViewport1280x1200: 1 << 10,
- squareScreen: 1 << 11,
- missingScreenDimensions: 1 << 12
- };
- var CLIENT_BOT_SIGNAL_NAMES = Object.keys(CLIENT_BOT_SIGNAL_MASKS);
- var CLIENT_BOT_SIGNAL_WEIGHTS = {
- automationApi: 3,
- zeroOuterDimensions: 2,
- missingChrome: 1,
- swiftShader: 1,
- emptyPlugins: 1,
- defaultViewport800x600: 3,
- defaultViewport1024x768: 3,
- impossibleDimensions: 3,
- outerDimensionsWeird: 2,
- pluginApiAbsence: 0,
- defaultViewport1280x1200: 3,
- squareScreen: 3,
- missingScreenDimensions: 1
- };
- var ALL_CLIENT_BOT_SIGNAL_BITS = CLIENT_BOT_SIGNAL_NAMES.reduce(
- (mask, name) => mask | CLIENT_BOT_SIGNAL_MASKS[name],
- 0
- );
- var STRONG_CLIENT_BOT_SIGNAL_BITS = CLIENT_BOT_SIGNAL_MASKS.automationApi | CLIENT_BOT_SIGNAL_MASKS.impossibleDimensions | CLIENT_BOT_SIGNAL_MASKS.defaultViewport800x600 | CLIENT_BOT_SIGNAL_MASKS.defaultViewport1024x768 | CLIENT_BOT_SIGNAL_MASKS.defaultViewport1280x1200 | CLIENT_BOT_SIGNAL_MASKS.squareScreen;
- var MAX_CLIENT_BOT_SCORE = 10;
- var MIN_PLAUSIBLE_SCREEN_DIMENSION = 200;
- var MAX_PLAUSIBLE_SCREEN_DIMENSION = 8192;
- var IMPLAUSIBLE_DESKTOP_VIEWPORTS = [
- { width: 800, height: 600, signal: "defaultViewport800x600" },
- { width: 1024, height: 768, signal: "defaultViewport1024x768" },
- { width: 1280, height: 1200, signal: "defaultViewport1280x1200" }
- ];
- function isPlausibleScreenDimensions(width, height) {
- return Number.isFinite(width) && Number.isFinite(height) && width >= MIN_PLAUSIBLE_SCREEN_DIMENSION && height >= MIN_PLAUSIBLE_SCREEN_DIMENSION && width <= MAX_PLAUSIBLE_SCREEN_DIMENSION && height <= MAX_PLAUSIBLE_SCREEN_DIMENSION;
- }
- function isDesktopUserAgent(userAgent) {
- return /Windows NT|Macintosh|X11|Linux x86_64/.test(userAgent) && !/Mobile|Android|iPhone|iPad/.test(userAgent);
- }
- function getScreenDimensionSignals(width, height, userAgent) {
- if (!isPlausibleScreenDimensions(width, height)) {
- return ["impossibleDimensions"];
- }
- const signals = [];
- if (width === height) {
- signals.push("squareScreen");
- }
- if (isDesktopUserAgent(userAgent)) {
- for (const viewport of IMPLAUSIBLE_DESKTOP_VIEWPORTS) {
- if (width === viewport.width && height === viewport.height) {
- signals.push(viewport.signal);
- }
- }
- }
- return signals;
- }
-
- // botSignals.ts
- var cachedBotSignals = null;
- function getBotScore() {
- return getBotSignals().score;
- }
- function getBotSignalMask() {
- return getBotSignals().mask;
- }
- function isPrerendering() {
- return document.prerendering === true;
- }
- function getBotSignals() {
- if (isPrerendering()) {
- return calculateBotSignals();
- }
- cachedBotSignals ?? (cachedBotSignals = calculateBotSignals());
- return cachedBotSignals;
- }
- function calculateBotSignals() {
- let score = 0;
- let mask = 0;
- function addSignal(name) {
- const signalMask = CLIENT_BOT_SIGNAL_MASKS[name];
- if ((mask & signalMask) !== 0) {
- return;
- }
- mask |= signalMask;
- score += CLIENT_BOT_SIGNAL_WEIGHTS[name];
- }
- try {
- const userAgent = navigator.userAgent;
- const isChromeLike = /Chrome\//.test(userAgent) && !/\bwv\b|; wv\)/.test(userAgent);
- const screenWidth = Number(window.screen?.width);
- const screenHeight = Number(window.screen?.height);
- const outerWidth = Number(window.outerWidth);
- const outerHeight = Number(window.outerHeight);
- const innerWidth = Number(window.innerWidth);
- const innerHeight = Number(window.innerHeight);
- const automationGlobalNames = [
- "__webdriver_evaluate",
- "__selenium_evaluate",
- "__webdriver_script_function",
- "__webdriver_script_func",
- "__webdriver_script_fn",
- "__fxdriver_evaluate",
- "__driver_unwrapped",
- "__webdriver_unwrapped",
- "__driver_evaluate",
- "__selenium_unwrapped",
- "__fxdriver_unwrapped",
- "_phantom",
- "callPhantom",
- "__nightmare",
- "domAutomation",
- "domAutomationController"
- ];
- const hasAutomationGlobal = automationGlobalNames.some((name) => name in window || name in document);
- if (navigator.webdriver === true || hasAutomationGlobal) {
- addSignal("automationApi");
- }
- if ((outerHeight === 0 || outerWidth === 0) && !isPrerendering()) {
- addSignal("zeroOuterDimensions");
- }
- for (const signal of getScreenDimensionSignals(screenWidth, screenHeight, userAgent)) {
- addSignal(signal);
- }
- if (Number.isFinite(outerWidth) && Number.isFinite(outerHeight) && Number.isFinite(innerWidth) && Number.isFinite(innerHeight) && outerWidth > 0 && outerHeight > 0 && innerWidth > 0 && innerHeight > 0 && (outerWidth + 8 < innerWidth || outerHeight + 8 < innerHeight)) {
- addSignal("outerDimensionsWeird");
- }
- let hasPluginOrApiAbsence = false;
- if (!window.chrome && isChromeLike) {
- addSignal("missingChrome");
- hasPluginOrApiAbsence = true;
- }
- try {
- const canvas = document.createElement("canvas");
- const gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
- if (gl) {
- try {
- const rendererParts = [];
- const rendererRaw = gl.getParameter(gl.RENDERER);
- if (typeof rendererRaw === "string") {
- rendererParts.push(rendererRaw);
- }
- try {
- const debugInfo = gl.getExtension("WEBGL_debug_renderer_info");
- if (debugInfo) {
- const unmaskedRaw = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
- if (typeof unmaskedRaw === "string") {
- rendererParts.push(unmaskedRaw);
- }
- }
- } catch {
- }
- if (rendererParts.join(" ").toLowerCase().includes("swiftshader")) {
- addSignal("swiftShader");
- }
- } finally {
- releaseWebGlContext(canvas, gl);
- }
- }
- } catch {
- }
- if ((!navigator.plugins || navigator.plugins.length === 0) && isChromeLike) {
- addSignal("emptyPlugins");
- hasPluginOrApiAbsence = true;
- }
- if (hasPluginOrApiAbsence) {
- addSignal("pluginApiAbsence");
- }
- } catch (e2) {
- }
- return {
- score: Math.min(score, MAX_CLIENT_BOT_SCORE),
- mask
- };
- }
- function releaseWebGlContext(canvas, gl) {
- try {
- const loseContextExt = gl.getExtension("WEBGL_lose_context");
- loseContextExt?.loseContext?.();
- } catch {
- }
- canvas.width = 0;
- canvas.height = 0;
- }
-
// tracking.ts
var FEATURE_FLAG_REQUEST_TIMEOUT_MS2 = 2e3;
+ var MAX_REFERRER_LENGTH = 2048;
+ var MAX_PAGE_TITLE_LENGTH = 512;
+ var MAX_PROPERTIES_LENGTH = 2048;
+ var MAX_OUTBOUND_TEXT_LENGTH = 500;
+ var REPLAY_KEEPALIVE_BUDGET_BYTES = 48 * 1024;
+ var RETRYABLE_REPLAY_STATUSES = [408, 429, 502, 503, 504];
var Tracker = class {
constructor(config) {
this.customUserId = null;
this.errorDedupeCache = /* @__PURE__ */ new Map();
this.errorDedupeLastCleanup = 0;
this.exposedFeatureFlags = /* @__PURE__ */ new Set();
+ this.replayKeepaliveBytesInFlight = 0;
this.config = config;
this.lastPageviewUrl = this.getPageviewUrl();
this.loadUserId();
@@ -803,7 +879,19 @@
console.error("Failed to initialize session replay:", error);
}
}
+ /**
+ * Resolves once the server has dealt with the batch (stored or deliberately
+ * skipped it); rejects when it is worth sending again, which the recorder does.
+ * Every batch that fits what is left of the keepalive budget is sent with
+ * keepalive, so a navigation can't cancel it: a batch already on its way
+ * when the page hides is out of the buffer that pagehide flushes.
+ */
async sendSessionReplayBatch(batch) {
+ const body = JSON.stringify(batch);
+ const keepaliveRoom = REPLAY_KEEPALIVE_BUDGET_BYTES - this.replayKeepaliveBytesInFlight;
+ const keepaliveBytes = body.length <= keepaliveRoom ? new Blob([body]).size : Infinity;
+ const useKeepalive = keepaliveBytes <= keepaliveRoom;
+ if (useKeepalive) this.replayKeepaliveBytesInFlight += keepaliveBytes;
let response;
try {
response = await fetch(`${this.config.analyticsHost}/session-replay/record/${this.config.siteId}`, {
@@ -811,14 +899,21 @@
headers: {
"Content-Type": "application/json"
},
- body: JSON.stringify(batch),
+ body,
mode: "cors",
- keepalive: false
- // Disable keepalive for large session replay requests
+ keepalive: useKeepalive
});
} catch (error) {
console.error("Failed to send session replay batch:", error);
throw error;
+ } finally {
+ if (useKeepalive) this.replayKeepaliveBytesInFlight -= keepaliveBytes;
+ }
+ if (!response.ok) {
+ if (RETRYABLE_REPLAY_STATUSES.includes(response.status)) {
+ throw new Error(`Session replay batch failed with status ${response.status}`);
+ }
+ return;
}
try {
return await response.json();
@@ -844,8 +939,8 @@
screenWidth: screen.width,
screenHeight: screen.height,
language: navigator.language,
- page_title: document.title,
- referrer: document.referrer,
+ page_title: truncateText(document.title, MAX_PAGE_TITLE_LENGTH),
+ referrer: truncateText(document.referrer, MAX_REFERRER_LENGTH),
_bs: getBotScore(),
_bsm: getBotSignalMask()
};
@@ -957,7 +1052,12 @@
);
}
trackOutbound(url, text = "", target = "_self") {
- this.track("outbound", "", { url, text, target });
+ const properties = { url, text: truncateText(text, MAX_OUTBOUND_TEXT_LENGTH), target };
+ const overflow = JSON.stringify(properties).length - MAX_PROPERTIES_LENGTH;
+ if (overflow > 0) {
+ properties.url = truncateText(url, Math.max(0, url.length - overflow));
+ }
+ this.track("outbound", "", properties);
}
trackWebVitals(vitals) {
const basePayload = this.createBasePayload();
@@ -1564,6 +1664,9 @@
};
// formTracking.ts
+ function getFormProperty(form, key) {
+ return Object.getOwnPropertyDescriptor(HTMLFormElement.prototype, key)?.get?.call(form);
+ }
var FormTrackingManager = class {
constructor(tracker, config) {
this.tracker = tracker;
@@ -1581,13 +1684,13 @@
}
handleSubmit(event) {
const form = event.target;
- if (form.tagName !== "FORM") return;
+ if (!(form instanceof HTMLFormElement)) return;
const properties = {
- formId: form.id || "",
- formName: form.name || "",
- formAction: form.action || "",
- method: (form.method || "get").toUpperCase(),
- fieldCount: form.elements.length,
+ formId: form.getAttribute("id") || "",
+ formName: form.getAttribute("name") || "",
+ formAction: getFormProperty(form, "action") || "",
+ method: (getFormProperty(form, "method") || "get").toUpperCase(),
+ fieldCount: getFormProperty(form, "elements")?.length ?? 0,
ariaLabel: form.getAttribute("aria-label") || void 0,
...this.extractDataAttributes(form)
};
@@ -1609,8 +1712,8 @@
element: tagName.toLowerCase(),
inputType: tagName === "INPUT" ? target.type?.toLowerCase() : void 0,
inputName,
- formId: target.form?.id || void 0,
- formName: target.form?.name || void 0,
+ formId: target.form?.getAttribute("id") || void 0,
+ formName: target.form?.getAttribute("name") || void 0,
...this.extractDataAttributes(target)
};
this.tracker.trackInputChange(properties);
@@ -1628,6 +1731,14 @@
};
// index.ts
+ function hasOptedOut(optOutKey) {
+ if (window.__RYBBIT_OPTOUT__) return true;
+ try {
+ return localStorage.getItem(optOutKey) !== null;
+ } catch {
+ return false;
+ }
+ }
(async function() {
const scriptTag = document.currentScript || document.querySelector('script[src*="/script.js"]');
if (!scriptTag) {
@@ -1636,7 +1747,7 @@
}
const namespace = scriptTag.getAttribute("data-namespace") || "rybbit";
const optOutKey = `disable-${namespace}`;
- if (window.__RYBBIT_OPTOUT__ || localStorage.getItem(optOutKey) !== null) {
+ if (hasOptedOut(optOutKey)) {
window[namespace] = {
pageview: () => {
},
@@ -1808,7 +1919,6 @@
window.addEventListener("beforeunload", () => {
clickManager?.cleanup();
copyManager?.cleanup();
- tracker.cleanup();
});
if (config.autoTrackPageview) {
tracker.trackPageview();
diff --git a/server/public/script.js b/server/public/script.js
index 835e50677..cc6a28f53 100644
--- a/server/public/script.js
+++ b/server/public/script.js
@@ -1 +1 @@
-"use strict";(()=>{var $e=Object.defineProperty;var Ke=(n,e,t)=>e in n?$e(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var w=(n,e,t)=>Ke(n,typeof e!="symbol"?e+"":e,t);function qe(n){if(n.startsWith("re:")){let c=n.slice(3);if(!c)throw new Error("Empty regex pattern");return new RegExp(c)}let t="__DOUBLE_ASTERISK_TOKEN__",r="__SINGLE_ASTERISK_TOKEN__",s=n.replace(/\*\*/g,t).replace(/\*/g,r).replace(/[.+?^${}()|[\]\\]/g,"\\$&");s=s.replace(new RegExp(`/${t}/`,"g"),"/(?:.+/)?"),s=s.replace(new RegExp(t,"g"),".*"),s=s.replace(/\//g,"\\/");let o=s.replace(new RegExp(r,"g"),"[^/]+");return new RegExp("^"+o+"$")}function z(n,e){for(let t of e)try{if(qe(t).test(n))return t}catch(r){console.error(`Invalid pattern: ${t}`,r)}return null}function ce(n,e){let t=null;return(...r)=>{t&&clearTimeout(t),t=setTimeout(()=>n(...r),e)}}function ue(n){try{let e=window.location.hostname,t=new URL(n).hostname;return t!==e&&t!==""}catch{return!1}}function I(n,e){if(!n)return e;try{let t=JSON.parse(n);return Array.isArray(e)&&!Array.isArray(t)?e:t}catch(t){return console.error("Error parsing JSON:",t),e}}function _(n,e=!1){return n.hash.startsWith("#/")?n.hash.substring(1):n.hash.startsWith("#!/")?n.hash.substring(2):n.pathname+(e?n.hash:"")}var Je=2e3;function G(){try{if(crypto?.randomUUID)return crypto.randomUUID()}catch{}return`${Date.now().toString(36)}-${Math.random().toString(36).slice(2,12)}`}function Xe(n){let e=`${n}-visitor-id`;try{let t=localStorage.getItem(e);if(t)return t;let r=G();return localStorage.setItem(e,r),r}catch{return G()}}function Qe(n){try{return localStorage.getItem(`${n}-user-id`)||void 0}catch{return}}async function Ye(n,e,t,r,i){let s=new AbortController,o=window.setTimeout(()=>s.abort(),Je);try{let c=new URL(window.location.href),d=await fetch(`${n}/site/${e}/feature-flags/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},credentials:"omit",signal:s.signal,body:JSON.stringify({anonymousId:r,identifiedUserId:Qe(t),hostname:c.hostname,pathname:_(c,i),querystring:c.search,query:Object.fromEntries(c.searchParams.entries()),referrer:document.referrer,language:navigator.language,screenWidth:screen.width,screenHeight:screen.height})});if(!d.ok)return{enabled:!0,flags:{}};let u=await d.json();return{enabled:u?.featureFlagsEnabled!==!1,flags:u?.flags&&typeof u.flags=="object"?u.flags:{}}}catch{return{enabled:!0,flags:{}}}finally{window.clearTimeout(o)}}function Ze(n){try{let e=new URL(n,window.location.href);return e.searchParams.get("siteId")||e.searchParams.get("site-id")||e.searchParams.get("site_id")}catch{return null}}async function de(n){let e=n.getAttribute("src");if(!e)return console.error("Script src attribute is missing"),null;let t=e.split("/script.js")[0];if(!t)return console.error("Please provide a valid analytics host"),null;let r=Ze(e)||n.getAttribute("data-site-id")||n.getAttribute("site-id");if(!r)return console.error("Please provide a valid site ID using the ?siteId= query parameter or the data-site-id attribute"),null;let i=n.getAttribute("data-namespace")||"rybbit",s=G(),o=I(n.getAttribute("data-skip-patterns"),[]),c=I(n.getAttribute("data-mask-patterns"),[]),d=I(n.getAttribute("data-replay-mask-text-selectors"),[]),u=n.getAttribute("data-debounce")?Math.max(0,parseInt(n.getAttribute("data-debounce"))):500,g=n.getAttribute("data-replay-batch-size")?Math.max(1,parseInt(n.getAttribute("data-replay-batch-size"))):250,y=n.getAttribute("data-replay-batch-interval")?Math.max(1e3,parseInt(n.getAttribute("data-replay-batch-interval"))):5e3,T=n.getAttribute("data-replay-block-class")||void 0,h=n.getAttribute("data-replay-block-selector")||void 0,a=n.getAttribute("data-replay-ignore-class")||void 0,l=n.getAttribute("data-replay-ignore-selector")||void 0,p=n.getAttribute("data-replay-mask-text-class")||void 0,S=n.getAttribute("data-replay-mask-all-inputs"),R=S!==null?S!=="false":void 0,C=n.getAttribute("data-replay-mask-input-options"),He=C?I(C,{password:!0,email:!0}):void 0,ie=n.getAttribute("data-replay-collect-fonts"),We=ie!==null?ie!=="false":void 0,se=n.getAttribute("data-replay-sampling"),Ve=se?I(se,{}):void 0,ae=n.getAttribute("data-replay-slim-dom-options"),ze=ae?I(ae,{}):void 0,oe=n.getAttribute("data-replay-sample-rate"),Ge=oe?Math.min(100,Math.max(0,parseInt(oe,10))):void 0,je=n.getAttribute("data-tag")||"",f={namespace:i,analyticsHost:t,siteId:r,visitorId:s,debounceDuration:u,sessionReplayBatchSize:g,sessionReplayBatchInterval:y,sessionReplayMaskTextSelectors:d,skipPatterns:o,maskPatterns:c,autoTrackPageview:!0,autoTrackSpa:!0,trackQuerystring:!0,trackUrlFragments:!1,trackOutbound:!0,enableWebVitals:!1,trackErrors:!1,enableSessionReplay:!1,trackButtonClicks:!1,trackCopy:!1,trackFormInteractions:!1,tag:je,featureFlagsEnabled:!1,featureFlags:{},sessionReplayBlockClass:T,sessionReplayBlockSelector:h,sessionReplayIgnoreClass:a,sessionReplayIgnoreSelector:l,sessionReplayMaskTextClass:p,sessionReplayMaskAllInputs:R,sessionReplayMaskInputOptions:He,sessionReplayCollectFonts:We,sessionReplaySampling:Ve,sessionReplaySlimDOMOptions:ze,sessionReplaySampleRate:Ge},E=f;try{let P=`${t}/site/tracking-config/${r}`,le=await fetch(P,{method:"GET",credentials:"omit"});if(le.ok){let m=await le.json();E={...f,autoTrackPageview:m.trackInitialPageView??f.autoTrackPageview,autoTrackSpa:m.trackSpaNavigation??f.autoTrackSpa,trackQuerystring:m.trackUrlParams??f.trackQuerystring,trackUrlFragments:m.trackUrlFragments??f.trackUrlFragments,trackOutbound:m.trackOutbound??f.trackOutbound,enableWebVitals:m.webVitals??f.enableWebVitals,trackErrors:m.trackErrors??f.trackErrors,enableSessionReplay:m.sessionReplay??f.enableSessionReplay,trackButtonClicks:m.trackButtonClicks??f.trackButtonClicks,trackCopy:m.trackCopy??f.trackCopy,trackFormInteractions:m.trackFormInteractions??f.trackFormInteractions,featureFlagsEnabled:m.featureFlagsEnabled===!0}}else console.warn("Failed to fetch tracking config from API, using defaults")}catch(P){console.warn("Error fetching tracking config:",P)}if(E.featureFlagsEnabled){E.visitorId=Xe(i);let P=await Ye(t,r,i,E.visitorId,E.trackUrlFragments);E.featureFlagsEnabled=P.enabled,E.featureFlags=P.flags}return E}var pe="rybbit-replay-sampled";function et(n){if(n>=100)return!0;if(n<=0)return!1;try{let e=sessionStorage.getItem(pe);if(e!==null)return e==="1";let t=Math.random()*100<n;return sessionStorage.setItem(pe,t?"1":"0"),t}catch{return Math.random()*100<n}}var B=class{constructor(e,t,r){this.isRecording=!1;this.capped=!1;this.eventBuffer=[];this.config=e,this.userId=t,this.sendBatch=r}async initialize(){if(!this.config.enableSessionReplay)return;let e=this.config.sessionReplaySampleRate;e!==void 0&&!et(e)||(window.rrweb||await this.loadRrweb(),window.rrweb&&this.startRecording())}async loadRrweb(){return new Promise((e,t)=>{let r=document.createElement("script");r.src=`${this.config.analyticsHost}/replay.js`,r.async=!1,r.onload=()=>{e()},r.onerror=()=>t(new Error("Failed to load rrweb")),document.head.appendChild(r)})}startRecording(){if(!(this.capped||this.isRecording||!window.rrweb||!this.config.enableSessionReplay))try{let e={mousemove:!1,mouseInteraction:{MouseUp:!1,MouseDown:!1,Click:!0,ContextMenu:!1,DblClick:!0,Focus:!0,Blur:!0,TouchStart:!1,TouchEnd:!1},scroll:500,input:"last",media:800},t={script:!1,comment:!0,headFavicon:!0,headWhitespace:!0,headMetaDescKeywords:!0,headMetaSocial:!0,headMetaRobots:!0,headMetaHttpEquiv:!0,headMetaAuthorship:!0,headMetaVerification:!0},r={emit:i=>{this.addEvent({type:i.type,data:i.data,timestamp:i.timestamp||Date.now()})},recordCanvas:!1,checkoutEveryNms:6e4,checkoutEveryNth:500,blockClass:this.config.sessionReplayBlockClass??"rr-block",blockSelector:this.config.sessionReplayBlockSelector??null,ignoreClass:this.config.sessionReplayIgnoreClass??"rr-ignore",ignoreSelector:this.config.sessionReplayIgnoreSelector??null,maskTextClass:this.config.sessionReplayMaskTextClass??"rr-mask",maskAllInputs:this.config.sessionReplayMaskAllInputs??!0,maskInputOptions:this.config.sessionReplayMaskInputOptions??{password:!0,email:!0},collectFonts:this.config.sessionReplayCollectFonts??!0,sampling:this.config.sessionReplaySampling??e,slimDOMOptions:this.config.sessionReplaySlimDOMOptions??t};this.config.sessionReplayMaskTextSelectors&&this.config.sessionReplayMaskTextSelectors.length>0&&(r.maskTextSelector=this.config.sessionReplayMaskTextSelectors.join(", ")),this.stopRecordingFn=window.rrweb.record(r),this.isRecording=!0,this.setupBatchTimer()}catch{}}stopRecording(){this.isRecording&&(this.stopRecordingFn&&this.stopRecordingFn(),this.isRecording=!1,this.clearBatchTimer(),this.eventBuffer.length>0&&this.flushEvents())}isActive(){return this.isRecording}addEvent(e){this.eventBuffer.push(e),this.eventBuffer.length>=this.config.sessionReplayBatchSize&&this.flushEvents()}setupBatchTimer(){this.clearBatchTimer(),this.batchTimer=window.setInterval(()=>{this.eventBuffer.length>0&&this.flushEvents()},this.config.sessionReplayBatchInterval)}clearBatchTimer(){this.batchTimer&&(clearInterval(this.batchTimer),this.batchTimer=void 0)}async flushEvents(){if(this.eventBuffer.length===0)return;let e=[...this.eventBuffer];this.eventBuffer=[];let t={userId:this.userId,events:e,metadata:{pageUrl:window.location.href,viewportWidth:screen.width,viewportHeight:screen.height,language:navigator.language}};try{(await this.sendBatch(t))?.stopRecording&&this.stopForSizeCap()}catch{this.eventBuffer.unshift(...e)}}stopForSizeCap(){this.capped=!0,this.stopRecordingFn&&(this.stopRecordingFn(),this.stopRecordingFn=void 0),this.isRecording=!1,this.clearBatchTimer(),this.eventBuffer=[]}updateUserId(e){e!==this.userId&&(this.eventBuffer.length>0&&this.flushEvents(),this.userId=e)}onPageChange(){this.isRecording&&this.flushEvents()}cleanup(){this.stopRecording()}};var b={automationApi:1,zeroOuterDimensions:2,missingChrome:4,swiftShader:8,emptyPlugins:16,defaultViewport800x600:32,defaultViewport1024x768:64,impossibleDimensions:128,outerDimensionsWeird:256,pluginApiAbsence:512,defaultViewport1280x1200:1024,squareScreen:2048,missingScreenDimensions:4096},tt=Object.keys(b),fe={automationApi:3,zeroOuterDimensions:2,missingChrome:1,swiftShader:1,emptyPlugins:1,defaultViewport800x600:3,defaultViewport1024x768:3,impossibleDimensions:3,outerDimensionsWeird:2,pluginApiAbsence:0,defaultViewport1280x1200:3,squareScreen:3,missingScreenDimensions:1},bt=tt.reduce((n,e)=>n|b[e],0),vt=b.automationApi|b.impossibleDimensions|b.defaultViewport800x600|b.defaultViewport1024x768|b.defaultViewport1280x1200|b.squareScreen,me=10,ge=200,he=8192,rt=[{width:800,height:600,signal:"defaultViewport800x600"},{width:1024,height:768,signal:"defaultViewport1024x768"},{width:1280,height:1200,signal:"defaultViewport1280x1200"}];function nt(n,e){return Number.isFinite(n)&&Number.isFinite(e)&&n>=ge&&e>=ge&&n<=he&&e<=he}function it(n){return/Windows NT|Macintosh|X11|Linux x86_64/.test(n)&&!/Mobile|Android|iPhone|iPad/.test(n)}function ye(n,e,t){if(!nt(n,e))return["impossibleDimensions"];let r=[];if(n===e&&r.push("squareScreen"),it(t))for(let i of rt)n===i.width&&e===i.height&&r.push(i.signal);return r}var be=null;function ke(){return Re().score}function we(){return Re().mask}function Se(){return document.prerendering===!0}function Re(){return Se()?ve():(be??(be=ve()),be)}function ve(){let n=0,e=0;function t(r){let i=b[r];(e&i)===0&&(e|=i,n+=fe[r])}try{let r=navigator.userAgent,i=/Chrome\//.test(r)&&!/\bwv\b|; wv\)/.test(r),s=Number(window.screen?.width),o=Number(window.screen?.height),c=Number(window.outerWidth),d=Number(window.outerHeight),u=Number(window.innerWidth),g=Number(window.innerHeight),T=["__webdriver_evaluate","__selenium_evaluate","__webdriver_script_function","__webdriver_script_func","__webdriver_script_fn","__fxdriver_evaluate","__driver_unwrapped","__webdriver_unwrapped","__driver_evaluate","__selenium_unwrapped","__fxdriver_unwrapped","_phantom","callPhantom","__nightmare","domAutomation","domAutomationController"].some(a=>a in window||a in document);(navigator.webdriver===!0||T)&&t("automationApi"),(d===0||c===0)&&!Se()&&t("zeroOuterDimensions");for(let a of ye(s,o,r))t(a);Number.isFinite(c)&&Number.isFinite(d)&&Number.isFinite(u)&&Number.isFinite(g)&&c>0&&d>0&&u>0&&g>0&&(c+8<u||d+8<g)&&t("outerDimensionsWeird");let h=!1;!window.chrome&&i&&(t("missingChrome"),h=!0);try{let a=document.createElement("canvas"),l=a.getContext("webgl")||a.getContext("experimental-webgl");if(l)try{let p=[],S=l.getParameter(l.RENDERER);typeof S=="string"&&p.push(S);try{let R=l.getExtension("WEBGL_debug_renderer_info");if(R){let C=l.getParameter(R.UNMASKED_RENDERER_WEBGL);typeof C=="string"&&p.push(C)}}catch{}p.join(" ").toLowerCase().includes("swiftshader")&&t("swiftShader")}finally{st(a,l)}}catch{}(!navigator.plugins||navigator.plugins.length===0)&&i&&(t("emptyPlugins"),h=!0),h&&t("pluginApiAbsence")}catch{}return{score:Math.min(n,me),mask:e}}function st(n,e){try{e.getExtension("WEBGL_lose_context")?.loseContext?.()}catch{}n.width=0,n.height=0}var at=2e3,x=class{constructor(e){this.customUserId=null;this.errorDedupeCache=new Map;this.errorDedupeLastCleanup=0;this.exposedFeatureFlags=new Set;this.config=e,this.lastPageviewUrl=this.getPageviewUrl(),this.loadUserId(),e.enableSessionReplay&&this.initializeSessionReplay()}serializeFeatureFlagValue(e){if(e==null)return"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return String(e);try{return JSON.stringify(e)}catch{return""}}getFeatureFlagEventPayload(){let e={};for(let[t,r]of Object.entries(this.config.featureFlags||{}))e[t]=this.serializeFeatureFlagValue(r.value);return e}getCurrentUrlContext(){let e=new URL(window.location.href),t=_(e,this.config.trackUrlFragments);return{hostname:e.hostname,pathname:t,querystring:e.search,query:Object.fromEntries(e.searchParams.entries()),referrer:document.referrer,language:navigator.language,screenWidth:screen.width,screenHeight:screen.height}}async refreshFeatureFlags(){if(!this.config.featureFlagsEnabled)return;let e=new AbortController,t=window.setTimeout(()=>e.abort(),at);try{let r=await fetch(`${this.config.analyticsHost}/site/${this.config.siteId}/feature-flags/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({anonymousId:this.config.visitorId,identifiedUserId:this.customUserId||void 0,...this.getCurrentUrlContext()}),mode:"cors",credentials:"omit",keepalive:!0,signal:e.signal});if(!r.ok)return;let i=await r.json();this.config.featureFlags=i?.flags&&typeof i.flags=="object"?i.flags:{},i?.featureFlagsEnabled===!1&&(this.config.featureFlagsEnabled=!1)}catch{}finally{window.clearTimeout(t)}}loadUserId(){try{let e=localStorage.getItem(`${this.config.namespace}-user-id`);e&&(this.customUserId=e)}catch{}}async initializeSessionReplay(){try{this.sessionReplayRecorder=new B(this.config,this.customUserId||"",e=>this.sendSessionReplayBatch(e)),await this.sessionReplayRecorder.initialize()}catch(e){console.error("Failed to initialize session replay:",e)}}async sendSessionReplayBatch(e){let t;try{t=await fetch(`${this.config.analyticsHost}/session-replay/record/${this.config.siteId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),mode:"cors",keepalive:!1})}catch(r){throw console.error("Failed to send session replay batch:",r),r}try{return await t.json()}catch{return}}createBasePayload(){let e=new URL(window.location.href),t=_(e,this.config.trackUrlFragments);if(z(t,this.config.skipPatterns))return null;let r=z(t,this.config.maskPatterns);r&&(t=r);let i={site_id:this.config.siteId,hostname:e.hostname,pathname:t,querystring:this.config.trackQuerystring?e.search:"",screenWidth:screen.width,screenHeight:screen.height,language:navigator.language,page_title:document.title,referrer:document.referrer,_bs:ke(),_bsm:we()};this.customUserId&&(i.user_id=this.customUserId),this.config.tag&&(i.tag=this.config.tag);let s=this.getFeatureFlagEventPayload();return Object.keys(s).length>0&&(i.feature_flags=s),i}async sendTrackingData(e){try{await fetch(`${this.config.analyticsHost}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),mode:"cors",keepalive:!0})}catch(t){console.error("Failed to send tracking data:",t)}}track(e,t="",r={}){if(e==="custom_event"&&(!t||typeof t!="string")){console.error("Event name is required and must be a string for custom events");return}let i=this.createBasePayload();if(!i)return;let o={...i,type:e,event_name:t,properties:["custom_event","outbound","error","button_click","copy","form_submit","input_change"].includes(e)?JSON.stringify(r):void 0};this.sendTrackingData(o)}trackPageview(){this.lastPageviewUrl=this.getPageviewUrl(),this.track("pageview")}trackPageviewIfUrlChanged(){this.getPageviewUrl()!==this.lastPageviewUrl&&this.trackPageview()}getPageviewUrl(){let e=new URL(window.location.href);return JSON.stringify([e.hostname,_(e,this.config.trackUrlFragments),this.config.trackQuerystring?e.search:""])}trackEvent(e,t={}){this.track("custom_event",e,t)}getFeatureFlag(e,t){let r=this.config.featureFlags?.[e];if(!r)return t;let i=`${e}:${r.version}:${this.serializeFeatureFlagValue(r.value)}`;return this.exposedFeatureFlags.has(i)||(this.exposedFeatureFlags.add(i),this.trackEvent("feature_flag_exposure",{key:e,value:this.serializeFeatureFlagValue(r.value),version:r.version,reason:r.reason})),r.value}getFeatureFlags(){return Object.fromEntries(Object.entries(this.config.featureFlags||{}).map(([e,t])=>[e,t.value]))}getFeatureFlagPayload(e,t){let r=this.config.featureFlags?.[e];return!r||r.payload===void 0?t:r.payload}getFeatureFlagPayloads(){return Object.fromEntries(Object.entries(this.config.featureFlags||{}).filter(([,e])=>e.payload!==void 0).map(([e,t])=>[e,t.payload]))}trackOutbound(e,t="",r="_self"){this.track("outbound","",{url:e,text:t,target:r})}trackWebVitals(e){let t=this.createBasePayload();if(!t)return;let r={...t,type:"performance",event_name:"web-vitals",...e};this.sendTrackingData(r)}trackError(e,t={}){let r=e?.message||"";if(r.includes("ResizeObserver loop completed with undelivered notifications")||r.includes("ResizeObserver loop limit exceeded"))return;let i=window.location.origin,s=t.filename||"",o=e.stack||"";if(s)try{if(new URL(s).origin!==i)return}catch{}else if(o&&!o.includes(i))return;let d=[e.name||"Error",r,t.filename||"",t.lineno??"",t.colno??""].join("|"),u=Date.now(),g=6e4,y=this.errorDedupeCache.get(d);if(y&&u-y<g)return;this.errorDedupeCache.set(d,u);let T=10*6e4;if(u-this.errorDedupeLastCleanup>g){for(let[a,l]of this.errorDedupeCache.entries())u-l>T&&this.errorDedupeCache.delete(a);this.errorDedupeLastCleanup=u}let h={message:e.message?.substring(0,500)||"Unknown error",stack:o.substring(0,2e3)||""};if(s&&(h.fileName=s),t.lineno){let a=typeof t.lineno=="string"?parseInt(t.lineno,10):t.lineno;a&&a!==0&&(h.lineNumber=a)}if(t.colno){let a=typeof t.colno=="string"?parseInt(t.colno,10):t.colno;a&&a!==0&&(h.columnNumber=a)}for(let a in t)!["lineno","colno"].includes(a)&&t[a]!==void 0&&(h[a]=t[a]);this.track("error",e.name||"Error",h)}trackButtonClick(e){this.track("button_click","",e)}trackCopy(e){this.track("copy","",e)}trackFormSubmit(e){this.track("form_submit","",e)}trackInputChange(e){this.track("input_change","",e)}identify(e,t){if(typeof e!="string"||e.trim()===""){console.error("User ID must be a non-empty string");return}this.customUserId=e.trim();try{localStorage.setItem(`${this.config.namespace}-user-id`,this.customUserId)}catch{console.warn("Could not persist user ID to localStorage")}this.sendIdentifyEvent(this.customUserId,t,!0).then(()=>this.refreshFeatureFlags()),this.sessionReplayRecorder&&this.sessionReplayRecorder.updateUserId(this.customUserId)}setTraits(e){if(!e||typeof e!="object"){console.error("Traits must be an object");return}let t=this.customUserId;if(!t){console.warn("Cannot set traits without identifying user first. Call identify() first.");return}this.sendIdentifyEvent(t,e,!1).then(()=>this.refreshFeatureFlags())}async sendIdentifyEvent(e,t,r=!0){try{await fetch(`${this.config.analyticsHost}/identify`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({site_id:this.config.siteId,user_id:e,traits:t,is_new_identify:r}),mode:"cors",keepalive:!0})}catch(i){console.error("Failed to send identify event:",i)}}clearUserId(){this.customUserId=null;try{localStorage.removeItem(`${this.config.namespace}-user-id`)}catch{}this.sessionReplayRecorder&&this.sessionReplayRecorder.updateUserId(""),this.refreshFeatureFlags()}getUserId(){return this.customUserId}startSessionReplay(){this.sessionReplayRecorder?this.sessionReplayRecorder.startRecording():console.warn("Session replay not initialized")}stopSessionReplay(){this.sessionReplayRecorder&&this.sessionReplayRecorder.stopRecording()}isSessionReplayActive(){return this.sessionReplayRecorder?.isActive()??!1}onPageChange(){this.refreshFeatureFlags(),this.sessionReplayRecorder&&this.sessionReplayRecorder.onPageChange()}cleanup(){this.sessionReplayRecorder&&this.sessionReplayRecorder.cleanup()}};var Le=-1,F=n=>{addEventListener("pageshow",(e=>{e.persisted&&(Le=e.timeStamp,n(e))}),!0)},v=(n,e,t,r)=>{let i,s;return o=>{e.value>=0&&(o||r)&&(s=e.value-(i??0),(s||i===void 0)&&(i=e.value,e.delta=s,e.rating=((c,d)=>c>d[1]?"poor":c>d[0]?"needs-improvement":"good")(e.value,t),n(e)))}},Z=n=>{requestAnimationFrame((()=>requestAnimationFrame((()=>n()))))},ee=()=>{let n=performance.getEntriesByType("navigation")[0];if(n&&n.responseStart>0&&n.responseStart<performance.now())return n},L=()=>ee()?.activationStart??0,k=(n,e=-1)=>{let t=ee(),r="navigate";return Le>=0?r="back-forward-cache":t&&(document.prerendering||L()>0?r="prerender":document.wasDiscarded?r="restore":t.type&&(r=t.type.replace(/_/g,"-"))),{name:n,value:e,rating:"good",delta:0,entries:[],id:`v5-${Date.now()}-${Math.floor(8999999999999*Math.random())+1e12}`,navigationType:r}},j=new WeakMap;function te(n,e){return j.get(n)||j.set(n,new e),j.get(n)}var K=class{constructor(){w(this,"t");w(this,"i",0);w(this,"o",[])}h(e){if(e.hadRecentInput)return;let t=this.o[0],r=this.o.at(-1);this.i&&t&&r&&e.startTime-r.startTime<1e3&&e.startTime-t.startTime<5e3?(this.i+=e.value,this.o.push(e)):(this.i=e.value,this.o=[e]),this.t?.(e)}},M=(n,e,t={})=>{try{if(PerformanceObserver.supportedEntryTypes.includes(n)){let r=new PerformanceObserver((i=>{Promise.resolve().then((()=>{e(i.getEntries())}))}));return r.observe({type:n,buffered:!0,...t}),r}}catch{}},re=n=>{let e=!1;return()=>{e||(n(),e=!0)}},A=-1,Me=new Set,Ee=()=>document.visibilityState!=="hidden"||document.prerendering?1/0:0,q=n=>{if(document.visibilityState==="hidden"){if(n.type==="visibilitychange")for(let e of Me)e();isFinite(A)||(A=n.type==="visibilitychange"?n.timeStamp:0,removeEventListener("prerenderingchange",q,!0))}},O=()=>{if(A<0){let n=L();A=(document.prerendering?void 0:globalThis.performance.getEntriesByType("visibility-state").filter((t=>t.name==="hidden"&&t.startTime>n))[0]?.startTime)??Ee(),addEventListener("visibilitychange",q,!0),addEventListener("prerenderingchange",q,!0),F((()=>{setTimeout((()=>{A=Ee()}))}))}return{get firstHiddenTime(){return A},onHidden(n){Me.add(n)}}},U=n=>{document.prerendering?addEventListener("prerenderingchange",(()=>n()),!0):n()},Te=[1800,3e3],ne=(n,e={})=>{U((()=>{let t=O(),r,i=k("FCP"),s=M("paint",(o=>{for(let c of o)c.name==="first-contentful-paint"&&(s.disconnect(),c.startTime<t.firstHiddenTime&&(i.value=Math.max(c.startTime-L(),0),i.entries.push(c),r(!0)))}));s&&(r=v(n,i,Te,e.reportAllChanges),F((o=>{i=k("FCP"),r=v(n,i,Te,e.reportAllChanges),Z((()=>{i.value=performance.now()-o.timeStamp,r(!0)}))})))}))},Ce=[.1,.25],Be=(n,e={})=>{let t=O();ne(re((()=>{let r,i=k("CLS",0),s=te(e,K),o=d=>{for(let u of d)s.h(u);s.i>i.value&&(i.value=s.i,i.entries=s.o,r())},c=M("layout-shift",o);c&&(r=v(n,i,Ce,e.reportAllChanges),t.onHidden((()=>{o(c.takeRecords()),r(!0)})),F((()=>{s.i=0,i=k("CLS",0),r=v(n,i,Ce,e.reportAllChanges),Z((()=>r()))})),setTimeout(r))})))},xe=0,$=1/0,N=0,ot=n=>{for(let e of n)e.interactionId&&($=Math.min($,e.interactionId),N=Math.max(N,e.interactionId),xe=N?(N-$)/7+1:0)},J,Ie=()=>J?xe:performance.interactionCount??0,lt=()=>{"interactionCount"in performance||J||(J=M("event",ot,{type:"event",buffered:!0,durationThreshold:0}))},Pe=0,X=class{constructor(){w(this,"u",[]);w(this,"l",new Map);w(this,"m");w(this,"p")}v(){Pe=Ie(),this.u.length=0,this.l.clear()}L(){let e=Math.min(this.u.length-1,Math.floor((Ie()-Pe)/50));return this.u[e]}h(e){if(this.m?.(e),!e.interactionId&&e.entryType!=="first-input")return;let t=this.u.at(-1),r=this.l.get(e.interactionId);if(r||this.u.length<10||e.duration>t.P){if(r?e.duration>r.P?(r.entries=[e],r.P=e.duration):e.duration===r.P&&e.startTime===r.entries[0].startTime&&r.entries.push(e):(r={id:e.interactionId,entries:[e],P:e.duration},this.l.set(r.id,r),this.u.push(r)),this.u.sort(((i,s)=>s.P-i.P)),this.u.length>10){let i=this.u.splice(10);for(let s of i)this.l.delete(s.id)}this.p?.(r)}}},Ne=n=>{let e=globalThis.requestIdleCallback||setTimeout;document.visibilityState==="hidden"?n():(n=re(n),addEventListener("visibilitychange",n,{once:!0,capture:!0}),e((()=>{n(),removeEventListener("visibilitychange",n,{capture:!0})})))},_e=[200,500],Oe=(n,e={})=>{if(!globalThis.PerformanceEventTiming||!("interactionId"in PerformanceEventTiming.prototype))return;let t=O();U((()=>{lt();let r,i=k("INP"),s=te(e,X),o=d=>{Ne((()=>{for(let g of d)s.h(g);let u=s.L();u&&u.P!==i.value&&(i.value=u.P,i.entries=u.entries,r())}))},c=M("event",o,{durationThreshold:e.durationThreshold??40});r=v(n,i,_e,e.reportAllChanges),c&&(c.observe({type:"first-input",buffered:!0}),t.onHidden((()=>{o(c.takeRecords()),r(!0)})),F((()=>{s.v(),i=k("INP"),r=v(n,i,_e,e.reportAllChanges)})))}))},Q=class{constructor(){w(this,"m")}h(e){this.m?.(e)}},Ae=[2500,4e3],Ue=(n,e={})=>{U((()=>{let t=O(),r,i=k("LCP"),s=te(e,Q),o=d=>{e.reportAllChanges||(d=d.slice(-1));for(let u of d)s.h(u),u.startTime<t.firstHiddenTime&&(i.value=Math.max(u.startTime-L(),0),i.entries=[u],r())},c=M("largest-contentful-paint",o);if(c){r=v(n,i,Ae,e.reportAllChanges);let d=re((()=>{o(c.takeRecords()),c.disconnect(),r(!0)})),u=g=>{g.isTrusted&&(Ne(d),removeEventListener(g.type,u,{capture:!0}))};for(let g of["keydown","click","visibilitychange"])addEventListener(g,u,{capture:!0});F((g=>{i=k("LCP"),r=v(n,i,Ae,e.reportAllChanges),Z((()=>{i.value=performance.now()-g.timeStamp,r(!0)}))}))}}))},Fe=[800,1800],Y=n=>{document.prerendering?U((()=>Y(n))):document.readyState!=="complete"?addEventListener("load",(()=>Y(n)),!0):setTimeout(n)},De=(n,e={})=>{let t=k("TTFB"),r=v(n,t,Fe,e.reportAllChanges);Y((()=>{let i=ee();i&&(t.value=Math.max(i.responseStart-L(),0),t.entries=[i],r(!0),F((()=>{t=k("TTFB",0),r=v(n,t,Fe,e.reportAllChanges),r(!0)})))}))};var ct=2e4,D=class{constructor(e){this.data={lcp:null,cls:null,inp:null,fcp:null,ttfb:null};this.sent=!1;this.timeout=null;this.onReadyCallback=null;this.onReadyCallback=e}initialize(){try{Ue(this.collectMetric.bind(this),{reportAllChanges:!0}),Be(this.collectMetric.bind(this),{reportAllChanges:!0}),Oe(this.collectMetric.bind(this),{reportAllChanges:!0}),ne(this.collectMetric.bind(this)),De(this.collectMetric.bind(this)),this.timeout=setTimeout(()=>this.sendData(),ct),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&this.sendData()}),window.addEventListener("pagehide",()=>this.sendData())}catch(e){console.warn("Error initializing web vitals tracking:",e)}}collectMetric(e){if(this.sent)return;let t=e.name.toLowerCase();this.data[t]=e.value}sendData(){this.sent||(this.sent=!0,this.timeout&&(clearTimeout(this.timeout),this.timeout=null),this.onReadyCallback&&this.onReadyCallback(this.data))}getData(){return{...this.data}}};var ut=1e3,H=class{constructor(e,t){this.lastClickAt=new WeakMap;this.tracker=e,this.config=t}initialize(){document.addEventListener("click",this.handleClick.bind(this),!0)}handleClick(e){let t=e.target;this.config.trackButtonClicks&&this.isButton(t)&&this.trackButtonClick(t)}isButton(e){if(e.tagName==="BUTTON"||e.getAttribute("role")==="button")return!0;if(e.tagName==="INPUT"){let i=e.type?.toLowerCase();if(i==="submit"||i==="button")return!0}let t=e.parentElement,r=0;for(;t&&r<3;){if(t.tagName==="BUTTON"||t.getAttribute("role")==="button")return!0;t=t.parentElement,r++}return!1}trackButtonClick(e){let t=this.findButton(e);if(!t||t.hasAttribute("data-rybbit-event"))return;let r=Date.now(),i=this.lastClickAt.get(t);if(i!==void 0&&r-i<ut)return;this.lastClickAt.set(t,r);let s={text:this.getElementText(t),...this.extractDataAttributes(t)};this.tracker.trackButtonClick(s)}extractDataAttributes(e){let t={};for(let r of e.attributes)if(r.name.startsWith("data-rybbit-prop-")){let i=r.name.replace("data-rybbit-prop-","");t[i]=r.value}return t}findButton(e){if(e.tagName==="BUTTON"||e.getAttribute("role")==="button")return e;if(e.tagName==="INPUT"){let i=e.type?.toLowerCase();if(i==="submit"||i==="button")return e}let t=e.parentElement,r=0;for(;t&&r<3;){if(t.tagName==="BUTTON"||t.getAttribute("role")==="button")return t;t=t.parentElement,r++}return null}getElementText(e){let t=e.textContent?.trim().substring(0,100);if(t)return t;let r=e.getAttribute("aria-label")?.trim().substring(0,100);if(r)return r;if(e.tagName==="INPUT"){let s=e.value?.trim().substring(0,100);if(s)return s}let i=e.getAttribute("title")?.trim().substring(0,100);if(i)return i}cleanup(){document.removeEventListener("click",this.handleClick.bind(this),!0)}};var W=class{constructor(e){this.tracker=e}initialize(){document.addEventListener("copy",this.handleCopy.bind(this))}handleCopy(){let e=window.getSelection();if(!e||e.isCollapsed)return;let t=e.toString(),r=t.length;if(r===0)return;let i=e.anchorNode,s=i instanceof HTMLElement?i:i?.parentElement;if(!s)return;let o={text:t.substring(0,500),...r>500&&{textLength:r},sourceElement:s.tagName.toLowerCase()};this.tracker.trackCopy(o)}cleanup(){document.removeEventListener("copy",this.handleCopy.bind(this))}};var V=class{constructor(e,t){this.tracker=e,this.config=t,this.boundHandleSubmit=this.handleSubmit.bind(this),this.boundHandleChange=this.handleChange.bind(this)}initialize(){document.addEventListener("submit",this.boundHandleSubmit,!0),document.addEventListener("change",this.boundHandleChange,!0)}cleanup(){document.removeEventListener("submit",this.boundHandleSubmit,!0),document.removeEventListener("change",this.boundHandleChange,!0)}handleSubmit(e){let t=e.target;if(t.tagName!=="FORM")return;let r={formId:t.id||"",formName:t.name||"",formAction:t.action||"",method:(t.method||"get").toUpperCase(),fieldCount:t.elements.length,ariaLabel:t.getAttribute("aria-label")||void 0,...this.extractDataAttributes(t)};this.tracker.trackFormSubmit(r)}handleChange(e){let t=e.target;if(!(t instanceof Element))return;let r=t.tagName.toUpperCase();if(!["INPUT","SELECT","TEXTAREA"].includes(r)||t.disabled||t.readOnly)return;if(r==="INPUT"){let o=t.type?.toLowerCase();if(o==="hidden"||o==="password")return}let i=t.name||t.id||t.getAttribute("aria-label")||t.placeholder||"",s={element:r.toLowerCase(),inputType:r==="INPUT"?t.type?.toLowerCase():void 0,inputName:i,formId:t.form?.id||void 0,formName:t.form?.name||void 0,...this.extractDataAttributes(t)};this.tracker.trackInputChange(s)}extractDataAttributes(e){let t={};for(let r of e.attributes)if(r.name.startsWith("data-rybbit-prop-")){let i=r.name.replace("data-rybbit-prop-","");t[i]=r.value}return t}};(async function(){let n=document.currentScript||document.querySelector('script[src*="/script.js"]');if(!n){console.error("Could not find current script tag");return}let e=n.getAttribute("data-namespace")||"rybbit",t=`disable-${e}`;if(window.__RYBBIT_OPTOUT__||localStorage.getItem(t)!==null){window[e]={pageview:()=>{},event:()=>{},error:()=>{},trackOutbound:()=>{},identify:()=>{},setTraits:()=>{},clearUserId:()=>{},getUserId:()=>null,flag:(a,l)=>l,flagPayload:(a,l)=>l,flags:()=>({}),flagPayloads:()=>({}),onReady:()=>{},startSessionReplay:()=>{},stopSessionReplay:()=>{},isSessionReplayActive:()=>!1};return}let r=[],i=a=>(...l)=>{r.push([a,l])};window[e]={pageview:i("pageview"),event:i("event"),error:i("error"),trackOutbound:i("trackOutbound"),identify:i("identify"),setTraits:i("setTraits"),clearUserId:i("clearUserId"),getUserId:()=>null,flag:(a,l)=>l,flagPayload:(a,l)=>l,flags:()=>({}),flagPayloads:()=>({}),onReady:i("onReady"),startSessionReplay:i("startSessionReplay"),stopSessionReplay:i("stopSessionReplay"),isSessionReplayActive:()=>!1};let s=await de(n);if(!s)return;let o=new x(s);s.enableWebVitals&&new D(l=>{o.trackWebVitals(l)}).initialize();let c=null,d=null,u=null;s.trackButtonClicks&&(c=new H(o,s),c.initialize()),s.trackCopy&&(d=new W(o),d.initialize()),s.trackFormInteractions&&(u=new V(o,s),u.initialize()),s.trackErrors&&(window.addEventListener("error",a=>{o.trackError(a.error||new Error(a.message),{filename:a.filename,lineno:a.lineno,colno:a.colno})}),window.addEventListener("unhandledrejection",a=>{let l=a.reason instanceof Error?a.reason:new Error(String(a.reason));o.trackError(l,{type:"unhandledrejection"})}));let g=()=>o.trackPageviewIfUrlChanged(),y=s.debounceDuration>0?ce(g,s.debounceDuration):g;function T(){if(document.addEventListener("click",function(a){let l=a.target;for(;l&&l!==document.documentElement;){if(l.hasAttribute("data-rybbit-event")){let p=l.getAttribute("data-rybbit-event");if(p){let S={};for(let R of l.attributes)if(R.name.startsWith("data-rybbit-prop-")){let C=R.name.replace("data-rybbit-prop-","");S[C]=R.value}o.trackEvent(p,S)}break}l=l.parentElement}if(s.trackOutbound){let p=a.target.closest("a");p?.href&&ue(p.href)&&o.trackOutbound(p.href,p.innerText||p.textContent||"",p.target||"_self")}}),s.autoTrackSpa){let a=history.pushState,l=history.replaceState;history.pushState=function(...p){a.apply(this,p),y(),o.onPageChange()},history.replaceState=function(...p){l.apply(this,p),y(),o.onPageChange()},window.addEventListener("popstate",()=>{y(),o.onPageChange()}),window.addEventListener("hashchange",()=>{y(),o.onPageChange()})}}window[s.namespace]={pageview:()=>o.trackPageview(),event:(a,l={})=>o.trackEvent(a,l),error:(a,l={})=>o.trackError(a,l),trackOutbound:(a,l="",p="_self")=>o.trackOutbound(a,l,p),identify:(a,l)=>o.identify(a,l),setTraits:a=>o.setTraits(a),clearUserId:()=>o.clearUserId(),getUserId:()=>o.getUserId(),flag:(a,l)=>o.getFeatureFlag(a,l),flagPayload:(a,l)=>o.getFeatureFlagPayload(a,l),flags:()=>o.getFeatureFlags(),flagPayloads:()=>o.getFeatureFlagPayloads(),onReady:a=>a(window[s.namespace]),startSessionReplay:()=>o.startSessionReplay(),stopSessionReplay:()=>o.stopSessionReplay(),isSessionReplayActive:()=>o.isSessionReplayActive()};let h=window[s.namespace];for(let[a,l]of r)h[a](...l);T(),window.addEventListener("beforeunload",()=>{c?.cleanup(),d?.cleanup(),o.cleanup()}),s.autoTrackPageview&&o.trackPageview()})();})();
+"use strict";(()=>{var qe=Object.defineProperty;var Je=(r,e,t)=>e in r?qe(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var k=(r,e,t)=>Je(r,typeof e!="symbol"?e+"":e,t);function Xe(r){if(r.startsWith("re:")){let c=r.slice(3);if(!c)throw new Error("Empty regex pattern");return new RegExp(c)}let t="__DOUBLE_ASTERISK_TOKEN__",n="__SINGLE_ASTERISK_TOKEN__",s=r.replace(/\*\*/g,t).replace(/\*/g,n).replace(/[.+?^${}()|[\]\\]/g,"\\$&");s=s.replace(new RegExp(`/${t}/`,"g"),"/(?:.+/)?"),s=s.replace(new RegExp(t,"g"),".*"),s=s.replace(/\//g,"\\/");let a=s.replace(new RegExp(n,"g"),"[^/]+");return new RegExp("^"+a+"$")}function j(r,e){for(let t of e)try{if(Xe(t).test(r))return t}catch(n){console.error(`Invalid pattern: ${t}`,n)}return null}function pe(r,e){let t=null;return(...n)=>{t&&clearTimeout(t),t=setTimeout(()=>r(...n),e)}}function ge(r){try{let e=window.location.hostname,t=new URL(r).hostname;return t!==e&&t!==""}catch{return!1}}function L(r,e){if(r.length<=e)return r;let t=r.charCodeAt(e-1);return r.slice(0,t>=55296&&t<=56319?e-1:e)}function I(r,e){if(!r)return e;try{let t=JSON.parse(r);return Array.isArray(e)&&!Array.isArray(t)?e:t}catch(t){return console.error("Error parsing JSON:",t),e}}function _(r,e=!1){return r.hash.startsWith("#/")?r.hash.substring(1):r.hash.startsWith("#!/")?r.hash.substring(2):r.pathname+(e?r.hash:"")}var Qe=2e3;function $(){try{if(crypto?.randomUUID)return crypto.randomUUID()}catch{}return`${Date.now().toString(36)}-${Math.random().toString(36).slice(2,12)}`}function Ye(r){let e=`${r}-visitor-id`;try{let t=localStorage.getItem(e);if(t)return t;let n=$();return localStorage.setItem(e,n),n}catch{return $()}}function Ze(r){try{return localStorage.getItem(`${r}-user-id`)||void 0}catch{return}}async function et(r,e,t,n,i){let s=new AbortController,a=window.setTimeout(()=>s.abort(),Qe);try{let c=new URL(window.location.href),d=await fetch(`${r}/site/${e}/feature-flags/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},credentials:"omit",signal:s.signal,body:JSON.stringify({anonymousId:n,identifiedUserId:Ze(t),hostname:c.hostname,pathname:_(c,i),querystring:c.search,query:Object.fromEntries(c.searchParams.entries()),referrer:document.referrer,language:navigator.language,screenWidth:screen.width,screenHeight:screen.height})});if(!d.ok)return{enabled:!0,flags:{}};let u=await d.json();return{enabled:u?.featureFlagsEnabled!==!1,flags:u?.flags&&typeof u.flags=="object"?u.flags:{}}}catch{return{enabled:!0,flags:{}}}finally{window.clearTimeout(a)}}function tt(r){try{let e=new URL(r,window.location.href);return e.searchParams.get("siteId")||e.searchParams.get("site-id")||e.searchParams.get("site_id")}catch{return null}}async function he(r){let e=r.getAttribute("src");if(!e)return console.error("Script src attribute is missing"),null;let t=e.split("/script.js")[0];if(!t)return console.error("Please provide a valid analytics host"),null;let n=tt(e)||r.getAttribute("data-site-id")||r.getAttribute("site-id");if(!n)return console.error("Please provide a valid site ID using the ?siteId= query parameter or the data-site-id attribute"),null;let i=r.getAttribute("data-namespace")||"rybbit",s=$(),a=I(r.getAttribute("data-skip-patterns"),[]),c=I(r.getAttribute("data-mask-patterns"),[]),d=I(r.getAttribute("data-replay-mask-text-selectors"),[]),u=r.getAttribute("data-debounce")?Math.max(0,parseInt(r.getAttribute("data-debounce"))):500,g=r.getAttribute("data-replay-batch-size")?Math.max(1,parseInt(r.getAttribute("data-replay-batch-size"))):250,y=r.getAttribute("data-replay-batch-interval")?Math.max(1e3,parseInt(r.getAttribute("data-replay-batch-interval"))):5e3,R=r.getAttribute("data-replay-block-class")||void 0,h=r.getAttribute("data-replay-block-selector")||void 0,o=r.getAttribute("data-replay-ignore-class")||void 0,l=r.getAttribute("data-replay-ignore-selector")||void 0,p=r.getAttribute("data-replay-mask-text-class")||void 0,w=r.getAttribute("data-replay-mask-all-inputs"),E=w!==null?w!=="false":void 0,C=r.getAttribute("data-replay-mask-input-options"),Ve=C?I(C,{password:!0,email:!0}):void 0,oe=r.getAttribute("data-replay-collect-fonts"),Ge=oe!==null?oe!=="false":void 0,le=r.getAttribute("data-replay-sampling"),ze=le?I(le,{}):void 0,ce=r.getAttribute("data-replay-slim-dom-options"),je=ce?I(ce,{}):void 0,ue=r.getAttribute("data-replay-sample-rate"),$e=ue?Math.min(100,Math.max(0,parseInt(ue,10))):void 0,Ke=r.getAttribute("data-tag")||"",f={namespace:i,analyticsHost:t,siteId:n,visitorId:s,debounceDuration:u,sessionReplayBatchSize:g,sessionReplayBatchInterval:y,sessionReplayMaskTextSelectors:d,skipPatterns:a,maskPatterns:c,autoTrackPageview:!0,autoTrackSpa:!0,trackQuerystring:!0,trackUrlFragments:!1,trackOutbound:!0,enableWebVitals:!1,trackErrors:!1,enableSessionReplay:!1,trackButtonClicks:!1,trackCopy:!1,trackFormInteractions:!1,tag:Ke,featureFlagsEnabled:!1,featureFlags:{},sessionReplayBlockClass:R,sessionReplayBlockSelector:h,sessionReplayIgnoreClass:o,sessionReplayIgnoreSelector:l,sessionReplayMaskTextClass:p,sessionReplayMaskAllInputs:E,sessionReplayMaskInputOptions:Ve,sessionReplayCollectFonts:Ge,sessionReplaySampling:ze,sessionReplaySlimDOMOptions:je,sessionReplaySampleRate:$e},T=f;try{let P=`${t}/site/tracking-config/${n}`,de=await fetch(P,{method:"GET",credentials:"omit"});if(de.ok){let m=await de.json();T={...f,autoTrackPageview:m.trackInitialPageView??f.autoTrackPageview,autoTrackSpa:m.trackSpaNavigation??f.autoTrackSpa,trackQuerystring:m.trackUrlParams??f.trackQuerystring,trackUrlFragments:m.trackUrlFragments??f.trackUrlFragments,trackOutbound:m.trackOutbound??f.trackOutbound,enableWebVitals:m.webVitals??f.enableWebVitals,trackErrors:m.trackErrors??f.trackErrors,enableSessionReplay:m.sessionReplay??f.enableSessionReplay,trackButtonClicks:m.trackButtonClicks??f.trackButtonClicks,trackCopy:m.trackCopy??f.trackCopy,trackFormInteractions:m.trackFormInteractions??f.trackFormInteractions,featureFlagsEnabled:m.featureFlagsEnabled===!0}}else console.warn("Failed to fetch tracking config from API, using defaults")}catch(P){console.warn("Error fetching tracking config:",P)}if(T.featureFlagsEnabled){T.visitorId=Ye(i);let P=await et(t,n,i,T.visitorId,T.trackUrlFragments);T.featureFlagsEnabled=P.enabled,T.featureFlags=P.flags}return T}var b={automationApi:1,zeroOuterDimensions:2,missingChrome:4,swiftShader:8,emptyPlugins:16,defaultViewport800x600:32,defaultViewport1024x768:64,impossibleDimensions:128,outerDimensionsWeird:256,pluginApiAbsence:512,defaultViewport1280x1200:1024,squareScreen:2048,missingScreenDimensions:4096},rt=Object.keys(b),ye={automationApi:3,zeroOuterDimensions:2,missingChrome:1,swiftShader:1,emptyPlugins:1,defaultViewport800x600:3,defaultViewport1024x768:3,impossibleDimensions:3,outerDimensionsWeird:2,pluginApiAbsence:0,defaultViewport1280x1200:3,squareScreen:3,missingScreenDimensions:1},It=rt.reduce((r,e)=>r|b[e],0),Pt=b.automationApi|b.impossibleDimensions|b.defaultViewport800x600|b.defaultViewport1024x768|b.defaultViewport1280x1200|b.squareScreen,be=10,fe=200,me=8192,nt=[{width:800,height:600,signal:"defaultViewport800x600"},{width:1024,height:768,signal:"defaultViewport1024x768"},{width:1280,height:1200,signal:"defaultViewport1280x1200"}];function it(r,e){return Number.isFinite(r)&&Number.isFinite(e)&&r>=fe&&e>=fe&&r<=me&&e<=me}function st(r){return/Windows NT|Macintosh|X11|Linux x86_64/.test(r)&&!/Mobile|Android|iPhone|iPad/.test(r)}function ve(r,e,t){if(!it(r,e))return["impossibleDimensions"];let n=[];if(r===e&&n.push("squareScreen"),st(t))for(let i of nt)r===i.width&&e===i.height&&n.push(i.signal);return n}var Se=null;function we(){return Te().score}function x(){return Te().mask}function Ee(){return document.prerendering===!0}function Te(){return Ee()?ke():(Se??(Se=ke()),Se)}function ke(){let r=0,e=0;function t(n){let i=b[n];(e&i)===0&&(e|=i,r+=ye[n])}try{let n=navigator.userAgent,i=/Chrome\//.test(n)&&!/\bwv\b|; wv\)/.test(n),s=Number(window.screen?.width),a=Number(window.screen?.height),c=Number(window.outerWidth),d=Number(window.outerHeight),u=Number(window.innerWidth),g=Number(window.innerHeight),R=["__webdriver_evaluate","__selenium_evaluate","__webdriver_script_function","__webdriver_script_func","__webdriver_script_fn","__fxdriver_evaluate","__driver_unwrapped","__webdriver_unwrapped","__driver_evaluate","__selenium_unwrapped","__fxdriver_unwrapped","_phantom","callPhantom","__nightmare","domAutomation","domAutomationController"].some(o=>o in window||o in document);(navigator.webdriver===!0||R)&&t("automationApi"),(d===0||c===0)&&!Ee()&&t("zeroOuterDimensions");for(let o of ve(s,a,n))t(o);Number.isFinite(c)&&Number.isFinite(d)&&Number.isFinite(u)&&Number.isFinite(g)&&c>0&&d>0&&u>0&&g>0&&(c+8<u||d+8<g)&&t("outerDimensionsWeird");let h=!1;!window.chrome&&i&&(t("missingChrome"),h=!0);try{let o=document.createElement("canvas"),l=o.getContext("webgl")||o.getContext("experimental-webgl");if(l)try{let p=[],w=l.getParameter(l.RENDERER);typeof w=="string"&&p.push(w);try{let E=l.getExtension("WEBGL_debug_renderer_info");if(E){let C=l.getParameter(E.UNMASKED_RENDERER_WEBGL);typeof C=="string"&&p.push(C)}}catch{}p.join(" ").toLowerCase().includes("swiftshader")&&t("swiftShader")}finally{at(o,l)}}catch{}(!navigator.plugins||navigator.plugins.length===0)&&i&&(t("emptyPlugins"),h=!0),h&&t("pluginApiAbsence")}catch{}return{score:Math.min(r,be),mask:e}}function at(r,e){try{e.getExtension("WEBGL_lose_context")?.loseContext?.()}catch{}r.width=0,r.height=0}var Re="rybbit-replay-sampled",ot=1e4;function lt(r){if(r>=100)return!0;if(r<=0)return!1;try{let e=sessionStorage.getItem(Re);if(e!==null)return e==="1";let t=Math.random()*100<r;return sessionStorage.setItem(Re,t?"1":"0"),t}catch{return Math.random()*100<r}}var N=class{constructor(e,t,n){this.isRecording=!1;this.capped=!1;this.eventBuffer=[];this.failedBatches=[];this.lastFullSnapshotRequestAt=-1/0;this.config=e,this.userId=t,this.sendBatch=n,document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&this.flushEvents()}),window.addEventListener("pagehide",()=>void this.flushEvents())}async initialize(){if(!this.config.enableSessionReplay)return;let e=this.config.sessionReplaySampleRate;e!==void 0&&!lt(e)||(window.rrweb||await this.loadRrweb(),window.rrweb&&this.startRecording())}async loadRrweb(){return new Promise((e,t)=>{let n=document.createElement("script");n.src=`${this.config.analyticsHost}/replay.js`,n.async=!1,n.onload=()=>{e()},n.onerror=()=>t(new Error("Failed to load rrweb")),document.head.appendChild(n)})}startRecording(){if(!(this.capped||this.isRecording||!window.rrweb||!this.config.enableSessionReplay))try{let e={mousemove:!1,mouseInteraction:{MouseUp:!1,MouseDown:!1,Click:!0,ContextMenu:!1,DblClick:!0,Focus:!0,Blur:!0,TouchStart:!1,TouchEnd:!1},scroll:500,input:"last",media:800},t={script:!1,comment:!0,headFavicon:!0,headWhitespace:!0,headMetaDescKeywords:!0,headMetaSocial:!0,headMetaRobots:!0,headMetaHttpEquiv:!0,headMetaAuthorship:!0,headMetaVerification:!0},n={emit:i=>{this.addEvent({type:i.type,data:i.data,timestamp:i.timestamp||Date.now()})},recordCanvas:!1,checkoutEveryNms:6e4,checkoutEveryNth:500,blockClass:this.config.sessionReplayBlockClass??"rr-block",blockSelector:this.config.sessionReplayBlockSelector??null,ignoreClass:this.config.sessionReplayIgnoreClass??"rr-ignore",ignoreSelector:this.config.sessionReplayIgnoreSelector??null,maskTextClass:this.config.sessionReplayMaskTextClass??"rr-mask",maskAllInputs:this.config.sessionReplayMaskAllInputs??!0,maskInputOptions:this.config.sessionReplayMaskInputOptions??{password:!0,email:!0},collectFonts:this.config.sessionReplayCollectFonts??!0,sampling:this.config.sessionReplaySampling??e,slimDOMOptions:this.config.sessionReplaySlimDOMOptions??t};this.config.sessionReplayMaskTextSelectors&&this.config.sessionReplayMaskTextSelectors.length>0&&(n.maskTextSelector=this.config.sessionReplayMaskTextSelectors.join(", ")),this.stopRecordingFn=window.rrweb.record(n),this.isRecording=!0,this.setupBatchTimer()}catch{}}stopRecording(){this.isRecording&&(this.stopRecordingFn&&this.stopRecordingFn(),this.isRecording=!1,this.clearBatchTimer(),this.deferredSnapshotTimer!==void 0&&(clearTimeout(this.deferredSnapshotTimer),this.deferredSnapshotTimer=void 0),(this.eventBuffer.length>0||this.failedBatches.length>0)&&this.flushEvents())}isActive(){return this.isRecording}addEvent(e){this.eventBuffer.push(e),this.eventBuffer.length>=this.config.sessionReplayBatchSize&&this.flushEvents()}setupBatchTimer(){this.clearBatchTimer(),this.batchTimer=window.setInterval(()=>{(this.eventBuffer.length>0||this.failedBatches.length>0)&&this.flushEvents()},this.config.sessionReplayBatchInterval)}clearBatchTimer(){this.batchTimer&&(clearInterval(this.batchTimer),this.batchTimer=void 0)}async flushEvents(){let e=this.failedBatches;this.failedBatches=[],this.eventBuffer.length>0&&(e.push({userId:this.userId,events:this.eventBuffer,metadata:{pageUrl:window.location.href,viewportWidth:screen.width,viewportHeight:screen.height,language:navigator.language},_bsm:x()}),this.eventBuffer=[]),await Promise.all(e.map(t=>this.sendOrRequeue(t)))}async sendOrRequeue(e){try{let t=await this.sendBatch(e);t?.stopRecording?this.stopForSizeCap():t?.takeFullSnapshot?this.takeFullSnapshot():this.deferredSnapshotTimer!==void 0&&e.events.some(n=>n.type===2)&&(clearTimeout(this.deferredSnapshotTimer),this.deferredSnapshotTimer=void 0)}catch{this.capped||this.requeue(e)}}requeue(e){let t=this.failedBatches[this.failedBatches.length-1];if(t&&t.userId===e.userId&&JSON.stringify(t.metadata)===JSON.stringify(e.metadata)){t.events=t.events.concat(e.events);return}this.failedBatches.push(e)}takeFullSnapshot(){if(!this.isRecording)return;let e=this.lastFullSnapshotRequestAt+ot-Date.now();if(e>0){this.deferredSnapshotTimer===void 0&&(this.deferredSnapshotTimer=window.setTimeout(()=>{this.deferredSnapshotTimer=void 0,this.takeFullSnapshot()},e));return}this.lastFullSnapshotRequestAt=Date.now(),this.flushEvents();try{window.rrweb?.record.takeFullSnapshot?.(!0)}catch{}this.flushEvents()}stopForSizeCap(){this.capped=!0,this.stopRecordingFn&&(this.stopRecordingFn(),this.stopRecordingFn=void 0),this.isRecording=!1,this.clearBatchTimer(),this.eventBuffer=[],this.failedBatches=[]}updateUserId(e){e!==this.userId&&(this.eventBuffer.length>0&&this.flushEvents(),this.userId=e)}onPageChange(){this.isRecording&&this.flushEvents()}cleanup(){this.stopRecording()}};var ct=2e3,ut=2048,dt=512,pt=2048,gt=500,ht=48*1024,ft=[408,429,502,503,504],O=class{constructor(e){this.customUserId=null;this.errorDedupeCache=new Map;this.errorDedupeLastCleanup=0;this.exposedFeatureFlags=new Set;this.replayKeepaliveBytesInFlight=0;this.config=e,this.lastPageviewUrl=this.getPageviewUrl(),this.loadUserId(),e.enableSessionReplay&&this.initializeSessionReplay()}serializeFeatureFlagValue(e){if(e==null)return"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return String(e);try{return JSON.stringify(e)}catch{return""}}getFeatureFlagEventPayload(){let e={};for(let[t,n]of Object.entries(this.config.featureFlags||{}))e[t]=this.serializeFeatureFlagValue(n.value);return e}getCurrentUrlContext(){let e=new URL(window.location.href),t=_(e,this.config.trackUrlFragments);return{hostname:e.hostname,pathname:t,querystring:e.search,query:Object.fromEntries(e.searchParams.entries()),referrer:document.referrer,language:navigator.language,screenWidth:screen.width,screenHeight:screen.height}}async refreshFeatureFlags(){if(!this.config.featureFlagsEnabled)return;let e=new AbortController,t=window.setTimeout(()=>e.abort(),ct);try{let n=await fetch(`${this.config.analyticsHost}/site/${this.config.siteId}/feature-flags/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({anonymousId:this.config.visitorId,identifiedUserId:this.customUserId||void 0,...this.getCurrentUrlContext()}),mode:"cors",credentials:"omit",keepalive:!0,signal:e.signal});if(!n.ok)return;let i=await n.json();this.config.featureFlags=i?.flags&&typeof i.flags=="object"?i.flags:{},i?.featureFlagsEnabled===!1&&(this.config.featureFlagsEnabled=!1)}catch{}finally{window.clearTimeout(t)}}loadUserId(){try{let e=localStorage.getItem(`${this.config.namespace}-user-id`);e&&(this.customUserId=e)}catch{}}async initializeSessionReplay(){try{this.sessionReplayRecorder=new N(this.config,this.customUserId||"",e=>this.sendSessionReplayBatch(e)),await this.sessionReplayRecorder.initialize()}catch(e){console.error("Failed to initialize session replay:",e)}}async sendSessionReplayBatch(e){let t=JSON.stringify(e),n=ht-this.replayKeepaliveBytesInFlight,i=t.length<=n?new Blob([t]).size:1/0,s=i<=n;s&&(this.replayKeepaliveBytesInFlight+=i);let a;try{a=await fetch(`${this.config.analyticsHost}/session-replay/record/${this.config.siteId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:t,mode:"cors",keepalive:s})}catch(c){throw console.error("Failed to send session replay batch:",c),c}finally{s&&(this.replayKeepaliveBytesInFlight-=i)}if(!a.ok){if(ft.includes(a.status))throw new Error(`Session replay batch failed with status ${a.status}`);return}try{return await a.json()}catch{return}}createBasePayload(){let e=new URL(window.location.href),t=_(e,this.config.trackUrlFragments);if(j(t,this.config.skipPatterns))return null;let n=j(t,this.config.maskPatterns);n&&(t=n);let i={site_id:this.config.siteId,hostname:e.hostname,pathname:t,querystring:this.config.trackQuerystring?e.search:"",screenWidth:screen.width,screenHeight:screen.height,language:navigator.language,page_title:L(document.title,dt),referrer:L(document.referrer,ut),_bs:we(),_bsm:x()};this.customUserId&&(i.user_id=this.customUserId),this.config.tag&&(i.tag=this.config.tag);let s=this.getFeatureFlagEventPayload();return Object.keys(s).length>0&&(i.feature_flags=s),i}async sendTrackingData(e){try{await fetch(`${this.config.analyticsHost}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),mode:"cors",keepalive:!0})}catch(t){console.error("Failed to send tracking data:",t)}}track(e,t="",n={}){if(e==="custom_event"&&(!t||typeof t!="string")){console.error("Event name is required and must be a string for custom events");return}let i=this.createBasePayload();if(!i)return;let a={...i,type:e,event_name:t,properties:["custom_event","outbound","error","button_click","copy","form_submit","input_change"].includes(e)?JSON.stringify(n):void 0};this.sendTrackingData(a)}trackPageview(){this.lastPageviewUrl=this.getPageviewUrl(),this.track("pageview")}trackPageviewIfUrlChanged(){this.getPageviewUrl()!==this.lastPageviewUrl&&this.trackPageview()}getPageviewUrl(){let e=new URL(window.location.href);return JSON.stringify([e.hostname,_(e,this.config.trackUrlFragments),this.config.trackQuerystring?e.search:""])}trackEvent(e,t={}){this.track("custom_event",e,t)}getFeatureFlag(e,t){let n=this.config.featureFlags?.[e];if(!n)return t;let i=`${e}:${n.version}:${this.serializeFeatureFlagValue(n.value)}`;return this.exposedFeatureFlags.has(i)||(this.exposedFeatureFlags.add(i),this.trackEvent("feature_flag_exposure",{key:e,value:this.serializeFeatureFlagValue(n.value),version:n.version,reason:n.reason})),n.value}getFeatureFlags(){return Object.fromEntries(Object.entries(this.config.featureFlags||{}).map(([e,t])=>[e,t.value]))}getFeatureFlagPayload(e,t){let n=this.config.featureFlags?.[e];return!n||n.payload===void 0?t:n.payload}getFeatureFlagPayloads(){return Object.fromEntries(Object.entries(this.config.featureFlags||{}).filter(([,e])=>e.payload!==void 0).map(([e,t])=>[e,t.payload]))}trackOutbound(e,t="",n="_self"){let i={url:e,text:L(t,gt),target:n},s=JSON.stringify(i).length-pt;s>0&&(i.url=L(e,Math.max(0,e.length-s))),this.track("outbound","",i)}trackWebVitals(e){let t=this.createBasePayload();if(!t)return;let n={...t,type:"performance",event_name:"web-vitals",...e};this.sendTrackingData(n)}trackError(e,t={}){let n=e?.message||"";if(n.includes("ResizeObserver loop completed with undelivered notifications")||n.includes("ResizeObserver loop limit exceeded"))return;let i=window.location.origin,s=t.filename||"",a=e.stack||"";if(s)try{if(new URL(s).origin!==i)return}catch{}else if(a&&!a.includes(i))return;let d=[e.name||"Error",n,t.filename||"",t.lineno??"",t.colno??""].join("|"),u=Date.now(),g=6e4,y=this.errorDedupeCache.get(d);if(y&&u-y<g)return;this.errorDedupeCache.set(d,u);let R=10*6e4;if(u-this.errorDedupeLastCleanup>g){for(let[o,l]of this.errorDedupeCache.entries())u-l>R&&this.errorDedupeCache.delete(o);this.errorDedupeLastCleanup=u}let h={message:e.message?.substring(0,500)||"Unknown error",stack:a.substring(0,2e3)||""};if(s&&(h.fileName=s),t.lineno){let o=typeof t.lineno=="string"?parseInt(t.lineno,10):t.lineno;o&&o!==0&&(h.lineNumber=o)}if(t.colno){let o=typeof t.colno=="string"?parseInt(t.colno,10):t.colno;o&&o!==0&&(h.columnNumber=o)}for(let o in t)!["lineno","colno"].includes(o)&&t[o]!==void 0&&(h[o]=t[o]);this.track("error",e.name||"Error",h)}trackButtonClick(e){this.track("button_click","",e)}trackCopy(e){this.track("copy","",e)}trackFormSubmit(e){this.track("form_submit","",e)}trackInputChange(e){this.track("input_change","",e)}identify(e,t){if(typeof e!="string"||e.trim()===""){console.error("User ID must be a non-empty string");return}this.customUserId=e.trim();try{localStorage.setItem(`${this.config.namespace}-user-id`,this.customUserId)}catch{console.warn("Could not persist user ID to localStorage")}this.sendIdentifyEvent(this.customUserId,t,!0).then(()=>this.refreshFeatureFlags()),this.sessionReplayRecorder&&this.sessionReplayRecorder.updateUserId(this.customUserId)}setTraits(e){if(!e||typeof e!="object"){console.error("Traits must be an object");return}let t=this.customUserId;if(!t){console.warn("Cannot set traits without identifying user first. Call identify() first.");return}this.sendIdentifyEvent(t,e,!1).then(()=>this.refreshFeatureFlags())}async sendIdentifyEvent(e,t,n=!0){try{await fetch(`${this.config.analyticsHost}/identify`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({site_id:this.config.siteId,user_id:e,traits:t,is_new_identify:n}),mode:"cors",keepalive:!0})}catch(i){console.error("Failed to send identify event:",i)}}clearUserId(){this.customUserId=null;try{localStorage.removeItem(`${this.config.namespace}-user-id`)}catch{}this.sessionReplayRecorder&&this.sessionReplayRecorder.updateUserId(""),this.refreshFeatureFlags()}getUserId(){return this.customUserId}startSessionReplay(){this.sessionReplayRecorder?this.sessionReplayRecorder.startRecording():console.warn("Session replay not initialized")}stopSessionReplay(){this.sessionReplayRecorder&&this.sessionReplayRecorder.stopRecording()}isSessionReplayActive(){return this.sessionReplayRecorder?.isActive()??!1}onPageChange(){this.refreshFeatureFlags(),this.sessionReplayRecorder&&this.sessionReplayRecorder.onPageChange()}cleanup(){this.sessionReplayRecorder&&this.sessionReplayRecorder.cleanup()}};var Me=-1,F=r=>{addEventListener("pageshow",(e=>{e.persisted&&(Me=e.timeStamp,r(e))}),!0)},v=(r,e,t,n)=>{let i,s;return a=>{e.value>=0&&(a||n)&&(s=e.value-(i??0),(s||i===void 0)&&(i=e.value,e.delta=s,e.rating=((c,d)=>c>d[1]?"poor":c>d[0]?"needs-improvement":"good")(e.value,t),r(e)))}},te=r=>{requestAnimationFrame((()=>requestAnimationFrame((()=>r()))))},re=()=>{let r=performance.getEntriesByType("navigation")[0];if(r&&r.responseStart>0&&r.responseStart<performance.now())return r},B=()=>re()?.activationStart??0,S=(r,e=-1)=>{let t=re(),n="navigate";return Me>=0?n="back-forward-cache":t&&(document.prerendering||B()>0?n="prerender":document.wasDiscarded?n="restore":t.type&&(n=t.type.replace(/_/g,"-"))),{name:r,value:e,rating:"good",delta:0,entries:[],id:`v5-${Date.now()}-${Math.floor(8999999999999*Math.random())+1e12}`,navigationType:n}},K=new WeakMap;function ne(r,e){return K.get(r)||K.set(r,new e),K.get(r)}var J=class{constructor(){k(this,"t");k(this,"i",0);k(this,"o",[])}h(e){if(e.hadRecentInput)return;let t=this.o[0],n=this.o.at(-1);this.i&&t&&n&&e.startTime-n.startTime<1e3&&e.startTime-t.startTime<5e3?(this.i+=e.value,this.o.push(e)):(this.i=e.value,this.o=[e]),this.t?.(e)}},M=(r,e,t={})=>{try{if(PerformanceObserver.supportedEntryTypes.includes(r)){let n=new PerformanceObserver((i=>{Promise.resolve().then((()=>{e(i.getEntries())}))}));return n.observe({type:r,buffered:!0,...t}),n}}catch{}},ie=r=>{let e=!1;return()=>{e||(r(),e=!0)}},A=-1,xe=new Set,Ce=()=>document.visibilityState!=="hidden"||document.prerendering?1/0:0,X=r=>{if(document.visibilityState==="hidden"){if(r.type==="visibilitychange")for(let e of xe)e();isFinite(A)||(A=r.type==="visibilitychange"?r.timeStamp:0,removeEventListener("prerenderingchange",X,!0))}},D=()=>{if(A<0){let r=B();A=(document.prerendering?void 0:globalThis.performance.getEntriesByType("visibility-state").filter((t=>t.name==="hidden"&&t.startTime>r))[0]?.startTime)??Ce(),addEventListener("visibilitychange",X,!0),addEventListener("prerenderingchange",X,!0),F((()=>{setTimeout((()=>{A=Ce()}))}))}return{get firstHiddenTime(){return A},onHidden(r){xe.add(r)}}},H=r=>{document.prerendering?addEventListener("prerenderingchange",(()=>r()),!0):r()},Ie=[1800,3e3],se=(r,e={})=>{H((()=>{let t=D(),n,i=S("FCP"),s=M("paint",(a=>{for(let c of a)c.name==="first-contentful-paint"&&(s.disconnect(),c.startTime<t.firstHiddenTime&&(i.value=Math.max(c.startTime-B(),0),i.entries.push(c),n(!0)))}));s&&(n=v(r,i,Ie,e.reportAllChanges),F((a=>{i=S("FCP"),n=v(r,i,Ie,e.reportAllChanges),te((()=>{i.value=performance.now()-a.timeStamp,n(!0)}))})))}))},Pe=[.1,.25],Ne=(r,e={})=>{let t=D();se(ie((()=>{let n,i=S("CLS",0),s=ne(e,J),a=d=>{for(let u of d)s.h(u);s.i>i.value&&(i.value=s.i,i.entries=s.o,n())},c=M("layout-shift",a);c&&(n=v(r,i,Pe,e.reportAllChanges),t.onHidden((()=>{a(c.takeRecords()),n(!0)})),F((()=>{s.i=0,i=S("CLS",0),n=v(r,i,Pe,e.reportAllChanges),te((()=>n()))})),setTimeout(n))})))},Oe=0,q=1/0,U=0,mt=r=>{for(let e of r)e.interactionId&&(q=Math.min(q,e.interactionId),U=Math.max(U,e.interactionId),Oe=U?(U-q)/7+1:0)},Q,_e=()=>Q?Oe:performance.interactionCount??0,yt=()=>{"interactionCount"in performance||Q||(Q=M("event",mt,{type:"event",buffered:!0,durationThreshold:0}))},Ae=0,Y=class{constructor(){k(this,"u",[]);k(this,"l",new Map);k(this,"m");k(this,"p")}v(){Ae=_e(),this.u.length=0,this.l.clear()}L(){let e=Math.min(this.u.length-1,Math.floor((_e()-Ae)/50));return this.u[e]}h(e){if(this.m?.(e),!e.interactionId&&e.entryType!=="first-input")return;let t=this.u.at(-1),n=this.l.get(e.interactionId);if(n||this.u.length<10||e.duration>t.P){if(n?e.duration>n.P?(n.entries=[e],n.P=e.duration):e.duration===n.P&&e.startTime===n.entries[0].startTime&&n.entries.push(e):(n={id:e.interactionId,entries:[e],P:e.duration},this.l.set(n.id,n),this.u.push(n)),this.u.sort(((i,s)=>s.P-i.P)),this.u.length>10){let i=this.u.splice(10);for(let s of i)this.l.delete(s.id)}this.p?.(n)}}},Ue=r=>{let e=globalThis.requestIdleCallback||setTimeout;document.visibilityState==="hidden"?r():(r=ie(r),addEventListener("visibilitychange",r,{once:!0,capture:!0}),e((()=>{r(),removeEventListener("visibilitychange",r,{capture:!0})})))},Fe=[200,500],De=(r,e={})=>{if(!globalThis.PerformanceEventTiming||!("interactionId"in PerformanceEventTiming.prototype))return;let t=D();H((()=>{yt();let n,i=S("INP"),s=ne(e,Y),a=d=>{Ue((()=>{for(let g of d)s.h(g);let u=s.L();u&&u.P!==i.value&&(i.value=u.P,i.entries=u.entries,n())}))},c=M("event",a,{durationThreshold:e.durationThreshold??40});n=v(r,i,Fe,e.reportAllChanges),c&&(c.observe({type:"first-input",buffered:!0}),t.onHidden((()=>{a(c.takeRecords()),n(!0)})),F((()=>{s.v(),i=S("INP"),n=v(r,i,Fe,e.reportAllChanges)})))}))},Z=class{constructor(){k(this,"m")}h(e){this.m?.(e)}},Le=[2500,4e3],He=(r,e={})=>{H((()=>{let t=D(),n,i=S("LCP"),s=ne(e,Z),a=d=>{e.reportAllChanges||(d=d.slice(-1));for(let u of d)s.h(u),u.startTime<t.firstHiddenTime&&(i.value=Math.max(u.startTime-B(),0),i.entries=[u],n())},c=M("largest-contentful-paint",a);if(c){n=v(r,i,Le,e.reportAllChanges);let d=ie((()=>{a(c.takeRecords()),c.disconnect(),n(!0)})),u=g=>{g.isTrusted&&(Ue(d),removeEventListener(g.type,u,{capture:!0}))};for(let g of["keydown","click","visibilitychange"])addEventListener(g,u,{capture:!0});F((g=>{i=S("LCP"),n=v(r,i,Le,e.reportAllChanges),te((()=>{i.value=performance.now()-g.timeStamp,n(!0)}))}))}}))},Be=[800,1800],ee=r=>{document.prerendering?H((()=>ee(r))):document.readyState!=="complete"?addEventListener("load",(()=>ee(r)),!0):setTimeout(r)},We=(r,e={})=>{let t=S("TTFB"),n=v(r,t,Be,e.reportAllChanges);ee((()=>{let i=re();i&&(t.value=Math.max(i.responseStart-B(),0),t.entries=[i],n(!0),F((()=>{t=S("TTFB",0),n=v(r,t,Be,e.reportAllChanges),n(!0)})))}))};var bt=2e4,W=class{constructor(e){this.data={lcp:null,cls:null,inp:null,fcp:null,ttfb:null};this.sent=!1;this.timeout=null;this.onReadyCallback=null;this.onReadyCallback=e}initialize(){try{He(this.collectMetric.bind(this),{reportAllChanges:!0}),Ne(this.collectMetric.bind(this),{reportAllChanges:!0}),De(this.collectMetric.bind(this),{reportAllChanges:!0}),se(this.collectMetric.bind(this)),We(this.collectMetric.bind(this)),this.timeout=setTimeout(()=>this.sendData(),bt),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&this.sendData()}),window.addEventListener("pagehide",()=>this.sendData())}catch(e){console.warn("Error initializing web vitals tracking:",e)}}collectMetric(e){if(this.sent)return;let t=e.name.toLowerCase();this.data[t]=e.value}sendData(){this.sent||(this.sent=!0,this.timeout&&(clearTimeout(this.timeout),this.timeout=null),this.onReadyCallback&&this.onReadyCallback(this.data))}getData(){return{...this.data}}};var vt=1e3,V=class{constructor(e,t){this.lastClickAt=new WeakMap;this.tracker=e,this.config=t}initialize(){document.addEventListener("click",this.handleClick.bind(this),!0)}handleClick(e){let t=e.target;this.config.trackButtonClicks&&this.isButton(t)&&this.trackButtonClick(t)}isButton(e){if(e.tagName==="BUTTON"||e.getAttribute("role")==="button")return!0;if(e.tagName==="INPUT"){let i=e.type?.toLowerCase();if(i==="submit"||i==="button")return!0}let t=e.parentElement,n=0;for(;t&&n<3;){if(t.tagName==="BUTTON"||t.getAttribute("role")==="button")return!0;t=t.parentElement,n++}return!1}trackButtonClick(e){let t=this.findButton(e);if(!t||t.hasAttribute("data-rybbit-event"))return;let n=Date.now(),i=this.lastClickAt.get(t);if(i!==void 0&&n-i<vt)return;this.lastClickAt.set(t,n);let s={text:this.getElementText(t),...this.extractDataAttributes(t)};this.tracker.trackButtonClick(s)}extractDataAttributes(e){let t={};for(let n of e.attributes)if(n.name.startsWith("data-rybbit-prop-")){let i=n.name.replace("data-rybbit-prop-","");t[i]=n.value}return t}findButton(e){if(e.tagName==="BUTTON"||e.getAttribute("role")==="button")return e;if(e.tagName==="INPUT"){let i=e.type?.toLowerCase();if(i==="submit"||i==="button")return e}let t=e.parentElement,n=0;for(;t&&n<3;){if(t.tagName==="BUTTON"||t.getAttribute("role")==="button")return t;t=t.parentElement,n++}return null}getElementText(e){let t=e.textContent?.trim().substring(0,100);if(t)return t;let n=e.getAttribute("aria-label")?.trim().substring(0,100);if(n)return n;if(e.tagName==="INPUT"){let s=e.value?.trim().substring(0,100);if(s)return s}let i=e.getAttribute("title")?.trim().substring(0,100);if(i)return i}cleanup(){document.removeEventListener("click",this.handleClick.bind(this),!0)}};var G=class{constructor(e){this.tracker=e}initialize(){document.addEventListener("copy",this.handleCopy.bind(this))}handleCopy(){let e=window.getSelection();if(!e||e.isCollapsed)return;let t=e.toString(),n=t.length;if(n===0)return;let i=e.anchorNode,s=i instanceof HTMLElement?i:i?.parentElement;if(!s)return;let a={text:t.substring(0,500),...n>500&&{textLength:n},sourceElement:s.tagName.toLowerCase()};this.tracker.trackCopy(a)}cleanup(){document.removeEventListener("copy",this.handleCopy.bind(this))}};function ae(r,e){return Object.getOwnPropertyDescriptor(HTMLFormElement.prototype,e)?.get?.call(r)}var z=class{constructor(e,t){this.tracker=e,this.config=t,this.boundHandleSubmit=this.handleSubmit.bind(this),this.boundHandleChange=this.handleChange.bind(this)}initialize(){document.addEventListener("submit",this.boundHandleSubmit,!0),document.addEventListener("change",this.boundHandleChange,!0)}cleanup(){document.removeEventListener("submit",this.boundHandleSubmit,!0),document.removeEventListener("change",this.boundHandleChange,!0)}handleSubmit(e){let t=e.target;if(!(t instanceof HTMLFormElement))return;let n={formId:t.getAttribute("id")||"",formName:t.getAttribute("name")||"",formAction:ae(t,"action")||"",method:(ae(t,"method")||"get").toUpperCase(),fieldCount:ae(t,"elements")?.length??0,ariaLabel:t.getAttribute("aria-label")||void 0,...this.extractDataAttributes(t)};this.tracker.trackFormSubmit(n)}handleChange(e){let t=e.target;if(!(t instanceof Element))return;let n=t.tagName.toUpperCase();if(!["INPUT","SELECT","TEXTAREA"].includes(n)||t.disabled||t.readOnly)return;if(n==="INPUT"){let a=t.type?.toLowerCase();if(a==="hidden"||a==="password")return}let i=t.name||t.id||t.getAttribute("aria-label")||t.placeholder||"",s={element:n.toLowerCase(),inputType:n==="INPUT"?t.type?.toLowerCase():void 0,inputName:i,formId:t.form?.getAttribute("id")||void 0,formName:t.form?.getAttribute("name")||void 0,...this.extractDataAttributes(t)};this.tracker.trackInputChange(s)}extractDataAttributes(e){let t={};for(let n of e.attributes)if(n.name.startsWith("data-rybbit-prop-")){let i=n.name.replace("data-rybbit-prop-","");t[i]=n.value}return t}};function St(r){if(window.__RYBBIT_OPTOUT__)return!0;try{return localStorage.getItem(r)!==null}catch{return!1}}(async function(){let r=document.currentScript||document.querySelector('script[src*="/script.js"]');if(!r){console.error("Could not find current script tag");return}let e=r.getAttribute("data-namespace")||"rybbit",t=`disable-${e}`;if(St(t)){window[e]={pageview:()=>{},event:()=>{},error:()=>{},trackOutbound:()=>{},identify:()=>{},setTraits:()=>{},clearUserId:()=>{},getUserId:()=>null,flag:(o,l)=>l,flagPayload:(o,l)=>l,flags:()=>({}),flagPayloads:()=>({}),onReady:()=>{},startSessionReplay:()=>{},stopSessionReplay:()=>{},isSessionReplayActive:()=>!1};return}let n=[],i=o=>(...l)=>{n.push([o,l])};window[e]={pageview:i("pageview"),event:i("event"),error:i("error"),trackOutbound:i("trackOutbound"),identify:i("identify"),setTraits:i("setTraits"),clearUserId:i("clearUserId"),getUserId:()=>null,flag:(o,l)=>l,flagPayload:(o,l)=>l,flags:()=>({}),flagPayloads:()=>({}),onReady:i("onReady"),startSessionReplay:i("startSessionReplay"),stopSessionReplay:i("stopSessionReplay"),isSessionReplayActive:()=>!1};let s=await he(r);if(!s)return;let a=new O(s);s.enableWebVitals&&new W(l=>{a.trackWebVitals(l)}).initialize();let c=null,d=null,u=null;s.trackButtonClicks&&(c=new V(a,s),c.initialize()),s.trackCopy&&(d=new G(a),d.initialize()),s.trackFormInteractions&&(u=new z(a,s),u.initialize()),s.trackErrors&&(window.addEventListener("error",o=>{a.trackError(o.error||new Error(o.message),{filename:o.filename,lineno:o.lineno,colno:o.colno})}),window.addEventListener("unhandledrejection",o=>{let l=o.reason instanceof Error?o.reason:new Error(String(o.reason));a.trackError(l,{type:"unhandledrejection"})}));let g=()=>a.trackPageviewIfUrlChanged(),y=s.debounceDuration>0?pe(g,s.debounceDuration):g;function R(){if(document.addEventListener("click",function(o){let l=o.target;for(;l&&l!==document.documentElement;){if(l.hasAttribute("data-rybbit-event")){let p=l.getAttribute("data-rybbit-event");if(p){let w={};for(let E of l.attributes)if(E.name.startsWith("data-rybbit-prop-")){let C=E.name.replace("data-rybbit-prop-","");w[C]=E.value}a.trackEvent(p,w)}break}l=l.parentElement}if(s.trackOutbound){let p=o.target.closest("a");p?.href&&ge(p.href)&&a.trackOutbound(p.href,p.innerText||p.textContent||"",p.target||"_self")}}),s.autoTrackSpa){let o=history.pushState,l=history.replaceState;history.pushState=function(...p){o.apply(this,p),y(),a.onPageChange()},history.replaceState=function(...p){l.apply(this,p),y(),a.onPageChange()},window.addEventListener("popstate",()=>{y(),a.onPageChange()}),window.addEventListener("hashchange",()=>{y(),a.onPageChange()})}}window[s.namespace]={pageview:()=>a.trackPageview(),event:(o,l={})=>a.trackEvent(o,l),error:(o,l={})=>a.trackError(o,l),trackOutbound:(o,l="",p="_self")=>a.trackOutbound(o,l,p),identify:(o,l)=>a.identify(o,l),setTraits:o=>a.setTraits(o),clearUserId:()=>a.clearUserId(),getUserId:()=>a.getUserId(),flag:(o,l)=>a.getFeatureFlag(o,l),flagPayload:(o,l)=>a.getFeatureFlagPayload(o,l),flags:()=>a.getFeatureFlags(),flagPayloads:()=>a.getFeatureFlagPayloads(),onReady:o=>o(window[s.namespace]),startSessionReplay:()=>a.startSessionReplay(),stopSessionReplay:()=>a.stopSessionReplay(),isSessionReplayActive:()=>a.isSessionReplayActive()};let h=window[s.namespace];for(let[o,l]of n)h[o](...l);R(),window.addEventListener("beforeunload",()=>{c?.cleanup(),d?.cleanup()}),s.autoTrackPageview&&a.trackPageview()})();})();
diff --git a/server/src/analytics-script/formTracking.test.ts b/server/src/analytics-script/formTracking.test.ts
new file mode 100644
index 000000000..9d86ad550
--- /dev/null
+++ b/server/src/analytics-script/formTracking.test.ts
@@ -0,0 +1,61 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { FormTrackingManager } from "./formTracking.js";
+import type { Tracker } from "./tracking.js";
+import type { ScriptConfig } from "./types.js";
+
+describe("FormTrackingManager", () => {
+ let manager: FormTrackingManager;
+ let tracker: { trackFormSubmit: ReturnType<typeof vi.fn>; trackInputChange: ReturnType<typeof vi.fn> };
+
+ beforeEach(() => {
+ tracker = { trackFormSubmit: vi.fn(), trackInputChange: vi.fn() };
+ manager = new FormTrackingManager(tracker as unknown as Tracker, {} as ScriptConfig);
+ manager.initialize();
+ });
+
+ afterEach(() => {
+ manager.cleanup();
+ document.body.innerHTML = "";
+ });
+
+ // Browsers expose each named control as a property of its form, shadowing the
+ // form's own property of that name. jsdom doesn't, so define them the way a
+ // browser resolves them.
+ function shadowWithControls(form: HTMLFormElement) {
+ for (const control of Array.from(form.querySelectorAll("input[name]"))) {
+ Object.defineProperty(form, control.getAttribute("name")!, { configurable: true, value: control });
+ }
+ }
+
+ it("reads the form's own metadata when controls shadow it", () => {
+ document.body.innerHTML = `
+ <form id="signup" name="signup-form" action="/register" method="post">
+ <input name="name"><input name="method"><input name="id"><input name="action"><input name="elements">
+ </form>`;
+ const form = document.querySelector("form")!;
+ shadowWithControls(form);
+
+ form.dispatchEvent(new Event("submit", { bubbles: true }));
+
+ expect(tracker.trackFormSubmit).toHaveBeenCalledWith({
+ formId: "signup",
+ formName: "signup-form",
+ formAction: new URL("/register", document.baseURI).href,
+ method: "POST",
+ fieldCount: 5,
+ ariaLabel: undefined,
+ });
+ });
+
+ it("reads the owning form's id and name for input changes when controls shadow them", () => {
+ document.body.innerHTML = `<form id="search" name="search-form"><input name="id"><input name="name"><input name="q"></form>`;
+ const form = document.querySelector("form")!;
+ shadowWithControls(form);
+
+ form.querySelector<HTMLInputElement>('input[name="q"]')!.dispatchEvent(new Event("change", { bubbles: true }));
+
+ expect(tracker.trackInputChange).toHaveBeenCalledWith(
+ expect.objectContaining({ inputName: "q", formId: "search", formName: "search-form" })
+ );
+ });
+});
diff --git a/server/src/analytics-script/formTracking.ts b/server/src/analytics-script/formTracking.ts
index b2a3cb215..14a063e85 100644
--- a/server/src/analytics-script/formTracking.ts
+++ b/server/src/analytics-script/formTracking.ts
@@ -1,6 +1,19 @@
import { ScriptConfig, FormSubmitProperties, InputChangeProperties } from "./types.js";
import { Tracker } from "./tracking.js";
+/**
+ * A form's own property, read past its controls. A control named like the
+ * property (<input name="method">, "action", "elements") replaces it on the
+ * form itself β€” form.method is then that input, and .toUpperCase() throws β€”
+ * but the prototype's getter still returns the real value.
+ */
+function getFormProperty<K extends "action" | "method" | "elements">(
+ form: HTMLFormElement,
+ key: K
+): HTMLFormElement[K] | undefined {
+ return Object.getOwnPropertyDescriptor(HTMLFormElement.prototype, key)?.get?.call(form);
+}
+
export class FormTrackingManager {
private tracker: Tracker;
private config: ScriptConfig;
@@ -28,15 +41,18 @@ export class FormTrackingManager {
}
private handleSubmit(event: Event): void {
- const form = event.target as HTMLFormElement;
- if (form.tagName !== "FORM") return;
+ const form = event.target;
+ if (!(form instanceof HTMLFormElement)) return;
+ // id and name come from their attributes, which controls named "id" or
+ // "name" can't shadow; form.name would be that input, an object the server
+ // rejects as a form name.
const properties: FormSubmitProperties = {
- formId: form.id || "",
- formName: form.name || "",
- formAction: form.action || "",
- method: (form.method || "get").toUpperCase(),
- fieldCount: form.elements.length,
+ formId: form.getAttribute("id") || "",
+ formName: form.getAttribute("name") || "",
+ formAction: getFormProperty(form, "action") || "",
+ method: (getFormProperty(form, "method") || "get").toUpperCase(),
+ fieldCount: getFormProperty(form, "elements")?.length ?? 0,
ariaLabel: form.getAttribute("aria-label") || undefined,
...this.extractDataAttributes(form),
};
@@ -79,8 +95,8 @@ export class FormTrackingManager {
element: tagName.toLowerCase(),
inputType: tagName === "INPUT" ? (target as HTMLInputElement).type?.toLowerCase() : undefined,
inputName,
- formId: (target as HTMLInputElement).form?.id || undefined,
- formName: (target as HTMLInputElement).form?.name || undefined,
+ formId: (target as HTMLInputElement).form?.getAttribute("id") || undefined,
+ formName: (target as HTMLInputElement).form?.getAttribute("name") || undefined,
...this.extractDataAttributes(target),
};
diff --git a/server/src/analytics-script/index.ts b/server/src/analytics-script/index.ts
index a5ac68f60..d6fd5e4d0 100644
--- a/server/src/analytics-script/index.ts
+++ b/server/src/analytics-script/index.ts
@@ -249,11 +249,12 @@ function hasOptedOut(optOutKey: string): boolean {
// Initialize
setupEventListeners();
- // Setup cleanup on page unload
+ // Setup cleanup on page unload. Session replay is left running: it flushes
+ // itself on page hide, and beforeunload also fires for pages that stay (a
+ // cancelled leave prompt, a download link) or come back from the bfcache.
window.addEventListener("beforeunload", () => {
clickManager?.cleanup();
copyManager?.cleanup();
- tracker.cleanup();
});
// Track initial pageview if enabled
diff --git a/server/src/analytics-script/sessionReplay.test.ts b/server/src/analytics-script/sessionReplay.test.ts
index e6c86296f..2f9c315c2 100644
--- a/server/src/analytics-script/sessionReplay.test.ts
+++ b/server/src/analytics-script/sessionReplay.test.ts
@@ -113,3 +113,203 @@ describe("SessionReplayRecorder size cap", () => {
expect(sendBatch).toHaveBeenCalledOnce();
});
});
+
+describe("SessionReplayRecorder retries", () => {
+ let emit: (event: unknown) => void;
+ let recorder: SessionReplayRecorder;
+ let sendBatch: ReturnType<typeof vi.fn>;
+
+ beforeEach(async () => {
+ window.rrweb = {
+ record: vi.fn(options => {
+ emit = options.emit;
+ return vi.fn();
+ }),
+ };
+
+ sendBatch = vi.fn().mockResolvedValue(undefined);
+ recorder = new SessionReplayRecorder(config, "employee-alice", sendBatch);
+ await recorder.initialize();
+ });
+
+ afterEach(() => {
+ recorder.cleanup();
+ delete window.rrweb;
+ });
+
+ it("retries a failed batch under the identity it was recorded with", async () => {
+ sendBatch.mockRejectedValueOnce(new Error("503"));
+ emit({ type: 2, data: { user: "employee-alice" }, timestamp: 1_700_000_000_000 });
+
+ recorder.updateUserId("employee-bob");
+ await vi.waitFor(() => expect(sendBatch).toHaveBeenCalledTimes(1));
+
+ emit({ type: 3, data: { user: "employee-bob" }, timestamp: 1_700_000_001_000 });
+ recorder.onPageChange();
+
+ await vi.waitFor(() => expect(sendBatch).toHaveBeenCalledTimes(3));
+ const retried = sendBatch.mock.calls.slice(1).map(([batch]) => batch);
+ expect(retried).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ userId: "employee-alice", events: [expect.objectContaining({ type: 2 })] }),
+ expect.objectContaining({ userId: "employee-bob", events: [expect.objectContaining({ type: 3 })] }),
+ ])
+ );
+ });
+
+ it("merges repeated failures from one identity and page into a single retry", async () => {
+ sendBatch
+ .mockRejectedValueOnce(new Error("offline"))
+ .mockRejectedValueOnce(new Error("offline"))
+ .mockRejectedValueOnce(new Error("offline"));
+
+ emit({ type: 2, data: {}, timestamp: 1_700_000_000_000 });
+ recorder.onPageChange();
+ await vi.waitFor(() => expect(sendBatch).toHaveBeenCalledTimes(1));
+ emit({ type: 3, data: {}, timestamp: 1_700_000_001_000 });
+ recorder.onPageChange();
+ await vi.waitFor(() => expect(sendBatch).toHaveBeenCalledTimes(3));
+
+ recorder.onPageChange();
+ await vi.waitFor(() => expect(sendBatch).toHaveBeenCalledTimes(4));
+ expect(sendBatch.mock.calls[3][0].events.map((event: { type: number }) => event.type)).toEqual([2, 3]);
+ });
+});
+
+describe("SessionReplayRecorder page hide", () => {
+ let emit: (event: unknown) => void;
+ let stopRrweb: ReturnType<typeof vi.fn>;
+ let recorder: SessionReplayRecorder;
+ let sendBatch: ReturnType<typeof vi.fn>;
+
+ beforeEach(async () => {
+ stopRrweb = vi.fn();
+ window.rrweb = {
+ record: vi.fn(options => {
+ emit = options.emit;
+ return stopRrweb;
+ }),
+ };
+
+ sendBatch = vi.fn().mockResolvedValue(undefined);
+ recorder = new SessionReplayRecorder(config, "visitor", sendBatch);
+ await recorder.initialize();
+ });
+
+ afterEach(() => {
+ recorder.cleanup();
+ delete window.rrweb;
+ });
+
+ it("flushes the buffer on pagehide and keeps recording", async () => {
+ emit({ type: 3, data: {}, timestamp: 1_700_000_000_000 });
+
+ window.dispatchEvent(new Event("pagehide"));
+
+ expect(sendBatch).toHaveBeenCalledOnce();
+ // A page restored from the back/forward cache carries on recording.
+ expect(recorder.isActive()).toBe(true);
+ expect(stopRrweb).not.toHaveBeenCalled();
+ });
+
+ it("flushes when the page becomes hidden", async () => {
+ emit({ type: 3, data: {}, timestamp: 1_700_000_000_000 });
+ const visibility = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden");
+
+ document.dispatchEvent(new Event("visibilitychange"));
+ visibility.mockRestore();
+
+ expect(sendBatch).toHaveBeenCalledWith(expect.objectContaining({ events: [expect.anything()] }));
+ });
+});
+
+describe("SessionReplayRecorder full snapshot requests", () => {
+ let emit: (event: unknown) => void;
+ let takeFullSnapshot: ReturnType<typeof vi.fn>;
+ let recorder: SessionReplayRecorder;
+ let sendBatch: ReturnType<typeof vi.fn>;
+
+ beforeEach(async () => {
+ takeFullSnapshot = vi.fn(() => {
+ emit({ type: 4, data: {}, timestamp: 1_700_000_002_000 });
+ emit({ type: 2, data: {}, timestamp: 1_700_000_002_000 });
+ });
+ window.rrweb = {
+ record: Object.assign(
+ vi.fn(options => {
+ emit = options.emit;
+ return vi.fn();
+ }),
+ { takeFullSnapshot }
+ ),
+ };
+
+ sendBatch = vi.fn().mockResolvedValue(undefined);
+ recorder = new SessionReplayRecorder(config, "visitor", sendBatch);
+ await recorder.initialize();
+ });
+
+ afterEach(() => {
+ recorder.cleanup();
+ delete window.rrweb;
+ });
+
+ const typesSent = () =>
+ sendBatch.mock.calls.map(([batch]) => batch.events.map((event: { type: number }) => event.type));
+
+ it("sends what came before, then a batch that opens with the snapshot", async () => {
+ sendBatch.mockResolvedValueOnce({ success: true, takeFullSnapshot: true });
+ emit({ type: 3, data: {}, timestamp: 1_700_000_000_000 });
+ recorder.onPageChange();
+ emit({ type: 3, data: {}, timestamp: 1_700_000_001_000 });
+
+ await vi.waitFor(() => expect(sendBatch).toHaveBeenCalledTimes(3));
+ expect(takeFullSnapshot).toHaveBeenCalledWith(true);
+ expect(typesSent()).toEqual([[3], [3], [4, 2]]);
+ });
+
+ it("takes one snapshot for several batches that ask at once", async () => {
+ sendBatch.mockResolvedValue({ success: true, takeFullSnapshot: true });
+ emit({ type: 3, data: {}, timestamp: 1_700_000_000_000 });
+ recorder.onPageChange();
+ emit({ type: 3, data: {}, timestamp: 1_700_000_001_000 });
+ recorder.onPageChange();
+
+ await vi.waitFor(() => expect(sendBatch).toHaveBeenCalledTimes(3));
+ expect(takeFullSnapshot).toHaveBeenCalledOnce();
+ });
+
+ it("runs a request that arrives inside the cooldown once the cooldown ends", async () => {
+ vi.useFakeTimers();
+ try {
+ // A rollover asks for a snapshot; five seconds later identify() moves the
+ // visitor again and the server asks once more.
+ sendBatch.mockResolvedValueOnce({ success: true, takeFullSnapshot: true });
+ emit({ type: 3, data: {}, timestamp: 1_700_000_000_000 });
+ recorder.onPageChange();
+ await vi.advanceTimersByTimeAsync(0);
+ expect(takeFullSnapshot).toHaveBeenCalledTimes(1);
+
+ await vi.advanceTimersByTimeAsync(5_000);
+ sendBatch.mockResolvedValueOnce({ success: true, takeFullSnapshot: true });
+ emit({ type: 3, data: {}, timestamp: 1_700_000_005_000 });
+ recorder.onPageChange();
+ await vi.advanceTimersByTimeAsync(0);
+ expect(takeFullSnapshot).toHaveBeenCalledTimes(1);
+
+ await vi.advanceTimersByTimeAsync(5_000);
+ expect(takeFullSnapshot).toHaveBeenCalledTimes(2);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it("ignores the request once recording has stopped", async () => {
+ sendBatch.mockResolvedValueOnce({ success: true, takeFullSnapshot: true });
+ emit({ type: 3, data: {}, timestamp: 1_700_000_000_000 });
+ recorder.stopRecording();
+
+ await vi.waitFor(() => expect(sendBatch).toHaveBeenCalledOnce());
+ expect(takeFullSnapshot).not.toHaveBeenCalled();
+ });
+});
diff --git a/server/src/analytics-script/sessionReplay.ts b/server/src/analytics-script/sessionReplay.ts
index 1592dd28e..9922f0ef6 100644
--- a/server/src/analytics-script/sessionReplay.ts
+++ b/server/src/analytics-script/sessionReplay.ts
@@ -1,7 +1,14 @@
import { ScriptConfig, SessionReplayEvent, SessionReplayBatch, SessionReplayBatchResult } from "./types.js";
+import { getBotSignalMask } from "./botSignals.js";
+
+type SendSessionReplayBatch = (batch: SessionReplayBatch) => Promise<SessionReplayBatchResult | void>;
const SAMPLE_STORAGE_KEY = "rybbit-replay-sampled";
+// Batches that left before a requested snapshot reached the server ask for one
+// too; this covers the snapshot's own round trip and a retry.
+const FULL_SNAPSHOT_REQUEST_INTERVAL_MS = 10_000;
+
/**
* Determines if this session should have replay enabled based on sample rate.
* Uses sessionStorage to persist the decision for the entire browser session.
@@ -33,7 +40,7 @@ function shouldSampleSession(sampleRate: number): boolean {
declare global {
interface Window {
rrweb?: {
- record: (options: {
+ record: ((options: {
emit: (event: any) => void;
checkoutEveryNms?: number;
checkoutEveryNth?: number;
@@ -49,7 +56,9 @@ declare global {
sampling?: Record<string, any>;
recordCanvas?: boolean;
collectFonts?: boolean;
- }) => () => void;
+ }) => () => void) & {
+ takeFullSnapshot?: (isCheckout?: boolean) => void;
+ };
};
}
}
@@ -63,17 +72,31 @@ export class SessionReplayRecorder {
private stopRecordingFn?: () => void;
private userId: string;
private eventBuffer: SessionReplayEvent[] = [];
+ // Batches whose send failed, each kept as it was built so a retry after
+ // identify(), clearUserId() or a navigation still carries the identity and
+ // page it was recorded under.
+ private failedBatches: SessionReplayBatch[] = [];
private batchTimer?: number;
- private sendBatch: (batch: SessionReplayBatch) => Promise<SessionReplayBatchResult | void>;
+ private lastFullSnapshotRequestAt = -Infinity;
+ private deferredSnapshotTimer?: number;
+ private sendBatch: SendSessionReplayBatch;
- constructor(
- config: ScriptConfig,
- userId: string,
- sendBatch: (batch: SessionReplayBatch) => Promise<SessionReplayBatchResult | void>
- ) {
+ constructor(config: ScriptConfig, userId: string, sendBatch: SendSessionReplayBatch) {
this.config = config;
this.userId = userId;
this.sendBatch = sendBatch;
+
+ // The page may be about to unload: send what's buffered now (batches go
+ // with keepalive while they fit) so the end of the replay survives it.
+ // Recording itself carries on β€” a hidden tab can come back, and a page
+ // restored from the back/forward cache resumes with its recorder intact,
+ // which stopping on beforeunload broke.
+ document.addEventListener("visibilitychange", () => {
+ if (document.visibilityState === "hidden") {
+ void this.flushEvents();
+ }
+ });
+ window.addEventListener("pagehide", () => void this.flushEvents());
}
async initialize(): Promise<void> {
@@ -200,9 +223,13 @@ export class SessionReplayRecorder {
this.isRecording = false;
this.clearBatchTimer();
+ if (this.deferredSnapshotTimer !== undefined) {
+ clearTimeout(this.deferredSnapshotTimer);
+ this.deferredSnapshotTimer = undefined;
+ }
- // Send any remaining events
- if (this.eventBuffer.length > 0) {
+ // Send any remaining events, and retry failed batches while there's a chance
+ if (this.eventBuffer.length > 0 || this.failedBatches.length > 0) {
this.flushEvents();
}
}
@@ -223,7 +250,7 @@ export class SessionReplayRecorder {
private setupBatchTimer(): void {
this.clearBatchTimer();
this.batchTimer = window.setInterval(() => {
- if (this.eventBuffer.length > 0) {
+ if (this.eventBuffer.length > 0 || this.failedBatches.length > 0) {
this.flushEvents();
}
}, this.config.sessionReplayBatchInterval);
@@ -236,34 +263,90 @@ export class SessionReplayRecorder {
}
}
+ // Sends the buffered events, and retries the batches that failed before.
private async flushEvents(): Promise<void> {
- if (this.eventBuffer.length === 0) {
- return;
- }
+ const batches = this.failedBatches;
+ this.failedBatches = [];
- const events = [...this.eventBuffer];
- this.eventBuffer = [];
+ if (this.eventBuffer.length > 0) {
+ // Built before the first await, so the identity and page are the ones
+ // the events were recorded under.
+ batches.push({
+ userId: this.userId,
+ events: this.eventBuffer,
+ metadata: {
+ pageUrl: window.location.href,
+ viewportWidth: screen.width,
+ viewportHeight: screen.height,
+ language: navigator.language,
+ },
+ _bsm: getBotSignalMask(),
+ });
+ this.eventBuffer = [];
+ }
- const batch: SessionReplayBatch = {
- userId: this.userId,
- events,
- metadata: {
- pageUrl: window.location.href,
- viewportWidth: screen.width,
- viewportHeight: screen.height,
- language: navigator.language,
- },
- };
+ await Promise.all(batches.map(batch => this.sendOrRequeue(batch)));
+ }
+ private async sendOrRequeue(batch: SessionReplayBatch): Promise<void> {
try {
const result = await this.sendBatch(batch);
if (result?.stopRecording) {
this.stopForSizeCap();
+ } else if (result?.takeFullSnapshot) {
+ this.takeFullSnapshot();
+ } else if (this.deferredSnapshotTimer !== undefined && batch.events.some(event => event.type === 2)) {
+ // The server accepted a snapshot without asking for another.
+ clearTimeout(this.deferredSnapshotTimer);
+ this.deferredSnapshotTimer = undefined;
}
} catch (error) {
- // Re-queue the events for retry since this batch failed
- this.eventBuffer.unshift(...events);
+ if (!this.capped) {
+ this.requeue(batch);
+ }
+ }
+ }
+
+ // A failure from the same identity and page as the last queued one joins it,
+ // so an outage retries one batch per flush rather than one per failed send.
+ private requeue(batch: SessionReplayBatch): void {
+ const last = this.failedBatches[this.failedBatches.length - 1];
+ if (last && last.userId === batch.userId && JSON.stringify(last.metadata) === JSON.stringify(batch.metadata)) {
+ last.events = last.events.concat(batch.events);
+ return;
+ }
+ this.failedBatches.push(batch);
+ }
+
+ // The server keeps the replay in its previous session until a batch opens
+ // the visitor's new one with a full snapshot, without which a replay can't
+ // be played. Events recorded before the snapshot go first, to the old one.
+ private takeFullSnapshot(): void {
+ if (!this.isRecording) {
+ return;
+ }
+ // A request inside the cooldown (e.g. identify() right after a rollover)
+ // runs when it ends rather than being dropped: the newer session needs
+ // its own snapshot too.
+ const wait = this.lastFullSnapshotRequestAt + FULL_SNAPSHOT_REQUEST_INTERVAL_MS - Date.now();
+ if (wait > 0) {
+ if (this.deferredSnapshotTimer === undefined) {
+ this.deferredSnapshotTimer = window.setTimeout(() => {
+ this.deferredSnapshotTimer = undefined;
+ this.takeFullSnapshot();
+ }, wait);
+ }
+ return;
+ }
+ this.lastFullSnapshotRequestAt = Date.now();
+
+ void this.flushEvents();
+ try {
+ window.rrweb?.record.takeFullSnapshot?.(true);
+ } catch {
+ // rrweb isn't recording after all; its next checkout brings the snapshot.
}
+ void this.flushEvents();
}
// Unlike stopRecording, drops what's buffered: the server refuses it anyway.
@@ -276,6 +359,7 @@ export class SessionReplayRecorder {
this.isRecording = false;
this.clearBatchTimer();
this.eventBuffer = [];
+ this.failedBatches = [];
}
// Update user ID when it changes
@@ -285,8 +369,8 @@ export class SessionReplayRecorder {
}
// Keep events captured under the previous identity in their original batch.
- // flushEvents snapshots this.userId before its first await, so the identity
- // can be changed immediately after starting the send.
+ // flushEvents builds the batch before its first await, so the identity can
+ // be changed immediately after starting the send.
if (this.eventBuffer.length > 0) {
void this.flushEvents();
}
diff --git a/server/src/analytics-script/tracking.test.ts b/server/src/analytics-script/tracking.test.ts
index c4c16568e..aa48b2c78 100644
--- a/server/src/analytics-script/tracking.test.ts
+++ b/server/src/analytics-script/tracking.test.ts
@@ -202,6 +202,27 @@ describe("Tracker", () => {
expect(payload?.querystring).toBe("");
});
+ it("cuts a long title and referrer to the server's limits without splitting a character", () => {
+ Object.defineProperty(document, "title", { value: `${"t".repeat(511)}πŸ˜€`, writable: true });
+ Object.defineProperty(document, "referrer", { value: `https://google.com/${"r".repeat(3000)}`, writable: true });
+
+ const payload = tracker.createBasePayload()!;
+
+ expect(payload.referrer).toHaveLength(2048);
+ expect(payload.page_title).toBe("t".repeat(511));
+ });
+
+ it("sends a long path and query string whole, for the server's exclusions to see", () => {
+ // The server cuts them for storage only after matching Site Exclusions.
+ mockLocation.pathname = `/${"p".repeat(3000)}`;
+ mockLocation.search = `?padding=${"q".repeat(3000)}&preview=1`;
+
+ const payload = tracker.createBasePayload()!;
+
+ expect(payload.pathname).toBe(mockLocation.pathname);
+ expect(payload.querystring).toBe(mockLocation.search);
+ });
+
it("should include user ID when set", () => {
vi.mocked(window.localStorage.getItem).mockReturnValue("user-123");
tracker = new Tracker(config);
@@ -316,6 +337,18 @@ describe("Tracker", () => {
);
});
+ it("fits outbound properties within the server's limit", () => {
+ const url = `https://external.com/?ref=${"a".repeat(3000)}`;
+ tracker.trackOutbound(url, "card text ".repeat(500), "_blank");
+
+ const body = JSON.parse(vi.mocked(global.fetch).mock.calls[0][1]!.body as string);
+ const properties = JSON.parse(body.properties);
+ expect(body.properties.length).toBeLessThanOrEqual(2048);
+ expect(properties.text).toHaveLength(500);
+ expect(properties.target).toBe("_blank");
+ expect(url.startsWith(properties.url)).toBe(true);
+ });
+
it("should track web vitals", async () => {
const vitals = {
lcp: 2500,
@@ -532,4 +565,69 @@ describe("Tracker", () => {
);
});
});
+
+ describe("session replay batches", () => {
+ type SendReplayBatch = (batch: unknown) => Promise<unknown>;
+ const sendReplayBatch = (batch: unknown) =>
+ (tracker as unknown as { sendSessionReplayBatch: SendReplayBatch }).sendSessionReplayBatch(batch);
+ const batchOfSize = (bytes: number) => ({
+ userId: "",
+ events: [{ type: 3, data: "x".repeat(bytes), timestamp: 1 }],
+ });
+
+ beforeEach(() => {
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ });
+
+ it.each([408, 429, 502, 503, 504])(
+ "rejects on a %i, which stored nothing, so the recorder retries",
+ async status => {
+ vi.mocked(global.fetch).mockResolvedValue({ ok: false, status } as Response);
+
+ await expect(sendReplayBatch(batchOfSize(10))).rejects.toThrow(String(status));
+ }
+ );
+
+ it("does not retry a 500, which may come after the batch's events were stored", async () => {
+ vi.mocked(global.fetch).mockResolvedValue({ ok: false, status: 500 } as Response);
+
+ await expect(sendReplayBatch(batchOfSize(10))).resolves.toBeUndefined();
+ });
+
+ it("drops a batch the server refuses as invalid instead of retrying it", async () => {
+ vi.mocked(global.fetch).mockResolvedValue({ ok: false, status: 400 } as Response);
+
+ await expect(sendReplayBatch(batchOfSize(10))).resolves.toBeUndefined();
+ });
+
+ it("passes the server's stop signal back to the recorder", async () => {
+ vi.mocked(global.fetch).mockResolvedValue({
+ ok: true,
+ status: 200,
+ json: async () => ({ success: true, stopRecording: true }),
+ } as Response);
+
+ await expect(sendReplayBatch(batchOfSize(10))).resolves.toEqual({ success: true, stopRecording: true });
+ });
+
+ it("sends every batch that fits the shared keepalive budget with keepalive", async () => {
+ vi.mocked(global.fetch).mockResolvedValue({ ok: true, status: 200, json: async () => ({}) } as Response);
+ const keepaliveOf = (call: number) => vi.mocked(global.fetch).mock.calls[call][1]!.keepalive;
+
+ // An ordinary batch too: one already in flight when the page hides is
+ // past the flush that pagehide makes.
+ await sendReplayBatch(batchOfSize(10));
+ await sendReplayBatch(batchOfSize(60 * 1024));
+ expect([keepaliveOf(0), keepaliveOf(1)]).toEqual([true, false]);
+
+ // Two at once: the second would overrun what the page can hold in flight.
+ await Promise.all([sendReplayBatch(batchOfSize(30 * 1024)), sendReplayBatch(batchOfSize(30 * 1024))]);
+ expect([keepaliveOf(2), keepaliveOf(3)]).toEqual([true, false]);
+
+ // Measured in UTF-8 bytes: 20k three-byte characters don't fit.
+ await sendReplayBatch(batchOfSize(0));
+ await sendReplayBatch({ userId: "", events: [{ type: 3, data: "€".repeat(20_000), timestamp: 1 }] });
+ expect([keepaliveOf(4), keepaliveOf(5)]).toEqual([true, false]);
+ });
+ });
});
diff --git a/server/src/analytics-script/tracking.ts b/server/src/analytics-script/tracking.ts
index 8403e9e44..c17ff2637 100644
--- a/server/src/analytics-script/tracking.ts
+++ b/server/src/analytics-script/tracking.ts
@@ -10,13 +10,32 @@ import {
FormSubmitProperties,
InputChangeProperties,
} from "./types.js";
-import { findMatchingPattern } from "./utils.js";
+import { findMatchingPattern, truncateText } from "./utils.js";
import { SessionReplayRecorder } from "./sessionReplay.js";
import { getBotScore, getBotSignalMask } from "./botSignals.js";
import { getTrackedPathname } from "./url.js";
const FEATURE_FLAG_REQUEST_TIMEOUT_MS = 2000;
+// The server's limits (services/tracker/trackingPayload.ts). A value past one
+// used to fail validation, dropping the whole event. The path and query string
+// are sent whole: the server matches Site Exclusions against them before it
+// cuts them for storage, and a cut here could drop the parameter one names.
+const MAX_REFERRER_LENGTH = 2048;
+const MAX_PAGE_TITLE_LENGTH = 512;
+const MAX_PROPERTIES_LENGTH = 2048;
+const MAX_OUTBOUND_TEXT_LENGTH = 500;
+
+// Browsers cap the bodies of a page's in-flight keepalive requests at 64 KiB in
+// total, and the tracker's own events need some of it while the page unloads.
+const REPLAY_KEEPALIVE_BUDGET_BYTES = 48 * 1024;
+
+// Answers to a replay batch that mean the server stored none of it: a timeout,
+// a rate limit, or a backend it couldn't reach. A 500 can come after the
+// batch's events were written (its metadata insert failed), and sending it
+// again would store them twice; any other 4xx would only be refused again.
+const RETRYABLE_REPLAY_STATUSES = [408, 429, 502, 503, 504];
+
export class Tracker {
private config: ScriptConfig;
private customUserId: string | null = null;
@@ -25,6 +44,7 @@ export class Tracker {
private errorDedupeLastCleanup = 0;
private exposedFeatureFlags = new Set<string>();
private lastPageviewUrl: string;
+ private replayKeepaliveBytesInFlight = 0;
constructor(config: ScriptConfig) {
this.config = config;
this.lastPageviewUrl = this.getPageviewUrl();
@@ -131,7 +151,22 @@ export class Tracker {
}
}
+ /**
+ * Resolves once the server has dealt with the batch (stored or deliberately
+ * skipped it); rejects when it is worth sending again, which the recorder does.
+ * Every batch that fits what is left of the keepalive budget is sent with
+ * keepalive, so a navigation can't cancel it: a batch already on its way
+ * when the page hides is out of the buffer that pagehide flushes.
+ */
private async sendSessionReplayBatch(batch: SessionReplayBatch): Promise<SessionReplayBatchResult | void> {
+ const body = JSON.stringify(batch);
+ const keepaliveRoom = REPLAY_KEEPALIVE_BUDGET_BYTES - this.replayKeepaliveBytesInFlight;
+ // UTF-8 never takes fewer bytes than UTF-16 units, so a long body is ruled
+ // out without encoding it.
+ const keepaliveBytes = body.length <= keepaliveRoom ? new Blob([body]).size : Infinity;
+ const useKeepalive = keepaliveBytes <= keepaliveRoom;
+ if (useKeepalive) this.replayKeepaliveBytesInFlight += keepaliveBytes;
+
let response: Response;
try {
response = await fetch(`${this.config.analyticsHost}/session-replay/record/${this.config.siteId}`, {
@@ -139,13 +174,22 @@ export class Tracker {
headers: {
"Content-Type": "application/json",
},
- body: JSON.stringify(batch),
+ body,
mode: "cors",
- keepalive: false, // Disable keepalive for large session replay requests
+ keepalive: useKeepalive,
});
} catch (error) {
console.error("Failed to send session replay batch:", error);
throw error;
+ } finally {
+ if (useKeepalive) this.replayKeepaliveBytesInFlight -= keepaliveBytes;
+ }
+
+ if (!response.ok) {
+ if (RETRYABLE_REPLAY_STATUSES.includes(response.status)) {
+ throw new Error(`Session replay batch failed with status ${response.status}`);
+ }
+ return;
}
// Some skip responses are plain text; only a JSON body can carry a stop signal.
@@ -179,8 +223,8 @@ export class Tracker {
screenWidth: screen.width,
screenHeight: screen.height,
language: navigator.language,
- page_title: document.title,
- referrer: document.referrer,
+ page_title: truncateText(document.title, MAX_PAGE_TITLE_LENGTH),
+ referrer: truncateText(document.referrer, MAX_REFERRER_LENGTH),
_bs: getBotScore(),
_bsm: getBotSignalMask(),
};
@@ -317,7 +361,15 @@ export class Tracker {
}
trackOutbound(url: string, text: string = "", target: string = "_self"): void {
- this.track("outbound", "", { url, text, target });
+ // A link wrapping a whole card carries all of its text, and the serialized
+ // properties must fit the server's limit. Text goes first; a URL still too
+ // long is cut by the overflow, which stays a parseable URL.
+ const properties = { url, text: truncateText(text, MAX_OUTBOUND_TEXT_LENGTH), target };
+ const overflow = JSON.stringify(properties).length - MAX_PROPERTIES_LENGTH;
+ if (overflow > 0) {
+ properties.url = truncateText(url, Math.max(0, url.length - overflow));
+ }
+ this.track("outbound", "", properties);
}
trackWebVitals(vitals: WebVitalsData): void {
diff --git a/server/src/analytics-script/types.ts b/server/src/analytics-script/types.ts
index f08bf8144..ed18b3b9e 100644
--- a/server/src/analytics-script/types.ts
+++ b/server/src/analytics-script/types.ts
@@ -163,6 +163,8 @@ export interface SessionReplayEvent {
export interface SessionReplayBatchResult {
/** The session hit its replay size cap; stop recording for this page. */
stopRecording?: boolean;
+ /** The server needs a full snapshot to move the replay to a new session. */
+ takeFullSnapshot?: boolean;
}
export interface SessionReplayBatch {
@@ -174,4 +176,5 @@ export interface SessionReplayBatch {
viewportHeight?: number;
language?: string;
};
+ _bsm?: number; // Client-side bot detection signal bitmask, as on events
}
diff --git a/server/src/analytics-script/utils.ts b/server/src/analytics-script/utils.ts
index ccca6c4a4..7b6cf089d 100644
--- a/server/src/analytics-script/utils.ts
+++ b/server/src/analytics-script/utils.ts
@@ -80,6 +80,17 @@ export function isOutboundLink(url: string): boolean {
}
}
+/**
+ * Cut a string to at most `maxLength` UTF-16 units without splitting a
+ * surrogate pair: half an emoji serializes to an escape ClickHouse refuses,
+ * which loses the event at insert.
+ */
+export function truncateText(value: string, maxLength: number): string {
+ if (value.length <= maxLength) return value;
+ const lastCode = value.charCodeAt(maxLength - 1);
+ return value.slice(0, lastCode >= 0xd800 && lastCode <= 0xdbff ? maxLength - 1 : maxLength);
+}
+
/**
* Parse JSON safely with fallback
*/
diff --git a/server/src/api/analytics/bots/getBotAiSummary.test.ts b/server/src/api/analytics/bots/getBotAiSummary.test.ts
index 60e964f61..e89e7ee3a 100644
--- a/server/src/api/analytics/bots/getBotAiSummary.test.ts
+++ b/server/src/api/analytics/bots/getBotAiSummary.test.ts
@@ -2,10 +2,11 @@ import { describe, expect, it } from "vitest";
import { buildBotAiSummaryQuery } from "./getBotAiSummary.js";
const baseQuery = { start_date: "2024-01-01", end_date: "2024-01-31", time_zone: "UTC" } as any;
+const SITE_ID = 1;
describe("buildBotAiSummaryQuery", () => {
it("counts crawls per operator, split by purpose", () => {
- const sql = buildBotAiSummaryQuery(baseQuery);
+ const sql = buildBotAiSummaryQuery(baseQuery, SITE_ID);
expect(sql).toContain("bot_operator AS operator");
expect(sql).toContain("countIf(bot_purpose = 'ai_training') AS training_crawls");
@@ -14,7 +15,7 @@ describe("buildBotAiSummaryQuery", () => {
});
it("excludes non-AI bots and rows written before identity shipped", () => {
- const sql = buildBotAiSummaryQuery(baseQuery);
+ const sql = buildBotAiSummaryQuery(baseQuery, SITE_ID);
// An SEO or monitoring bot has an operator too; only the AI purposes belong
// on this surface. Pre-identity rows carry '' and would otherwise group
@@ -24,7 +25,7 @@ describe("buildBotAiSummaryQuery", () => {
});
it("maps AI referrer domains onto the same operator names the bot patterns use", () => {
- const sql = buildBotAiSummaryQuery(baseQuery);
+ const sql = buildBotAiSummaryQuery(baseQuery, SITE_ID);
expect(sql).toContain("'chatgpt.com'");
expect(sql).toContain("'OpenAI'");
@@ -35,22 +36,38 @@ describe("buildBotAiSummaryQuery", () => {
it("does not treat duckduckgo as an AI referrer", () => {
// DuckAssistBot crawls, but a duckduckgo.com referral is organic search.
// Listing it would silently reclassify every DuckDuckGo visit.
- expect(buildBotAiSummaryQuery(baseQuery)).not.toContain("duckduckgo.com");
+ expect(buildBotAiSummaryQuery(baseQuery, SITE_ID)).not.toContain("duckduckgo.com");
});
it("keeps operators that only crawl and operators that only refer", () => {
- const sql = buildBotAiSummaryQuery(baseQuery);
+ const sql = buildBotAiSummaryQuery(baseQuery, SITE_ID);
expect(sql).toContain("FULL OUTER JOIN referrals ON crawls.operator = referrals.operator");
expect(sql).toContain("if(crawls.operator != '', crawls.operator, referrals.operator) AS operator");
});
it("guards the ratio against a zero denominator", () => {
- expect(buildBotAiSummaryQuery(baseQuery)).toContain("if(referrals.referrals = 0, 0,");
+ expect(buildBotAiSummaryQuery(baseQuery, SITE_ID)).toContain("if(referrals.referrals = 0, 0,");
});
it("applies the time window to both halves", () => {
- const sql = buildBotAiSummaryQuery(baseQuery);
+ const sql = buildBotAiSummaryQuery(baseQuery, SITE_ID);
expect(sql.match(/toStartOfDay/g)?.length).toBeGreaterThanOrEqual(2);
});
+
+ it("counts referral visits as sessions, not event rows", () => {
+ const sql = buildBotAiSummaryQuery(baseQuery, SITE_ID);
+
+ expect(sql).toContain("count(DISTINCT session_id) AS referrals");
+ expect(sql).not.toContain("count() AS referrals");
+ });
+
+ it("bounds session-level filter subqueries to the site and window", () => {
+ const filters = JSON.stringify([{ parameter: "event_name", type: "equals", value: ["signup"] }]);
+ const sql = buildBotAiSummaryQuery({ ...baseQuery, filters }, SITE_ID);
+ const subquery = sql.slice(sql.indexOf("session_id IN ("));
+
+ expect(subquery).toContain("WHERE site_id = 1 AND timestamp >=");
+ expect(subquery).toContain("event_name = 'signup'");
+ });
});
diff --git a/server/src/api/analytics/bots/getBotAiSummary.ts b/server/src/api/analytics/bots/getBotAiSummary.ts
index 7e1227970..770103cc1 100644
--- a/server/src/api/analytics/bots/getBotAiSummary.ts
+++ b/server/src/api/analytics/bots/getBotAiSummary.ts
@@ -44,12 +44,14 @@ function buildReferrerOperatorExpression() {
return `transform(domainWithoutWWW(referrer), [${domains}], [${operators}], '')`;
}
-export const buildBotAiSummaryQuery = (query: BotAiSummaryRequest["Querystring"]) => {
+export const buildBotAiSummaryQuery = (query: BotAiSummaryRequest["Querystring"], siteId: number) => {
const timeStatement = getTimeStatement(query);
const botFilterStatement = getBotFilterStatement(query.filters);
// The referral half reads `events`, so it takes the events-surface filter
- // builder rather than the bot-table one.
- const eventFilterStatement = getFilterStatement(query.filters || "");
+ // builder rather than the bot-table one. Its session-level filters
+ // (event_name, channel) are subqueries over events, bounded to this site and
+ // window like the outer query.
+ const eventFilterStatement = getFilterStatement(query.filters || "", siteId, timeStatement);
const referrerOperator = buildReferrerOperatorExpression();
return `
@@ -72,7 +74,9 @@ export const buildBotAiSummaryQuery = (query: BotAiSummaryRequest["Querystring"]
referrals AS (
SELECT
${referrerOperator} AS operator,
- count() AS referrals
+ -- A referral is a visit: every event of a session landing from an
+ -- AI referrer carries its channel, so count sessions, not rows.
+ count(DISTINCT session_id) AS referrals
FROM events
WHERE site_id = {siteId:Int32}
AND channel = 'AI'
@@ -101,7 +105,7 @@ export const getBotAiSummary = analyticsRoute<BotAiSummaryRequest>(
"bot ai summary",
async (req: FastifyRequest<BotAiSummaryRequest>, res: FastifyReply) => {
const data = await runAnalyticsQuery<BotAiSummaryRow>({
- query: buildBotAiSummaryQuery(req.query),
+ query: buildBotAiSummaryQuery(req.query, Number(req.params.siteId)),
params: { siteId: Number(req.params.siteId) },
});
diff --git a/server/src/api/analytics/events/getEvents.test.ts b/server/src/api/analytics/events/getEvents.test.ts
index 376dc77c5..74b0abd5f 100644
--- a/server/src/api/analytics/events/getEvents.test.ts
+++ b/server/src/api/analytics/events/getEvents.test.ts
@@ -7,7 +7,7 @@ vi.mock("../../../db/postgres/postgres.js", () => ({
db: {},
}));
-import { buildEventsQuery } from "./getEvents.js";
+import { buildEventsQuery, eventsCursorError, nextPollCursor, POLL_LIMIT, POLL_OVERLAP_MS } from "./getEvents.js";
import { buildEventBucketedQuery } from "./getEventBucketed.js";
import { buildEventNamesQuery } from "./getEventNames.js";
import { buildSiteEventCountQuery } from "./getSiteEventCount.js";
@@ -53,6 +53,122 @@ describe("buildEventsQuery filters", () => {
});
});
+describe("buildEventsQuery cursors and windows", () => {
+ const CURSOR = "1767607200300_5E2691F3091B005F";
+
+ it("orders by the whole second first, then milliseconds and a tie-breaker", () => {
+ const { query } = buildEventsQuery(baseQuery(), SITE_ID);
+
+ expect(query).toMatch(
+ /ORDER BY timestamp DESC, timestamp_ms DESC, leftPad\(hex\(cityHash64\(.+\)\), 16, '0'\) DESC/
+ );
+ expect(query).toContain("AS event_cursor");
+ });
+
+ it("tells apart events that differ only in their properties or import", () => {
+ const { query } = buildEventsQuery(baseQuery(), SITE_ID);
+
+ expect(query).toMatch(/cityHash64\([^)]*session_id[^)]*toString\(props\), ifNull\(toString\(import_id\), ''\)\)/);
+ });
+
+ it("pages back from a composite cursor without skipping the rest of its second", () => {
+ const { query, params } = buildEventsQuery(baseQuery({ before_cursor: CURSOR }), SITE_ID);
+
+ expect(query).toContain("AND timestamp <= toDateTime(intDiv({beforeMs:Int64}, 1000), 'UTC')");
+ expect(query).toContain("(fromUnixTimestamp64Milli({beforeMs:Int64}, 'UTC'), {beforeTiebreak:String})");
+ expect(query).not.toContain("AND timestamp < ");
+ expect(params).toEqual({ siteId: SITE_ID, limit: 50, beforeMs: 1767607200300, beforeTiebreak: "5E2691F3091B005F" });
+ });
+
+ // Ten minutes after the cursor's event: its overlap has long passed.
+ const LATER = 1767607200300 + 600_000;
+
+ it("polls for the oldest rows strictly after a composite cursor", () => {
+ const { query, params } = buildEventsQuery(baseQuery({ since_cursor: CURSOR }), SITE_ID, LATER);
+
+ expect(query).toContain("AND timestamp >= toDateTime(intDiv({sinceMs:Int64}, 1000), 'UTC')");
+ expect(query).toContain(") > (fromUnixTimestamp64Milli({sinceMs:Int64}, 'UTC'), {sinceTiebreak:String})");
+ expect(query).toMatch(/ORDER BY timestamp, timestamp_ms, leftPad\(.+\)\s+LIMIT \{limit:Int32\}/);
+ expect(params).toEqual({
+ siteId: SITE_ID,
+ limit: POLL_LIMIT,
+ sinceMs: 1767607200300,
+ sinceTiebreak: "5E2691F3091B005F",
+ });
+ });
+
+ it("re-reads the overlap behind a cursor that is newer than it", () => {
+ const now = 1767607200300 + 1_000;
+ const { params } = buildEventsQuery(baseQuery({ since_cursor: CURSOR }), SITE_ID, now);
+
+ // Everything received from `now - POLL_OVERLAP_MS` on.
+ expect(params).toMatchObject({ sinceMs: now - POLL_OVERLAP_MS - 1, sinceTiebreak: "FFFFFFFFFFFFFFFF" });
+ });
+
+ it("keeps the newest-500 timestamp poll for API callers", () => {
+ const { query, params } = buildEventsQuery(baseQuery({ since_timestamp: "2026-01-05 10:00:00" }), SITE_ID);
+
+ expect(query).toContain("AND timestamp > toDateTime64({sinceTimestamp:String}, 3, 'UTC')");
+ expect(query).toContain("ORDER BY timestamp DESC");
+ expect(params).toMatchObject({ limit: 500 });
+ });
+
+ it("keeps the whole-second timestamp params for API callers", () => {
+ const { query } = buildEventsQuery(baseQuery({ before_timestamp: "2026-01-05 10:00:00" }), SITE_ID);
+ expect(query).toContain("AND timestamp < toDateTime64({beforeTimestamp:String}, 3, 'UTC')");
+ });
+
+ it.each([
+ ["past minutes", { past_minutes_start: "30", past_minutes_end: "0" }, "AND timestamp > toDateTime('"],
+ [
+ "datetime range",
+ { start_datetime: "2026-01-05 09:00:00", end_datetime: "2026-01-05 10:00:00" },
+ "AND timestamp >= toDateTime('2026-01-05 09:00:00', 'UTC')",
+ ],
+ ])("bounds a %s window, not only a date range", (_name, window, predicate) => {
+ expect(buildEventsQuery(baseQuery(window), SITE_ID).query).toContain(predicate);
+ });
+
+ it("bounds a session-level channel filter to the window", () => {
+ const filters = JSON.stringify([{ parameter: "channel", type: "equals", value: ["Direct"] }]);
+ const { query } = buildEventsQuery(
+ baseQuery({ filters, start_datetime: "2026-01-05 09:00:00", end_datetime: "2026-01-05 10:00:00" }),
+ SITE_ID
+ );
+ const subquery = query.slice(query.indexOf("session_id IN ("));
+ expect(subquery).toContain("WHERE site_id = 1 AND timestamp >= toDateTime('2026-01-05 09:00:00', 'UTC')");
+ });
+
+ it("rejects a malformed cursor", () => {
+ expect(eventsCursorError(baseQuery({ before_cursor: "2026-01-05 10:00:00" }))).toMatch(/Invalid before_cursor/);
+ expect(eventsCursorError(baseQuery({ since_cursor: "1_xyz" }))).toMatch(/Invalid since_cursor/);
+ expect(eventsCursorError(baseQuery({ before_cursor: CURSOR }))).toBeNull();
+ expect(eventsCursorError(baseQuery())).toBeNull();
+ });
+});
+
+describe("nextPollCursor", () => {
+ const NOW = 1767607200300;
+ const WATERMARK = `${NOW - POLL_OVERLAP_MS - 1}_FFFFFFFFFFFFFFFF`;
+ const page = (lastMs: number) =>
+ Array.from({ length: POLL_LIMIT }, (_, index) => `${lastMs - (POLL_LIMIT - 1 - index)}_0000000000000000`);
+
+ it("resumes at the watermark after a short page, so the overlap is read again", () => {
+ expect(nextPollCursor([`${NOW - 100}_0000000000000000`], NOW)).toEqual({ nextCursor: WATERMARK, hasMore: false });
+ expect(nextPollCursor([], NOW)).toEqual({ nextCursor: WATERMARK, hasMore: false });
+ });
+
+ it("continues straight after a full page that ends before the watermark", () => {
+ const rows = page(NOW - 60_000);
+
+ expect(nextPollCursor(rows, NOW)).toEqual({ nextCursor: rows[rows.length - 1], hasMore: true });
+ });
+
+ it("does not hurry a full page that reaches into the overlap", () => {
+ expect(nextPollCursor(page(NOW - 1_000), NOW)).toEqual({ nextCursor: WATERMARK, hasMore: false });
+ });
+});
+
describe("event summary filter consistency", () => {
const eventNameFilter = JSON.stringify([{ parameter: "event_name", type: "equals", value: ["signup"] }]);
diff --git a/server/src/api/analytics/events/getEvents.ts b/server/src/api/analytics/events/getEvents.ts
index 48653a414..92c49d113 100644
--- a/server/src/api/analytics/events/getEvents.ts
+++ b/server/src/api/analytics/events/getEvents.ts
@@ -31,6 +31,8 @@ export type GetEventsResponse = {
device_type: string;
type: string;
page_title: string;
+ /** The row's place in the log, as a cursor; also tells rows apart. */
+ event_cursor: string;
}[];
interface GetEventsRequest {
@@ -39,11 +41,103 @@ interface GetEventsRequest {
};
Querystring: FilterParams<{
page_size?: string;
+ /** Exact cursors: a cursor from an earlier response, or a row's `event_cursor`. */
+ before_cursor?: string;
+ since_cursor?: string;
+ /** Whole-second bounds, kept for API callers; events sharing the boundary second are skipped. */
since_timestamp?: string;
before_timestamp?: string;
}>;
}
+/**
+ * Events are ordered by `timestamp`, then `timestamp_ms`, then a hash of the
+ * fields that tell two events apart. `timestamp` alone is whole seconds, so a
+ * cursor on it skipped every other event in the boundary second; leading with
+ * it (rather than `timestamp_ms`) keeps the read in the table's sort order.
+ * `timestamp` is `timestamp_ms` truncated to the second, so the three keys sort
+ * the same way as the last two.
+ *
+ * The hash covers every per-event column: imported events carry whole-second
+ * times, so two rows can differ only in their properties. Session-level columns
+ * (browser, location, …) follow from `session_id`. Rows equal in all of these
+ * still share a cursor; without a row id nothing can tell them apart.
+ */
+const EVENT_TIEBREAK_COLUMNS = [
+ "session_id",
+ "user_id",
+ "identified_user_id",
+ "type",
+ "event_name",
+ "hostname",
+ "pathname",
+ "querystring",
+ "page_title",
+ "referrer",
+ "toString(props)",
+ "ifNull(toString(import_id), '')",
+];
+const EVENT_TIEBREAK = `leftPad(hex(cityHash64(${EVENT_TIEBREAK_COLUMNS.join(", ")})), 16, '0')`;
+const EVENT_ORDER = `timestamp DESC, timestamp_ms DESC, ${EVENT_TIEBREAK} DESC`;
+const EVENT_ORDER_ASC = `timestamp, timestamp_ms, ${EVENT_TIEBREAK}`;
+
+/** `<epoch milliseconds>_<16 hex digits>`: fixed width, so cursors also compare as strings. */
+const EVENT_CURSOR = `concat(toString(toUnixTimestamp64Milli(timestamp_ms)), '_', ${EVENT_TIEBREAK}) AS event_cursor`;
+const EVENT_CURSOR_PATTERN = /^(\d{1,15})_([0-9A-F]{16})$/;
+
+export interface EventCursor {
+ ms: number;
+ tiebreak: string;
+}
+
+export const parseEventCursor = (value: string): EventCursor | null => {
+ const match = EVENT_CURSOR_PATTERN.exec(value);
+ return match ? { ms: Number(match[1]), tiebreak: match[2] } : null;
+};
+
+const formatEventCursor = (cursor: EventCursor) => `${cursor.ms}_${cursor.tiebreak}`;
+
+const compareEventCursors = (a: EventCursor, b: EventCursor) =>
+ a.ms - b.ms || (a.tiebreak < b.tiebreak ? -1 : a.tiebreak > b.tiebreak ? 1 : 0);
+
+/** Events per poll; a larger backlog is read oldest first over several polls. */
+export const POLL_LIMIT = 500;
+
+/**
+ * How far back a poll reads again. Events are stamped on receipt but written by
+ * queues that flush every second per worker, so one received earlier can be
+ * inserted after one received later, behind a cursor that has already passed it.
+ */
+export const POLL_OVERLAP_MS = 5000;
+
+/** Sorts after every event received before `now - POLL_OVERLAP_MS`, and before every one received since. */
+const pollWatermark = (now: number): EventCursor => ({ ms: now - POLL_OVERLAP_MS - 1, tiebreak: "F".repeat(16) });
+
+/**
+ * Where the next poll resumes, given the cursors of this poll's rows (oldest
+ * first) and the time it started. A full page that ends before the watermark
+ * has more to read straight away. Otherwise every event before the watermark
+ * has been read, and the next poll starts there, re-reading the overlap; the
+ * client drops the rows it already has by their cursors.
+ */
+export const nextPollCursor = (rowCursors: string[], now: number): { nextCursor: string; hasMore: boolean } => {
+ const watermark = pollWatermark(now);
+ const last = rowCursors.length >= POLL_LIMIT ? parseEventCursor(rowCursors[rowCursors.length - 1]) : null;
+ if (last && compareEventCursors(last, watermark) < 0) {
+ return { nextCursor: formatEventCursor(last), hasMore: true };
+ }
+ return { nextCursor: formatEventCursor(watermark), hasMore: false };
+};
+
+/** Rows strictly older (`<`) or newer (`>`) than the cursor, in {@link EVENT_ORDER}. */
+const cursorCondition = (direction: "<" | ">", name: string) => {
+ // The whole-second bound is implied by the tuple comparison; it is there so
+ // the primary key can skip granules.
+ const secondBound = `toDateTime(intDiv({${name}Ms:Int64}, 1000), 'UTC')`;
+ return `AND timestamp ${direction}= ${secondBound}
+ AND (timestamp_ms, ${EVENT_TIEBREAK}) ${direction} (fromUnixTimestamp64Milli({${name}Ms:Int64}, 'UTC'), {${name}Tiebreak:String})`;
+};
+
const EVENT_COLUMNS = `
timestamp,
event_name,
@@ -76,66 +170,90 @@ const EVENT_COLUMNS = `
// belong here: the log has always offered an Error type to filter by.
const EVENT_TYPE_FILTER = `AND type IN ('custom_event', 'pageview', 'outbound', 'button_click', 'copy', 'form_submit', 'input_change', 'error')`;
-export const buildEventsQuery = (query: GetEventsRequest["Querystring"], siteId: number): QuerySpec => {
+/** Why a request's cursor params can't be used, or null. */
+export const eventsCursorError = (query: GetEventsRequest["Querystring"]): string | null => {
+ for (const name of ["since_cursor", "before_cursor"] as const) {
+ const value = query[name];
+ if (value !== undefined && value !== "" && !parseEventCursor(value)) {
+ return `Invalid ${name}: pass a cursor from an earlier response`;
+ }
+ }
+ return null;
+};
+
+/** `now` is when the request started; a cursor poll re-reads from {@link POLL_OVERLAP_MS} before it. */
+export const buildEventsQuery = (
+ query: GetEventsRequest["Querystring"],
+ siteId: number,
+ now = Date.now()
+): QuerySpec => {
const { since_timestamp, before_timestamp, page_size: pageSize = "50", filters } = query;
+ const since = query.since_cursor ? parseEventCursor(query.since_cursor) : null;
+ const before = query.before_cursor ? parseEventCursor(query.before_cursor) : null;
const limit = parseInt(pageSize, 10);
+ // Every mode honours the request's window, whichever way it is expressed
+ // (dates, datetimes or past minutes); realtime requests send none.
+ const timeStatement = getTimeStatement(query);
// The event log filters individual rows; only channel remains session-attributed.
const filterStatement = filters
- ? getFilterStatement(filters, siteId, undefined, { sessionLevelParams: ["channel"] })
+ ? getFilterStatement(filters, siteId, timeStatement, { sessionLevelParams: ["channel"] })
: "";
- // Mode A: Poll for new events since a timestamp (Realtime polling)
- if (since_timestamp) {
- return {
- query: `
- SELECT ${EVENT_COLUMNS}
- FROM events
- WHERE
- site_id = {siteId:Int32}
- ${EVENT_TYPE_FILTER}
- AND timestamp > toDateTime64({sinceTimestamp:String}, 3, 'UTC')
- ${filterStatement}
- ORDER BY timestamp DESC
- LIMIT 500
- `,
- params: {
- siteId,
- sinceTimestamp: since_timestamp,
- },
- };
- }
+ const params: Record<string, string | number> = { siteId };
+ let cursorStatement = "";
- // Mode B: Cursor-based pagination (initial load or scrolling back)
- const timeStatement = query.start_date || query.end_date ? getTimeStatement(query) : "";
-
- let cursorCondition = "";
- const queryParams: Record<string, string | number> = {
- siteId,
- limit: Number(limit),
- };
-
- if (before_timestamp) {
+ if (since) {
+ // Never start later than the watermark: events received before the cursor
+ // may still be on their way in.
+ const watermark = pollWatermark(now);
+ const from = compareEventCursors(since, watermark) < 0 ? since : watermark;
+ cursorStatement = cursorCondition(">", "since");
+ params.sinceMs = from.ms;
+ params.sinceTiebreak = from.tiebreak;
+ } else if (since_timestamp) {
+ cursorStatement = `AND timestamp > toDateTime64({sinceTimestamp:String}, 3, 'UTC')`;
+ params.sinceTimestamp = since_timestamp;
+ } else if (before) {
+ cursorStatement = cursorCondition("<", "before");
+ params.beforeMs = before.ms;
+ params.beforeTiebreak = before.tiebreak;
+ } else if (before_timestamp) {
// The API renders timestamps in UTC and the client echoes them back;
// parse in UTC too, not in the server timezone.
- cursorCondition = `AND timestamp < toDateTime64({beforeTimestamp:String}, 3, 'UTC')`;
- queryParams.beforeTimestamp = before_timestamp;
+ cursorStatement = `AND timestamp < toDateTime64({beforeTimestamp:String}, 3, 'UTC')`;
+ params.beforeTimestamp = before_timestamp;
+ }
+
+ // Mode A, polling for events newer than a cursor (realtime), reads the
+ // oldest POLL_LIMIT after it, so a larger backlog is read over several polls
+ // rather than skipped. Polling by timestamp, kept for API callers, still
+ // returns the newest 500. Mode B, the initial load or scrolling back, returns
+ // one page, newest first.
+ let order = EVENT_ORDER;
+ if (since) {
+ order = EVENT_ORDER_ASC;
+ params.limit = POLL_LIMIT;
+ } else if (since_timestamp) {
+ params.limit = 500;
+ } else {
+ params.limit = Number(limit);
}
return {
query: `
- SELECT ${EVENT_COLUMNS}
+ SELECT ${EVENT_COLUMNS}, ${EVENT_CURSOR}
FROM events
WHERE
site_id = {siteId:Int32}
${EVENT_TYPE_FILTER}
${timeStatement}
- ${cursorCondition}
+ ${cursorStatement}
${filterStatement}
- ORDER BY timestamp DESC
+ ORDER BY ${order}
LIMIT {limit:Int32}
`,
- params: queryParams,
+ params,
};
};
@@ -143,12 +261,30 @@ export const getEvents = analyticsRoute<GetEventsRequest>(
"events",
async (req: FastifyRequest<GetEventsRequest>, res: FastifyReply) => {
const { siteId } = req.params;
- const { since_timestamp, page_size: pageSize = "50" } = req.query;
+ const { since_cursor, since_timestamp, page_size: pageSize = "50" } = req.query;
- const events = await runAnalyticsQuery<GetEventsResponse[number]>(buildEventsQuery(req.query, Number(siteId)));
- const eventsWithTraits = await enrichWithTraits(events, Number(siteId));
+ const cursorError = eventsCursorError(req.query);
+ if (cursorError) {
+ return res.status(400).send({ error: cursorError });
+ }
+
+ // Taken before the query runs: whatever was received before the watermark
+ // is in ClickHouse by the time the query reads it.
+ const now = Date.now();
+ const rows = await runAnalyticsQuery<GetEventsResponse[number]>(buildEventsQuery(req.query, Number(siteId), now));
+
+ // Mode A: Poll for new events since a cursor (Realtime polling)
+ if (since_cursor) {
+ const { nextCursor, hasMore } = nextPollCursor(
+ rows.map(row => row.event_cursor),
+ now
+ );
+ // Read oldest first; sent newest first, like every other mode.
+ const data = await enrichWithTraits(rows.reverse(), Number(siteId));
+ return res.send({ data, cursor: { nextCursor, hasMore } });
+ }
- // Mode A: Poll for new events since a timestamp (Realtime polling)
+ const eventsWithTraits = await enrichWithTraits(rows, Number(siteId));
if (since_timestamp) {
return res.send({ data: eventsWithTraits });
}
@@ -158,8 +294,10 @@ export const getEvents = analyticsRoute<GetEventsRequest>(
return res.send({
data: eventsWithTraits,
cursor: {
- hasMore: events.length === limit,
- oldestTimestamp: events.length > 0 ? events[events.length - 1].timestamp : null,
+ hasMore: rows.length === limit,
+ oldestTimestamp: rows.length > 0 ? rows[rows.length - 1].timestamp : null,
+ oldestCursor: rows[rows.length - 1]?.event_cursor ?? null,
+ newestCursor: rows[0]?.event_cursor ?? null,
},
});
}
diff --git a/server/src/api/analytics/getJourneys.test.ts b/server/src/api/analytics/getJourneys.test.ts
index c1371ebbe..a397b8308 100644
--- a/server/src/api/analytics/getJourneys.test.ts
+++ b/server/src/api/analytics/getJourneys.test.ts
@@ -38,7 +38,7 @@ describe("buildJourneysQuery session-scoped filters", () => {
expect(sql).toContain("FilteredSessions AS");
expect(sql).toContain("argMinIf(referrer, timestamp, referrer != '') AS referrer");
expect(sql).toContain("WHERE 1 = 1 AND domainWithoutWWW(referrer) = 'google.com'");
- expect(sql).toContain("FROM events\n INNER JOIN FilteredSessions USING (session_id)");
+ expect(sql).toContain("FROM events\n INNER JOIN FilteredSessions USING (session_id)");
expect(sql).toContain("AND type = 'pageview'");
expect(sql.match(/domainWithoutWWW\(referrer\) = 'google\.com'/g)).toHaveLength(1);
});
@@ -64,6 +64,13 @@ describe("buildJourneysQuery session-scoped filters", () => {
});
describe("buildJourneysQuery", () => {
+ it("orders each session's pages by millisecond time, not the whole-second timestamp", () => {
+ const { query: sql } = journeysSql();
+
+ expect(sql).toContain("arraySort(groupArray((timestamp_ms, pathname)))");
+ expect(sql).not.toContain("ORDER BY session_id, timestamp");
+ });
+
it("takes each journey's share from the sessions with two or more pages", () => {
const { query: sql, params } = journeysSql();
diff --git a/server/src/api/analytics/getMetric.test.ts b/server/src/api/analytics/getMetric.test.ts
index 9edba078e..6149acf0f 100644
--- a/server/src/api/analytics/getMetric.test.ts
+++ b/server/src/api/analytics/getMetric.test.ts
@@ -32,6 +32,13 @@ describe("buildMetricQuery", () => {
expect(sql).toContain("LIMIT 100");
});
+ it("sums each attributed session's pageviews instead of counting sessions", () => {
+ const sql = buildMetricQuery(baseQuery({ parameter: "country" }), SITE_ID);
+ expect(sql).toContain("sum(pageviews_in_session) as pageviews");
+ expect(sql).toContain("sum(sum(pageviews_in_session)) OVER ()");
+ expect(sql).not.toMatch(/COUNT\(\) as pageviews\b/);
+ });
+
it("uses the session-channel aggregation for channel", () => {
const sql = buildMetricQuery(baseQuery({ parameter: "channel" }), SITE_ID);
expect(sql).toContain(`${SESSION_CHANNEL_AGG} as value`);
diff --git a/server/src/api/analytics/getMetric.ts b/server/src/api/analytics/getMetric.ts
index b38230683..e225c0260 100644
--- a/server/src/api/analytics/getMetric.ts
+++ b/server/src/api/analytics/getMetric.ts
@@ -413,8 +413,10 @@ export const buildMetricQuery = (
value,
COUNT(DISTINCT session_id) as count,
round((COUNT(DISTINCT session_id) / sum(COUNT(DISTINCT session_id)) OVER ()) * 100, 2) as percentage,
- COUNT() as pageviews,
- round((COUNT() / sum(COUNT()) OVER ()) * 100, 2) as pageviews_percentage,
+ -- SessionData holds one row per session, so COUNT() here is sessions;
+ -- pageviews are the attributed sessions' pageview totals.
+ sum(pageviews_in_session) as pageviews,
+ round((sum(pageviews_in_session) / nullIf(sum(sum(pageviews_in_session)) OVER (), 0)) * 100, 2) as pageviews_percentage,
round((countIf(DISTINCT session_id, pageviews_in_session = 1) / nullIf(COUNT(DISTINCT session_id), 0)) * 100, 2) as bounce_rate
FROM SessionData
GROUP BY value
diff --git a/server/src/api/analytics/getOverview.test.ts b/server/src/api/analytics/getOverview.test.ts
index 5a38bc18b..20c049b7a 100644
--- a/server/src/api/analytics/getOverview.test.ts
+++ b/server/src/api/analytics/getOverview.test.ts
@@ -33,3 +33,15 @@ describe("unique user counting", () => {
});
});
});
+
+describe("buildOverviewBucketedQuery join", () => {
+ it("takes the bucket from whichever side has it instead of defaulting to 1970", () => {
+ const sql = buildOverviewBucketedQuery(baseParams({ start_date: "", end_date: "" }), SITE_ID);
+
+ expect(sql).toContain("coalesce(session_stats.time, page_stats.time) AS time");
+ expect(sql).toContain("coalesce(session_stats.sessions, 0) AS sessions");
+ expect(sql).toContain("coalesce(page_stats.pageviews, 0) AS pageviews");
+ expect(sql).toContain("SETTINGS join_use_nulls = 1");
+ expect(sql).not.toContain("session_stats.time AS time");
+ });
+});
diff --git a/server/src/api/analytics/getOverviewBucketed.ts b/server/src/api/analytics/getOverviewBucketed.ts
index 799f9704a..d5e327e9a 100644
--- a/server/src/api/analytics/getOverviewBucketed.ts
+++ b/server/src/api/analytics/getOverviewBucketed.ts
@@ -32,13 +32,17 @@ SessionsWithStats AS (
GROUP BY session_id
)
SELECT
- session_stats.time AS time,
- session_stats.sessions,
- session_stats.pages_per_session,
- session_stats.bounce_rate * 100 AS bounce_rate,
- session_stats.session_duration,
- page_stats.pageviews,
- page_stats.users
+ -- A bucket can exist on one side only (with no fill, a day that a session
+ -- crossed into but none started on is page-side only). Without nullable
+ -- join results the missing side's key reads as 1970-01-01, so take
+ -- whichever side has the bucket and zero the other side's metrics.
+ coalesce(session_stats.time, page_stats.time) AS time,
+ coalesce(session_stats.sessions, 0) AS sessions,
+ coalesce(session_stats.pages_per_session, 0) AS pages_per_session,
+ coalesce(session_stats.bounce_rate, 0) * 100 AS bounce_rate,
+ coalesce(session_stats.session_duration, 0) AS session_duration,
+ coalesce(page_stats.pageviews, 0) AS pageviews,
+ coalesce(page_stats.users, 0) AS users
FROM
(
SELECT
@@ -63,8 +67,9 @@ FULL JOIN
${timeStatement}
GROUP BY time ORDER BY time ${fillClause}
) AS page_stats
-USING time
-ORDER BY time`;
+ON session_stats.time = page_stats.time
+ORDER BY time
+SETTINGS join_use_nulls = 1`;
};
type getOverviewBucketed = { time: string; pageviews: number }[];
diff --git a/server/src/api/analytics/journeyPaths.ts b/server/src/api/analytics/journeyPaths.ts
index b235f3b90..5c2a1cb6f 100644
--- a/server/src/api/analytics/journeyPaths.ts
+++ b/server/src/api/analytics/journeyPaths.ts
@@ -191,23 +191,19 @@ export const buildJourneyFragments = (
AND (${options.goalCondition})
)`
: null,
+ // Pages are ordered by millisecond arrival, then pathname for exact ties.
+ // Sorting by the whole-second `timestamp` (and relying on groupArray to keep
+ // a subquery's order) let a same-second redirect land before its source.
`session_paths AS (
SELECT
session_id,
- arrayCompact(groupArray(pathname)) AS path_sequence
- FROM (
- SELECT
- session_id,
- pathname,
- timestamp
- FROM events
- ${filteredSessionsJoin}
- WHERE
- site_id = {siteId:Int32}
- ${timeStatement || ""}
- AND type = 'pageview'
- ORDER BY session_id, timestamp
- )
+ arrayCompact(arrayMap(page -> page.2, arraySort(groupArray((timestamp_ms, pathname))))) AS path_sequence
+ FROM events
+ ${filteredSessionsJoin}
+ WHERE
+ site_id = {siteId:Int32}
+ ${timeStatement || ""}
+ AND type = 'pageview'
GROUP BY session_id
)`,
`user_paths AS (
diff --git a/server/src/api/analytics/lite/getOverviewBucketedLite.ts b/server/src/api/analytics/lite/getOverviewBucketedLite.ts
index 4065ca6bf..af467a03f 100644
--- a/server/src/api/analytics/lite/getOverviewBucketedLite.ts
+++ b/server/src/api/analytics/lite/getOverviewBucketedLite.ts
@@ -79,6 +79,9 @@ function buildHourBucketQuery(args: {
ORDER BY time ${fill}
) s USING time
ORDER BY time
+ -- Without nullable join results a bucket missing from p reads as
+ -- 1970-01-01 rather than NULL, and the coalesces above never fall back.
+ SETTINGS join_use_nulls = 1
`;
}
diff --git a/server/src/api/analytics/users/getUserInfo.test.ts b/server/src/api/analytics/users/getUserInfo.test.ts
index aabf17a79..dc6ea8903 100644
--- a/server/src/api/analytics/users/getUserInfo.test.ts
+++ b/server/src/api/analytics/users/getUserInfo.test.ts
@@ -9,10 +9,20 @@ const state = {
queriedUserIds: [] as string[],
};
+// ClickHouse answers an aggregate without GROUP BY over no rows with one
+// all-default row, not an empty result. The profile query only comes back empty
+// for an unknown user because it filters that row out, so the stub returns the
+// default row unless the query does.
+const emptyProfileResult = (query: string) =>
+ /HAVING count\(\) > 0/.test(query) ? [] : [{ sessions: 0, user_id: "", identified_user_id: "", pageviews: 0 }];
+
vi.mock("../utils/analyticsQuery.js", () => ({
- runAnalyticsQuery: vi.fn(async ({ params }: { params: { userId: string } }) => {
+ runAnalyticsQuery: vi.fn(async ({ query, params }: { query: string; params: { userId: string } }) => {
state.queriedUserIds.push(params.userId);
- return state.rowsByUserId[params.userId] ?? [];
+ // Only the profile query is keyed to user rows here; the vitals, location
+ // and device breakdowns come back empty.
+ if (!query.includes("COUNT(DISTINCT session_id) AS sessions")) return [];
+ return state.rowsByUserId[params.userId] ?? emptyProfileResult(query);
}),
}));
diff --git a/server/src/api/analytics/users/getUserInfo.ts b/server/src/api/analytics/users/getUserInfo.ts
index 6f029e281..419a7796b 100644
--- a/server/src/api/analytics/users/getUserInfo.ts
+++ b/server/src/api/analytics/users/getUserInfo.ts
@@ -156,6 +156,9 @@ export const buildUserInfoQueries = (query: FilterParams, siteId: number) => {
argMaxIf(user_timezone, session_end, user_timezone != '') AS timezone
FROM
sessions
+ -- An aggregate without GROUP BY returns one all-default row for no input;
+ -- return none, so an unknown user 404s and a claimed device follows its alias.
+ HAVING count() > 0
`;
// p75 Web Vitals across every performance event this user produced.
diff --git a/server/src/api/analytics/users/getUserTraits.ts b/server/src/api/analytics/users/getUserTraits.ts
index a4fe8a027..f3baa33f8 100644
--- a/server/src/api/analytics/users/getUserTraits.ts
+++ b/server/src/api/analytics/users/getUserTraits.ts
@@ -1,8 +1,10 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { sql } from "drizzle-orm";
+import { z } from "zod";
import { db } from "../../../db/postgres/postgres.js";
import { analyticsRoute, runAnalyticsQuery } from "../utils/analyticsQuery.js";
import { effectiveUserId } from "../utils/effectiveUserId.js";
+import { pagingParam } from "./userScope.js";
export interface GetUserTraitKeysRequest {
Params: { siteId: string };
@@ -18,6 +20,28 @@ export interface GetUserTraitValueUsersRequest {
Querystring: { key: string; value: string; limit?: string; offset?: string };
}
+const traitPageSchema = z.object({ limit: pagingParam("limit", 1), offset: pagingParam("offset", 0) });
+
+/**
+ * A page of a public route's results: malformed params are an error, and the
+ * limit is capped (the users page also sends its ids to ClickHouse as one list).
+ */
+export const parseTraitPage = (
+ query: unknown,
+ defaultLimit: number,
+ maxLimit: number
+): { ok: true; limit: number; offset: number } | { ok: false; error: string } => {
+ const parsed = traitPageSchema.safeParse(query);
+ if (!parsed.success) {
+ return { ok: false, error: parsed.error.issues[0]?.message ?? "Invalid pagination parameters" };
+ }
+ return {
+ ok: true,
+ limit: Math.min(parsed.data.limit ?? defaultLimit, maxLimit),
+ offset: parsed.data.offset ?? 0,
+ };
+};
+
export const getUserTraitKeys = analyticsRoute<GetUserTraitKeysRequest>(
"user trait keys",
async (req: FastifyRequest<GetUserTraitKeysRequest>, res: FastifyReply) => {
@@ -45,14 +69,17 @@ export const getUserTraitValues = analyticsRoute<GetUserTraitValuesRequest>(
"user trait values",
async (req: FastifyRequest<GetUserTraitValuesRequest>, res: FastifyReply) => {
const siteId = Number(req.params.siteId);
- const { key, limit: limitStr = "1000", offset: offsetStr = "0" } = req.query;
+ const { key } = req.query;
if (!key) {
return res.status(400).send({ error: "key query parameter is required" });
}
- const limit = parseInt(limitStr, 10);
- const offset = parseInt(offsetStr, 10);
+ const page = parseTraitPage(req.query, 1000, 10_000);
+ if (!page.ok) {
+ return res.status(400).send({ error: page.error });
+ }
+ const { limit, offset } = page;
const [valuesResult, countResult] = await Promise.all([
db.execute<{ value: string; user_count: number }>(sql`
@@ -107,14 +134,17 @@ export const getUserTraitValueUsers = analyticsRoute<GetUserTraitValueUsersReque
"trait value users",
async (req: FastifyRequest<GetUserTraitValueUsersRequest>, res: FastifyReply) => {
const siteId = Number(req.params.siteId);
- const { key, value, limit: limitStr = "50", offset: offsetStr = "0" } = req.query;
+ const { key, value } = req.query;
if (!key || value === undefined) {
return res.status(400).send({ error: "key and value query parameters are required" });
}
- const limit = parseInt(limitStr, 10);
- const offset = parseInt(offsetStr, 10);
+ const page = parseTraitPage(req.query, 50, 1000);
+ if (!page.ok) {
+ return res.status(400).send({ error: page.error });
+ }
+ const { limit, offset } = page;
// Step 1: Find matching user IDs and their traits from Postgres
const [profilesResult, countResult] = await Promise.all([
diff --git a/server/src/api/analytics/users/getUsers.ts b/server/src/api/analytics/users/getUsers.ts
index a782a62ef..d7ba37b48 100644
--- a/server/src/api/analytics/users/getUsers.ts
+++ b/server/src/api/analytics/users/getUsers.ts
@@ -1,4 +1,5 @@
import { FastifyReply, FastifyRequest } from "fastify";
+import { z } from "zod";
import { enrichWithTraits } from "../utils/utils.js";
import { getTimeStatement } from "../utils/timeWindow.js";
import { FilterParams } from "@rybbit/shared";
@@ -8,6 +9,7 @@ import { analyticsRoute, runAnalyticsQuery } from "../utils/analyticsQuery.js";
import { effectiveUserId } from "../utils/effectiveUserId.js";
import {
buildUserNarrowing,
+ pagingParam,
resolveSearchScope,
resolveTraitCohort,
UserNarrowingParams,
@@ -48,6 +50,35 @@ export interface GetUsersRequest {
>;
}
+const DEFAULT_PAGE_SIZE = 100;
+// The dashboard asks for at most 100 rows; larger pages are capped, as the
+// sessions list caps its own.
+const MAX_PAGE_SIZE = 1000;
+// The offset is bound as Int32.
+const MAX_OFFSET = 2_147_483_647;
+
+const usersPaginationSchema = z.object({
+ page: pagingParam("page", 1),
+ page_size: pagingParam("page_size", 1),
+});
+
+/** The list's page and offset: a malformed value is an error, an oversized page is capped. */
+export const parseUsersPagination = (
+ query: unknown
+): { ok: true; page: number; pageSize: number; offset: number } | { ok: false; error: string } => {
+ const parsed = usersPaginationSchema.safeParse(query);
+ if (!parsed.success) {
+ return { ok: false, error: parsed.error.issues[0]?.message ?? "Invalid pagination parameters" };
+ }
+ const page = parsed.data.page ?? 1;
+ const pageSize = Math.min(parsed.data.page_size ?? DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE);
+ const offset = (page - 1) * pageSize;
+ if (offset > MAX_OFFSET) {
+ return { ok: false, error: "page is out of range" };
+ }
+ return { ok: true, page, pageSize, offset };
+};
+
export const buildUsersQuery = (
query: GetUsersRequest["Querystring"],
siteId: number,
@@ -188,11 +219,14 @@ export const getUsers = analyticsRoute<GetUsersRequest>(
return res.status(400).send({ error: parsed.error.issues[0]?.message ?? "Invalid query parameters" });
}
- const { page = "1", page_size: pageSize = "100" } = req.query;
+ const pagination = parseUsersPagination(req.query);
+ if (!pagination.ok) {
+ return res.status(400).send({ error: pagination.error });
+ }
+
const { trait_key: traitKey, trait_value: traitValue, trait_missing: traitMissing } = parsed.data;
const siteId = Number(req.params.siteId);
- const pageNum = parseInt(page, 10);
- const pageSizeNum = parseInt(pageSize, 10);
+ const { page: pageNum, pageSize: pageSizeNum, offset } = pagination;
if (traitKey && traitValue === undefined && traitMissing !== "true") {
return res.status(400).send({ error: "trait_key needs trait_value or trait_missing=true" });
@@ -235,7 +269,6 @@ export const getUsers = analyticsRoute<GetUsersRequest>(
}
const { params: narrowingParams } = buildUserNarrowing(req.query, scope);
- const offset = (pageNum - 1) * pageSizeNum;
// Execute both queries in parallel
const [data, countData] = await Promise.all([
diff --git a/server/src/api/analytics/users/userScope.ts b/server/src/api/analytics/users/userScope.ts
index 390ff69f8..dbec35c1e 100644
--- a/server/src/api/analytics/users/userScope.ts
+++ b/server/src/api/analytics/users/userScope.ts
@@ -34,6 +34,24 @@ export const MAX_SCOPED_USER_IDS = 10_000;
const flag = z.enum(["true", "false"]).optional();
+/**
+ * An untrusted paging param (page, limit, offset): absent or empty, or a whole
+ * number of at least `min`. A bare parseInt let a malformed value through as
+ * NaN, which failed in the database as a 500.
+ */
+export const pagingParam = (name: string, min: 0 | 1) => {
+ const message = `${name} must be ${min === 1 ? "a positive" : "a non-negative"} integer`;
+ return z.preprocess(
+ value => (value === "" ? undefined : value),
+ z.coerce
+ .number({ invalid_type_error: message })
+ .int(message)
+ .min(min, message)
+ .max(Number.MAX_SAFE_INTEGER, message)
+ .optional()
+ );
+};
+
/** The Users page's own query params, on top of the shared time and filter params. */
export const userNarrowingSchema = z.object({
identified_only: flag,
diff --git a/server/src/api/analytics/users/userTraitHandlers.test.ts b/server/src/api/analytics/users/userTraitHandlers.test.ts
index 9da7cea06..8bd7fcbb5 100644
--- a/server/src/api/analytics/users/userTraitHandlers.test.ts
+++ b/server/src/api/analytics/users/userTraitHandlers.test.ts
@@ -27,6 +27,7 @@ vi.mock("../../../db/postgres/postgres.js", async () => {
import { sql as pgClient } from "../../../db/postgres/postgres.js";
import { getUserTraitBreakdown } from "./getUserTraitBreakdown.js";
+import { getUserTraitValues } from "./getUserTraits.js";
import { getUsers } from "./getUsers.js";
import { MAX_SCOPED_USER_IDS } from "./userScope.js";
@@ -291,3 +292,61 @@ describe("getUsers search", () => {
expect(reply.body.searchLimited).toBe(true);
});
});
+
+describe("getUsers pagination", () => {
+ it.each([
+ ["page_size", "abc"],
+ ["page_size", "0"],
+ ["page", "-1"],
+ ["page", "1.5"],
+ ])("rejects %s=%s with a 400 instead of running the query", async (param, value) => {
+ const reply = replyStub();
+ await getUsers(request(SITE, { ...period, [param]: value }), reply);
+
+ expect(reply.statusCode).toBe(400);
+ expect(reply.body.error).toBe(`${param} must be a positive integer`);
+ expect(mocks.clickhouseQuery).not.toHaveBeenCalled();
+ });
+
+ it("rejects a page whose offset cannot be bound", async () => {
+ const reply = replyStub();
+ await getUsers(request(SITE, { ...period, page: "100000000", page_size: "100" }), reply);
+
+ expect(reply.statusCode).toBe(400);
+ expect(mocks.clickhouseQuery).not.toHaveBeenCalled();
+ });
+
+ it("caps an oversized page and reports the size it served", async () => {
+ const reply = replyStub();
+ await getUsers(request(SITE, { ...period, page: "3", page_size: "5000000" }), reply);
+
+ const list = calls().find(isList)!;
+ expect(list.query_params).toMatchObject({ limit: 1000, offset: 2000 });
+ expect(reply.body).toMatchObject({ page: 3, pageSize: 1000 });
+ });
+
+ it("treats empty params as the defaults", async () => {
+ const reply = replyStub();
+ await getUsers(request(SITE, { ...period, page: "", page_size: "" }), reply);
+
+ expect(calls().find(isList)!.query_params).toMatchObject({ limit: 100, offset: 0 });
+ });
+});
+
+describe("getUserTraitValues pagination", () => {
+ it("rejects a malformed limit with a 400", async () => {
+ const reply = replyStub();
+ await getUserTraitValues(request(SITE, { key: "plan", limit: "abc" }), reply);
+
+ expect(reply.statusCode).toBe(400);
+ expect(reply.body.error).toBe("limit must be a positive integer");
+ });
+
+ it("serves an oversized limit capped", async () => {
+ const reply = replyStub();
+ await getUserTraitValues(request(SITE, { key: "plan", limit: "99999999", offset: "0" }), reply);
+
+ expect(reply.statusCode).toBe(200);
+ expect(reply.body).toMatchObject({ total: 2, hasMore: false });
+ });
+});
diff --git a/server/src/api/analytics/utils/getFilterStatement.test.ts b/server/src/api/analytics/utils/getFilterStatement.test.ts
index 0d24d73e4..c3fdc383d 100644
--- a/server/src/api/analytics/utils/getFilterStatement.test.ts
+++ b/server/src/api/analytics/utils/getFilterStatement.test.ts
@@ -454,6 +454,35 @@ describe("getFilterStatement", () => {
expect(result).toBe("AND (url_parameters['utm_source'] IS NULL OR url_parameters['utm_source'] = '')");
});
+ it.each([
+ ["city", "city"],
+ ["browser_version", "browser_version"],
+ ["operating_system_version", "operating_system_version"],
+ ])("should test the stored %s column, not its never-empty display expression", (parameter, column) => {
+ const isNull = getFilterStatement(JSON.stringify([{ parameter, type: "is_null", value: [] }]));
+ const isNotNull = getFilterStatement(JSON.stringify([{ parameter, type: "is_not_null", value: [] }]));
+ expect(isNull).toBe(`AND (${column} IS NULL OR ${column} = '')`);
+ expect(isNotNull).toBe(`AND (${column} IS NOT NULL AND ${column} != '')`);
+ expect(isNull).not.toContain("concat");
+ });
+
+ it("should treat unknown dimensions as a 0x0 screen", () => {
+ const filters = JSON.stringify([{ parameter: "dimensions", type: "is_null", value: [] }]);
+ expect(getFilterStatement(filters)).toBe("AND (screen_width = 0 AND screen_height = 0)");
+ const notNull = JSON.stringify([{ parameter: "dimensions", type: "is_not_null", value: [] }]);
+ expect(getFilterStatement(notNull)).toBe("AND (screen_width != 0 OR screen_height != 0)");
+ });
+
+ it("should compare unknown coordinates with 0, not ''", () => {
+ // `lat = ''` is CANNOT_PARSE_NUMBER in ClickHouse.
+ expect(getFilterStatement(JSON.stringify([{ parameter: "lat", type: "is_null", value: [] }]))).toBe(
+ "AND lat = 0"
+ );
+ expect(getFilterStatement(JSON.stringify([{ parameter: "lon", type: "is_not_null", value: [] }]))).toBe(
+ "AND lon != 0"
+ );
+ });
+
it("should handle is_not_null inside a session-level subquery", () => {
const filters = JSON.stringify([{ parameter: "event_name", type: "is_not_null", value: [] }]);
const result = getFilterStatement(filters);
@@ -514,11 +543,28 @@ describe("getFilterStatement", () => {
expect(getFilterStatement(filters)).toBe("AND lat >= 10");
});
- it("should coerce numeric comparisons on non-numeric parameters", () => {
- // Pinned behavior: any parameter accepts numeric comparison types; the
- // value is coerced with Number() and inlined unquoted.
+ it("should reject numeric comparisons on non-numeric parameters", () => {
+ // `pathname >= 10` compares a String with a UInt8, which ClickHouse
+ // rejects (NO_COMMON_TYPE) β€” a 500 instead of a 400.
const filters = JSON.stringify([{ parameter: "pathname", type: "greater_than_or_equal", value: ["10"] }]);
- expect(getFilterStatement(filters)).toBe("AND pathname >= 10");
+ expect(() => getFilterStatement(filters)).toThrow(
+ 'greater_than_or_equal filters only apply to lat and lon, not "pathname"'
+ );
+ });
+
+ it("should reject numeric comparisons inside session-level subqueries too", () => {
+ const filters = JSON.stringify([{ parameter: "event_name", type: "less_than", value: ["5"] }]);
+ expect(() => getFilterStatement(filters)).toThrow("only apply to lat and lon");
+ });
+
+ it("should reject string operators on numeric parameters", () => {
+ const filters = JSON.stringify([{ parameter: "lat", type: "contains", value: ["40"] }]);
+ expect(() => getFilterStatement(filters)).toThrow('contains filters do not apply to the numeric parameter "lat"');
+ });
+
+ it("should reject non-numeric values for lat/lon equality", () => {
+ const filters = JSON.stringify([{ parameter: "lon", type: "equals", value: ["east"] }]);
+ expect(() => getFilterStatement(filters)).toThrow("Invalid numeric value for equals filter: east");
});
it("should throw for non-numeric value with greater_than_or_equal", () => {
@@ -694,9 +740,12 @@ describe("filterParamError", () => {
expect(
filterParamError(JSON.stringify([{ parameter: "pathname", type: "regex", value: ["[invalid"] }]))
).toBeTruthy();
- expect(filterParamError(JSON.stringify([{ parameter: "pathname", type: "greater_than", value: ["abc"] }]))).toMatch(
+ expect(filterParamError(JSON.stringify([{ parameter: "lat", type: "greater_than", value: ["abc"] }]))).toMatch(
/Invalid numeric value/
);
+ expect(filterParamError(JSON.stringify([{ parameter: "browser", type: "greater_than", value: ["5"] }]))).toBe(
+ 'greater_than filters only apply to lat and lon, not "browser"'
+ );
expect(filterParamError(["array"])).toBe("filters must be a JSON array");
expect(filterParamError(JSON.stringify([{ parameter: "pathname", type: "equals", value: [] }]))).toMatch(
/needs at least one value/
diff --git a/server/src/api/analytics/utils/getFilterStatement.ts b/server/src/api/analytics/utils/getFilterStatement.ts
index 80bcb4226..ce84f9e75 100644
--- a/server/src/api/analytics/utils/getFilterStatement.ts
+++ b/server/src/api/analytics/utils/getFilterStatement.ts
@@ -196,6 +196,73 @@ export const getSqlParam = (parameter: FilterParameter) => {
return filterParamSchema.parse(parameter);
};
+// Parameters stored as numbers. Only these take numeric comparisons, and they
+// take no string operators: either mismatch renders SQL ClickHouse rejects
+// (`browser > 5`, `lat LIKE '%4%'`).
+const NUMERIC_PARAMS: ReadonlySet<FilterParameter> = new Set<FilterParameter>(["lat", "lon"]);
+const NUMERIC_COMPARISON_TYPES: ReadonlySet<FilterType> = new Set<FilterType>([
+ "greater_than",
+ "less_than",
+ "greater_than_or_equal",
+ "less_than_or_equal",
+]);
+const STRING_ONLY_TYPES: ReadonlySet<FilterType> = new Set<FilterType>([
+ "contains",
+ "not_contains",
+ "starts_with",
+ "ends_with",
+ "regex",
+ "not_regex",
+]);
+
+/** Throws when a filter's operator or values cannot apply to its parameter's type. */
+const assertFilterTypeCompatible = (
+ parameter: FilterParameter,
+ filterType: FilterType,
+ values: (string | number)[]
+) => {
+ const isNumericParam = NUMERIC_PARAMS.has(parameter);
+ if (NUMERIC_COMPARISON_TYPES.has(filterType) && !isNumericParam) {
+ throw new Error(`${filterType} filters only apply to lat and lon, not "${parameter}"`);
+ }
+ if (STRING_ONLY_TYPES.has(filterType) && isNumericParam) {
+ throw new Error(`${filterType} filters do not apply to the numeric parameter "${parameter}"`);
+ }
+ if (isNumericParam && filterType !== "is_null" && filterType !== "is_not_null") {
+ const invalid = values.find(value => !Number.isFinite(Number(value)));
+ if (invalid !== undefined) {
+ throw new Error(`Invalid numeric value for ${filterType} filter: ${invalid}`);
+ }
+ }
+};
+
+// Several filter expressions are built from more than one column β€” city is
+// `region-city`, browser_version `browser version`, dimensions `WxH` β€” so they
+// are never empty even when the value is unknown, and lat/lon are numbers that
+// cannot be compared with ''. Null checks test the stored column instead.
+const NULL_CHECK_COLUMNS: Partial<Record<FilterParameter, string>> = {
+ city: "city",
+ browser_version: "browser_version",
+ operating_system_version: "operating_system_version",
+};
+
+const buildNullCheckCondition = (
+ parameter: FilterParameter,
+ filterType: "is_null" | "is_not_null",
+ mapField: (expression: string) => string
+): string => {
+ const isNull = filterType === "is_null";
+ if (NUMERIC_PARAMS.has(parameter)) {
+ // Unknown coordinates are stored as 0.
+ const column = mapField(getSqlParam(parameter));
+ return isNull ? `${column} = 0` : `${column} != 0`;
+ }
+ if (parameter === "dimensions") {
+ return isNull ? "(screen_width = 0 AND screen_height = 0)" : "(screen_width != 0 OR screen_height != 0)";
+ }
+ return buildStringFilterCondition(mapField(NULL_CHECK_COLUMNS[parameter] ?? getSqlParam(parameter)), filterType, []);
+};
+
/**
* Returns why a request's `filters` param can't be used, or null when it is
* valid or absent. Builds the statement once so regex and numeric checks run
@@ -275,7 +342,10 @@ export function getFilterStatement(
is_null: "is_not_null",
};
const positiveFilterType = negatedSessionFilterTypes[filterType] ?? filterType;
- const condition = buildStringFilterCondition(getSqlParam(param), positiveFilterType, values);
+ const condition =
+ positiveFilterType === "is_null" || positiveFilterType === "is_not_null"
+ ? buildNullCheckCondition(param, positiveFilterType, expression => expression)
+ : buildStringFilterCondition(getSqlParam(param), positiveFilterType, values);
const membershipOperator = negatedSessionFilterTypes[filterType] ? "NOT IN" : "IN";
const finalWhere = whereClause ? `WHERE ${whereClause} AND ${condition}` : `WHERE ${condition}`;
@@ -310,8 +380,9 @@ export function getFilterStatement(
"AND " +
filtersArray
.map(filter => {
- const isNumericParam = filter.parameter === "lat" || filter.parameter === "lon";
- const isNullCheck = filter.type === "is_null" || filter.type === "is_not_null";
+ const isNumericParam = NUMERIC_PARAMS.has(filter.parameter);
+
+ assertFilterTypeCompatible(filter.parameter, filter.type, filter.value);
// Handle session-level filters (configurable via options).
// Most parameters match sessions containing an event; channel uses the session's first attributed value.
@@ -395,8 +466,8 @@ export function getFilterStatement(
}
}
- if (isNullCheck) {
- return buildStringFilterCondition(mapField(getSqlParam(filter.parameter)), filter.type, filter.value);
+ if (filter.type === "is_null" || filter.type === "is_not_null") {
+ return buildNullCheckCondition(filter.parameter, filter.type, mapField);
}
if (filter.type === "regex" || filter.type === "not_regex") {
diff --git a/server/src/api/experiments/getExperimentResults.test.ts b/server/src/api/experiments/getExperimentResults.test.ts
index be512a87c..21c77d278 100644
--- a/server/src/api/experiments/getExperimentResults.test.ts
+++ b/server/src/api/experiments/getExperimentResults.test.ts
@@ -8,6 +8,7 @@ vi.mock("../../db/postgres/postgres.js", () => ({
}));
import { buildExperimentResultQueries } from "./getExperimentResults.js";
+import { buildExperimentResults } from "./utils.js";
const CAMPAIGN_FILTER = JSON.stringify([{ parameter: "utm_campaign", type: "equals", value: ["recipe_book_2026"] }]);
@@ -23,6 +24,7 @@ const buildQueries = () =>
query,
siteId: 1,
flagKey: "recipe_book_test",
+ variantKeys: ["control", "treatment"],
goalCondition: "type = 'form_submit' AND JSONExtractString(toString(props), 'formId') = 'gform_115'",
});
@@ -48,7 +50,7 @@ describe("experiment result queries", () => {
expect(assignmentQuery).toContain("FilteredSessions AS");
expect(assignmentQuery).toContain("WHERE 1 = 1 AND utm_campaign = 'recipe_book_2026'");
expect(assignmentQuery.match(/INNER JOIN FilteredSessions USING \(session_id\)/g)).toHaveLength(2);
- expect(assignmentQuery).toContain("feature_flags['recipe_book_test'] != ''");
+ expect(assignmentQuery).toContain("feature_flags['recipe_book_test'] IN ('control', 'treatment')");
expect(assignmentQuery).toContain("type = 'form_submit'");
expect(assignmentQuery.match(/utm_campaign = 'recipe_book_2026'/g)).toHaveLength(1);
});
@@ -67,3 +69,33 @@ describe("experiment result queries", () => {
expect(assignmentQuery).not.toContain("GROUP BY session_id, variant");
});
});
+
+describe("experiment arms", () => {
+ it('only counts sessions assigned a configured variant, not the out-of-rollout "false"', () => {
+ const { exposureQuery, assignmentQuery } = buildQueries();
+
+ expect(exposureQuery).toContain("JSONExtractString(toString(props), 'value') IN ('control', 'treatment')");
+ expect(assignmentQuery).toContain("feature_flags['recipe_book_test'] IN ('control', 'treatment')");
+ });
+
+ it("only counts exposures assigned as a variant, so a variant keyed false can't absorb other sessions", () => {
+ const { exposureQuery } = buildQueries();
+
+ expect(exposureQuery).toContain("JSONExtractString(toString(props), 'reason') IN ('variant', '')");
+ });
+
+ it("reports one result per configured variant and drops rows for any other value", () => {
+ const results = buildExperimentResults(
+ ["control", "treatment"],
+ [
+ { variant: "control", sessions: 100, exposures: 120, conversions: 10 },
+ { variant: "false", sessions: 900, exposures: 900, conversions: 300 },
+ { variant: "treatment", sessions: 100, exposures: 110, conversions: 20 },
+ ]
+ );
+
+ expect(results.map(result => result.variant)).toEqual(["control", "treatment"]);
+ expect(results.reduce((sum, result) => sum + result.sessions, 0)).toBe(200);
+ expect(results[1]).toMatchObject({ conversionRate: 0.2, lift: 1, isControl: false });
+ });
+});
diff --git a/server/src/api/experiments/getExperimentResults.ts b/server/src/api/experiments/getExperimentResults.ts
index 22509902f..09babd54b 100644
--- a/server/src/api/experiments/getExperimentResults.ts
+++ b/server/src/api/experiments/getExperimentResults.ts
@@ -21,6 +21,8 @@ type BuildExperimentResultQueriesParams = {
query: FilterParams;
siteId: number;
flagKey: string;
+ /** The flag's configured variant keys; must not be empty. */
+ variantKeys: string[];
goalCondition: string;
};
@@ -28,6 +30,7 @@ export function buildExperimentResultQueries({
query,
siteId,
flagKey,
+ variantKeys,
goalCondition,
}: BuildExperimentResultQueriesParams) {
const timeStatement = getTimeStatement(query);
@@ -36,6 +39,10 @@ export function buildExperimentResultQueries({
const filteredSessionsPrefix = filteredSessionsCte ? `${filteredSessionsCte},` : "";
const escapedSiteId = SqlString.escape(siteId);
const escapedFlagKey = SqlString.escape(flagKey);
+ // Sessions outside the rollout carry the flag as "false"; they are not an experiment arm.
+ // Exposures also say why the value was assigned, which tells an arm apart from that "false"
+ // even for a variant named "false" (no longer allowed, but older flags may have one).
+ const escapedVariantKeys = variantKeys.map(key => SqlString.escape(key)).join(", ");
// A session qualifies once, independently of which event carried the filter
// value. Goal, exposure, and assignment rows are then scoped to that cohort.
@@ -69,7 +76,8 @@ export function buildExperimentResultQueries({
AND type = 'custom_event'
AND event_name = 'feature_flag_exposure'
AND JSONExtractString(toString(props), 'key') = ${escapedFlagKey}
- AND JSONExtractString(toString(props), 'value') != ''
+ AND JSONExtractString(toString(props), 'value') IN (${escapedVariantKeys})
+ AND JSONExtractString(toString(props), 'reason') IN ('variant', '')
${timeStatement}
GROUP BY session_id
),
@@ -98,7 +106,7 @@ export function buildExperimentResultQueries({
FROM events
${filteredSessionsJoin}
WHERE site_id = ${escapedSiteId}
- AND feature_flags[${escapedFlagKey}] != ''
+ AND feature_flags[${escapedFlagKey}] IN (${escapedVariantKeys})
${timeStatement}
GROUP BY session_id
),
@@ -139,14 +147,14 @@ export async function getExperimentResults(
const variants = getExperimentVariantKeys(record.featureFlag);
const goalCondition = record.primaryGoal ? buildGoalCondition(record.primaryGoal) : null;
- if (!record.primaryGoal || !goalCondition) {
+ if (!record.primaryGoal || !goalCondition || variants.length === 0) {
return reply.send({
data: {
experiment: serializeExperiment(record),
variants: buildExperimentResults(variants, []),
totalExposureSessions: 0,
totalConversions: 0,
- hasGoal: false,
+ hasGoal: !!(record.primaryGoal && goalCondition),
measurement: "exposure",
},
});
@@ -156,6 +164,7 @@ export async function getExperimentResults(
query: request.query,
siteId,
flagKey: record.featureFlag.key,
+ variantKeys: variants,
goalCondition,
});
@@ -167,8 +176,12 @@ export async function getExperimentResults(
// for this key), count sessions that were assigned the variant via the
// feature_flags map attached to every event. Looser, but avoids a confusing
// empty result when the flag is clearly assigning traffic.
+ // The assignment map can't tell a variant named "true"/"false" (older flags may have one)
+ // from the flag being on or off for a visitor outside the rollout, so such flags only
+ // report exposures.
const hasExposures = rows.some(row => Number(row.sessions) > 0);
- if (!hasExposures) {
+ const hasReservedVariant = variants.some(variant => variant === "true" || variant === "false");
+ if (!hasExposures && !hasReservedVariant) {
const assignmentResult = await clickhouse.query({ query: assignmentQuery, format: "JSONEachRow" });
const assignmentRows = await processResults<ExperimentResultRow>(assignmentResult);
if (assignmentRows.some(row => Number(row.sessions) > 0)) {
diff --git a/server/src/api/experiments/utils.ts b/server/src/api/experiments/utils.ts
index f2968bd34..609fb5bcb 100644
--- a/server/src/api/experiments/utils.ts
+++ b/server/src/api/experiments/utils.ts
@@ -118,21 +118,20 @@ function getControlVariant(variants: string[], rows: ExperimentResultRow[]) {
return variants.find(variant => variant === "control") || variants[0] || rows[0]?.variant || null;
}
+/**
+ * One result per configured variant. Rows for any other value (the "false" recorded for
+ * sessions outside the rollout, or a variant since removed from the flag) are not arms of the
+ * experiment and are left out.
+ */
export function buildExperimentResults(variants: string[], rows: ExperimentResultRow[]) {
- const resultMap = new Map(rows.map(row => [row.variant, row]));
- const allVariants = [...variants];
+ const armRows = rows.filter(row => variants.includes(row.variant));
+ const resultMap = new Map(armRows.map(row => [row.variant, row]));
- for (const row of rows) {
- if (!allVariants.includes(row.variant)) {
- allVariants.push(row.variant);
- }
- }
-
- const controlVariant = getControlVariant(allVariants, rows);
+ const controlVariant = getControlVariant(variants, armRows);
const controlRow = controlVariant ? resultMap.get(controlVariant) : undefined;
const controlRate = controlRow && controlRow.sessions > 0 ? controlRow.conversions / controlRow.sessions : null;
- return allVariants.map(variant => {
+ return variants.map(variant => {
const row = resultMap.get(variant);
const sessions = row?.sessions ?? 0;
const exposures = row?.exposures ?? 0;
diff --git a/server/src/api/featureFlags/index.ts b/server/src/api/featureFlags/index.ts
index efc48a43a..e918ce8ed 100644
--- a/server/src/api/featureFlags/index.ts
+++ b/server/src/api/featureFlags/index.ts
@@ -20,6 +20,7 @@ import {
evaluateFeatureFlagsSchema,
featureFlagBodySchema,
featureFlagUpdateSchema,
+ validateFeatureFlagUpdate,
type EvaluateFeatureFlagsBody,
type FeatureFlagBody,
type FeatureFlagUpdateBody,
@@ -200,23 +201,28 @@ export async function updateFeatureFlag(
if (body.rules === undefined) delete updateData.rules;
if (body.conditionSets === undefined) delete updateData.conditionSets;
- updateData.version = (
- await db.query.featureFlags.findFirst({
- columns: { version: true },
- where: and(eq(featureFlags.siteId, siteId), eq(featureFlags.flagId, flagId)),
- })
- )?.version;
-
- if (updateData.version === undefined) {
- return reply.status(404).send({ error: "Feature flag not found" });
- }
- updateData.version += 1;
-
- const [updated] = await db
- .update(featureFlags)
- .set(updateData)
- .where(and(eq(featureFlags.siteId, siteId), eq(featureFlags.flagId, flagId)))
- .returning();
+ const flagWhere = and(eq(featureFlags.siteId, siteId), eq(featureFlags.flagId, flagId));
+ // Lock the row so the patch is validated against the flag it actually updates.
+ const updated = await db.transaction(async tx => {
+ const [stored] = await tx
+ .select({
+ version: featureFlags.version,
+ flagType: featureFlags.flagType,
+ variants: featureFlags.variants,
+ conditionSets: featureFlags.conditionSets,
+ })
+ .from(featureFlags)
+ .where(flagWhere)
+ .for("update");
+
+ if (!stored) return null;
+
+ validateFeatureFlagUpdate(stored, body);
+ updateData.version = stored.version + 1;
+
+ const [row] = await tx.update(featureFlags).set(updateData).where(flagWhere).returning();
+ return row ?? null;
+ });
if (!updated) {
return reply.status(404).send({ error: "Feature flag not found" });
diff --git a/server/src/api/featureFlags/schemas.test.ts b/server/src/api/featureFlags/schemas.test.ts
index b75763457..f48e3bff9 100644
--- a/server/src/api/featureFlags/schemas.test.ts
+++ b/server/src/api/featureFlags/schemas.test.ts
@@ -1,5 +1,10 @@
import { describe, expect, it } from "vitest";
-import { featureFlagBodySchema } from "./schemas.js";
+import {
+ featureFlagBodySchema,
+ featureFlagUpdateSchema,
+ validateFeatureFlagUpdate,
+ type FeatureFlagShape,
+} from "./schemas.js";
const baseFlag = {
key: "new_checkout",
@@ -27,4 +32,74 @@ describe("feature flag schemas", () => {
expect(result.success).toBe(false);
});
+
+ // The tracker records a flag that is off or on as "false"/"true", which such a variant would collide with.
+ it.each(["true", "false"])("rejects the reserved variant key %s", key => {
+ const multivariate = { ...baseFlag, flagType: "multivariate" };
+ const control = { key: "control", rolloutPercentage: 50 };
+ const variants = [control, { key, rolloutPercentage: 50 }];
+
+ expect(featureFlagBodySchema.safeParse({ ...multivariate, variants }).success).toBe(false);
+ expect(featureFlagBodySchema.safeParse({ ...multivariate, conditionSets: [{ variants }] }).success).toBe(false);
+ expect(featureFlagUpdateSchema.safeParse({ variants }).success).toBe(false);
+
+ const capitalized = [control, { key: key.toUpperCase(), rolloutPercentage: 50 }];
+ expect(featureFlagBodySchema.safeParse({ ...multivariate, variants: capitalized }).success).toBe(true);
+ });
+});
+
+describe("feature flag partial updates", () => {
+ const storedMultivariate: Required<FeatureFlagShape> = {
+ flagType: "multivariate",
+ variants: [
+ { key: "control", rolloutPercentage: 50 },
+ { key: "treatment", rolloutPercentage: 50 },
+ ],
+ conditionSets: [],
+ };
+
+ const validate =
+ (patch: unknown, stored = storedMultivariate) =>
+ () =>
+ validateFeatureFlagUpdate(stored, featureFlagUpdateSchema.parse(patch));
+
+ it("rejects duplicate variant keys or rollouts over 100% when flagType is omitted", () => {
+ expect(
+ validate({
+ variants: [
+ { key: "control", rolloutPercentage: 60 },
+ { key: "control", rolloutPercentage: 60 },
+ ],
+ })
+ ).toThrow(/Variant keys must be unique[\s\S]*cannot exceed 100/);
+ });
+
+ it("rejects a single variant on a stored multivariate flag", () => {
+ expect(validate({ variants: [{ key: "control", rolloutPercentage: 100 }] })).toThrow(
+ "Multivariate flags require at least two variants"
+ );
+ });
+
+ it("rejects a type change that would keep the stored variants", () => {
+ expect(validate({ flagType: "boolean" })).toThrow("Boolean flags cannot have variants");
+ expect(() => validateFeatureFlagUpdate(storedMultivariate, { flagType: "boolean", variants: [] })).not.toThrow();
+ });
+
+ it("still saves other changes to a stored flag that has a variant keyed false", () => {
+ const storedLegacy: Required<FeatureFlagShape> = {
+ flagType: "multivariate",
+ variants: [
+ { key: "control", rolloutPercentage: 50 },
+ { key: "false", rolloutPercentage: 50 },
+ ],
+ conditionSets: [],
+ };
+ expect(validate({ enabled: false, description: "paused" }, storedLegacy)).not.toThrow();
+ });
+
+ it("accepts patches whose merged flag is valid", () => {
+ const storedBoolean: Required<FeatureFlagShape> = { flagType: "boolean", variants: [], conditionSets: [] };
+ expect(validate({ flagType: "multivariate" })).not.toThrow();
+ expect(validate({ enabled: true }, storedBoolean)).not.toThrow();
+ });
});
diff --git a/server/src/api/featureFlags/schemas.ts b/server/src/api/featureFlags/schemas.ts
index 650ec0a56..7d79c7351 100644
--- a/server/src/api/featureFlags/schemas.ts
+++ b/server/src/api/featureFlags/schemas.ts
@@ -100,7 +100,10 @@ const featureFlagVariantSchema = z.object({
.regex(
/^[A-Za-z][A-Za-z0-9_.:-]*$/,
"Variant key must start with a letter and contain only letters, numbers, _, ., :, or -"
- ),
+ )
+ // The tracker records a flag that is off for a visitor as "false" (and "true" for on), so a
+ // variant with either key could not be told apart from those in experiment results.
+ .refine(key => key !== "true" && key !== "false", 'Variant key cannot be "true" or "false"'),
name: z.string().trim().max(120).optional(),
rolloutPercentage: z.number().int().min(0).max(100),
payload: payloadValueSchema.optional(),
@@ -142,11 +145,11 @@ const featureFlagUpdateBaseSchema = z.object({
export const featureFlagBodySchema = featureFlagBaseSchema.superRefine(validateFeatureFlagShape);
-export const featureFlagUpdateSchema = featureFlagUpdateBaseSchema
- .refine(data => Object.keys(data).length > 0, {
- message: "At least one field must be provided",
- })
- .superRefine(validateFeatureFlagShape);
+// Shape checks need the whole flag (a patch may omit flagType or variants), so updates
+// run them on the stored flag with the patch applied: validateFeatureFlagUpdate.
+export const featureFlagUpdateSchema = featureFlagUpdateBaseSchema.refine(data => Object.keys(data).length > 0, {
+ message: "At least one field must be provided",
+});
export const evaluateFeatureFlagsSchema = z.object({
anonymousId: z.string().trim().min(1).max(128),
@@ -165,10 +168,31 @@ export type FeatureFlagBody = z.infer<typeof featureFlagBodySchema>;
export type FeatureFlagUpdateBody = z.infer<typeof featureFlagUpdateSchema>;
export type EvaluateFeatureFlagsBody = z.infer<typeof evaluateFeatureFlagsSchema>;
-function validateFeatureFlagShape(
- data: z.infer<typeof featureFlagBaseSchema> | z.infer<typeof featureFlagUpdateBaseSchema>,
- ctx: z.RefinementCtx
-) {
+type FeatureFlagVariantShape = { key: string; rolloutPercentage: number };
+
+/** The fields the cross-field shape checks read. */
+export type FeatureFlagShape = {
+ flagType?: z.infer<typeof featureFlagTypeSchema>;
+ variants?: FeatureFlagVariantShape[];
+ conditionSets?: Array<{ variants?: FeatureFlagVariantShape[]; rolloutPercentage?: number }>;
+};
+
+const featureFlagShapeSchema = z.custom<FeatureFlagShape>().superRefine(validateFeatureFlagShape);
+
+/**
+ * Validates a partial update against the flag it will produce: the stored flag with the patch
+ * applied. Throws a ZodError (like the body schemas) when the result is not a valid flag, e.g.
+ * duplicate variant keys, variants over 100%, or a type change that leaves stale variants.
+ */
+export function validateFeatureFlagUpdate(stored: Required<FeatureFlagShape>, patch: FeatureFlagUpdateBody): void {
+ featureFlagShapeSchema.parse({
+ flagType: patch.flagType ?? stored.flagType,
+ variants: patch.variants ?? stored.variants,
+ conditionSets: patch.conditionSets ?? stored.conditionSets,
+ });
+}
+
+function validateFeatureFlagShape(data: FeatureFlagShape, ctx: z.RefinementCtx) {
const variants = data.variants ?? [];
const conditionSets = data.conditionSets ?? [];
const flagType = data.flagType;
diff --git a/server/src/api/sessionReplay/getSessionReplays.ts b/server/src/api/sessionReplay/getSessionReplays.ts
index 3fe1abc8f..7692d3f3e 100644
--- a/server/src/api/sessionReplay/getSessionReplays.ts
+++ b/server/src/api/sessionReplay/getSessionReplays.ts
@@ -27,6 +27,9 @@ export async function getSessionReplays(
minDuration: minDuration ? Number(minDuration) : undefined,
start_date: request.query.start_date,
end_date: request.query.end_date,
+ // A zoomed chart sends only these; dropping them answered with all time.
+ start_datetime: request.query.start_datetime,
+ end_datetime: request.query.end_datetime,
time_zone: request.query.time_zone,
past_minutes_start: request.query.past_minutes_start ? Number(request.query.past_minutes_start) : undefined,
past_minutes_end: request.query.past_minutes_end ? Number(request.query.past_minutes_end) : undefined,
diff --git a/server/src/api/sessionReplay/recordSessionReplay.test.ts b/server/src/api/sessionReplay/recordSessionReplay.test.ts
index fa5effbb6..30cc729d3 100644
--- a/server/src/api/sessionReplay/recordSessionReplay.test.ts
+++ b/server/src/api/sessionReplay/recordSessionReplay.test.ts
@@ -128,15 +128,36 @@ describe("recordSessionReplay exclusions", () => {
await recordSessionReplay(createRequest(), reply);
expect(mocks.decideSiteExclusion).toHaveBeenCalledOnce();
- expect(mocks.recordEvents).toHaveBeenCalledWith(42, baseBody, {
- userAgent: "Mozilla/5.0 HeadlessChrome/120",
- ipAddress: "198.51.100.10",
- origin: "https://example.com",
- referrer: "https://example.com/admin/users",
- });
+ expect(mocks.recordEvents).toHaveBeenCalledWith(
+ 42,
+ baseBody,
+ expect.objectContaining({
+ userAgent: "Mozilla/5.0 HeadlessChrome/120",
+ ipAddress: "198.51.100.10",
+ origin: "https://example.com",
+ referrer: "https://example.com/admin/users",
+ anonymousId: undefined,
+ receivedAt: expect.any(Date),
+ })
+ );
expect(reply.sentPayload).toEqual({ success: true });
});
+ it.each(["anonymousId", "anonymous_id"])("hands ingestion the persistent client id sent as %s", async field => {
+ const reply = createReply();
+
+ await recordSessionReplay(
+ createRequest({ body: { ...baseBody, [field]: "client-123" } as RecordSessionReplayRequest }),
+ reply
+ );
+
+ expect(mocks.recordEvents).toHaveBeenCalledWith(
+ 42,
+ baseBody,
+ expect.objectContaining({ anonymousId: "client-123" })
+ );
+ });
+
it("tells the script to stop recording once the session is over its size cap", async () => {
mocks.recordEvents.mockResolvedValue({ capped: true });
@@ -150,6 +171,15 @@ describe("recordSessionReplay exclusions", () => {
});
});
+ it("asks the script for a full snapshot when the session needs one", async () => {
+ mocks.recordEvents.mockResolvedValue({ capped: false, needsFullSnapshot: true });
+
+ const reply = createReply();
+ await recordSessionReplay(createRequest(), reply);
+
+ expect(reply.sentPayload).toEqual({ success: true, takeFullSnapshot: true });
+ });
+
it("maps an IP Site Exclusion Decision to the replay skip response", async () => {
mocks.getConfig.mockResolvedValue({
...baseConfig,
@@ -272,3 +302,64 @@ describe("recordSessionReplay exclusions", () => {
});
});
});
+
+describe("recordSessionReplay bot detection", () => {
+ const browserHeaders = {
+ "user-agent":
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36",
+ accept: "*/*",
+ "accept-language": "en-US,en;q=0.9",
+ "accept-encoding": "gzip, deflate, br",
+ "sec-fetch-site": "cross-site",
+ "sec-fetch-mode": "cors",
+ };
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.decideSiteExclusion.mockResolvedValue({ excluded: false });
+ mocks.isSiteOverLimit.mockReturnValue(false);
+ mocks.isSiteWithoutReplay.mockReturnValue(false);
+ mocks.recordEvents.mockResolvedValue({ capped: false });
+ });
+
+ it("does not store a detected bot's replay when the Site blocks bots", async () => {
+ mocks.getConfig.mockResolvedValue({ ...baseConfig, blockBots: true });
+
+ const reply = createReply();
+ await recordSessionReplay(createRequest(), reply);
+
+ expect(mocks.recordEvents).not.toHaveBeenCalled();
+ expect(reply.sentPayload).toEqual({ success: true, message: "Session replay not recorded - bot detected" });
+ });
+
+ it("convicts on the strong client signals the tracker sends with the batch", async () => {
+ mocks.getConfig.mockResolvedValue({ ...baseConfig, blockBots: true });
+
+ const reply = createReply();
+ await recordSessionReplay(
+ createRequest({ headers: browserHeaders, body: { ...baseBody, _bsm: 1 } as RecordSessionReplayRequest }),
+ reply
+ );
+
+ expect(mocks.recordEvents).not.toHaveBeenCalled();
+ });
+
+ it("stores a detected bot's replay when the Site does not block bots", async () => {
+ mocks.getConfig.mockResolvedValue({ ...baseConfig, blockBots: false });
+
+ const reply = createReply();
+ await recordSessionReplay(createRequest(), reply);
+
+ expect(mocks.recordEvents).toHaveBeenCalledOnce();
+ });
+
+ it("stores a browser's replay when the Site blocks bots", async () => {
+ mocks.getConfig.mockResolvedValue({ ...baseConfig, blockBots: true });
+
+ const reply = createReply();
+ await recordSessionReplay(createRequest({ headers: browserHeaders }), reply);
+
+ expect(mocks.recordEvents).toHaveBeenCalledOnce();
+ expect(reply.sentPayload).toEqual({ success: true });
+ });
+});
diff --git a/server/src/api/sessionReplay/recordSessionReplay.ts b/server/src/api/sessionReplay/recordSessionReplay.ts
index ab0c5de9d..338728e43 100644
--- a/server/src/api/sessionReplay/recordSessionReplay.ts
+++ b/server/src/api/sessionReplay/recordSessionReplay.ts
@@ -1,14 +1,25 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { z } from "zod";
+import { createAsnLookup } from "../../db/geolocation/asn.js";
import { siteConfig } from "../../lib/siteConfig.js";
+import { isBlockedReplayBot } from "../../services/replay/replayBotDetection.js";
import { SessionReplayIngestService } from "../../services/replay/sessionReplayIngestService.js";
import { usageService } from "../../services/usageService.js";
import { RecordSessionReplayRequest } from "../../types/sessionReplay.js";
import { collectCandidateClientIps, resolveClientIp } from "../../services/tracker/resolveClientIp.js";
import { decideSiteExclusion } from "../../services/sites/siteExclusionDecision.js";
+// Bounds match `anonymous_id` on /track. @rybbit/js sends the camelCase
+// spelling, like the rest of this body; the snake_case one is /track's.
+const anonymousIdSchema = z.string().min(1).max(255).optional();
+
const recordSessionReplaySchema = z.object({
userId: z.string(),
+ anonymousId: anonymousIdSchema,
+ anonymous_id: anonymousIdSchema,
+ // Only the strong bits can convict, so unknown bits are ignored rather than
+ // costing the batch.
+ _bsm: z.number().int().nonnegative().optional(),
events: z.array(
z.object({
type: z.union([z.string(), z.number()]),
@@ -61,6 +72,10 @@ export async function recordSessionReplay(
}>,
reply: FastifyReply
) {
+ // Stamped before the first await, as on /track: it picks a salted
+ // fingerprint's day.
+ const receivedAt = new Date();
+
try {
// Get the site configuration to get the numeric siteId
const siteConfiguration = await siteConfig.getConfig(request.params.siteId);
@@ -92,9 +107,16 @@ export async function recordSessionReplay(
return reply.status(200).send({ success: true, message: "Session replay not available for plan or quota" });
}
- const body = recordSessionReplaySchema.parse(request.body) as RecordSessionReplayRequest;
+ const {
+ anonymousId,
+ anonymous_id,
+ _bsm: clientBotSignalMask,
+ ...parsedBody
+ } = recordSessionReplaySchema.parse(request.body);
+ const body = parsedBody as RecordSessionReplayRequest;
- const requestIP = resolveClientIp(request, { firstPartyProxy: siteConfiguration.firstPartyProxy });
+ const lookupAsn = createAsnLookup();
+ const requestIP = resolveClientIp(request, { firstPartyProxy: siteConfiguration.firstPartyProxy, lookupAsn });
const { hostname, pathname, querystring } = parseReplayPageUrl(body.metadata?.pageUrl);
const userAgent = request.headers["user-agent"] || "";
@@ -118,15 +140,38 @@ export async function recordSessionReplay(
});
}
+ // Event ingestion keeps a detected bot out of `events` when the Site blocks
+ // bots; its replay shouldn't be stored (or charged to the quota) either.
+ if (
+ isBlockedReplayBot({
+ blockBots: siteConfiguration.blockBots,
+ isMobileSite: siteConfiguration.type === "mobile",
+ headers: request.headers,
+ userAgent: String(userAgent),
+ ipAddress: requestIP,
+ screenWidth: body.metadata?.viewportWidth,
+ screenHeight: body.metadata?.viewportHeight,
+ clientBotSignalMask,
+ lookupAsn,
+ })
+ ) {
+ request.log.debug({ siteId }, "Skipping session replay event because the visitor was detected as a bot");
+ return reply.status(200).send({ success: true, message: "Session replay not recorded - bot detected" });
+ }
+
const origin = request.headers.origin || "";
const referrer = request.headers.referer || "";
const sessionReplayService = new SessionReplayIngestService();
- const { capped } = await sessionReplayService.recordEvents(siteId, body, {
+ const { capped, needsFullSnapshot } = await sessionReplayService.recordEvents(siteId, body, {
userAgent,
ipAddress: requestIP,
origin,
referrer,
+ anonymousId: anonymousId ?? anonymous_id,
+ saltUserIds: siteConfiguration.saltUserIds,
+ receivedAt,
+ lookupAsn,
});
if (capped) {
@@ -135,6 +180,12 @@ export async function recordSessionReplay(
return reply.send({ success: true, stopRecording: true, message: "Session replay size limit reached" });
}
+ if (needsFullSnapshot) {
+ // Lets the replay move to the visitor's new session (or be playable at
+ // all) from the next batch, instead of whenever rrweb next checks out.
+ return reply.send({ success: true, takeFullSnapshot: true });
+ }
+
return reply.send({ success: true });
} catch (error) {
if (error instanceof z.ZodError) {
diff --git a/server/src/api/sites/batchImportEvents.ts b/server/src/api/sites/batchImportEvents.ts
index e6d1b14e8..3fb55f4d5 100644
--- a/server/src/api/sites/batchImportEvents.ts
+++ b/server/src/api/sites/batchImportEvents.ts
@@ -98,7 +98,7 @@ export async function batchImportEvents(request: FastifyRequest<BatchImportReque
const invalidEventCount = events.length - transformedEvents.length;
const timestamps = transformedEvents.map(e => e.timestamp);
- const allowedIndices = quotaTracker.canImportBatch(timestamps);
+ const allowedIndices = await quotaTracker.reserveBatch(timestamps);
const eventsWithinQuota = allowedIndices.map(i => transformedEvents[i]);
const skippedDueToQuota = transformedEvents.length - eventsWithinQuota.length;
@@ -120,7 +120,7 @@ export async function batchImportEvents(request: FastifyRequest<BatchImportReque
if (isLastBatch) {
await completeImport(importId);
- importQuotaManager.completeImport(siteRecord.organizationId);
+ await importQuotaManager.completeImport(siteRecord.organizationId, importId);
}
return reply.send();
@@ -130,7 +130,7 @@ export async function batchImportEvents(request: FastifyRequest<BatchImportReque
if (isLastBatch) {
await completeImport(importId);
- importQuotaManager.completeImport(siteRecord.organizationId);
+ await importQuotaManager.completeImport(siteRecord.organizationId, importId);
}
return reply.status(500).send({
diff --git a/server/src/api/sites/createSiteImport.ts b/server/src/api/sites/createSiteImport.ts
index 4dc6f16e9..197f60496 100644
--- a/server/src/api/sites/createSiteImport.ts
+++ b/server/src/api/sites/createSiteImport.ts
@@ -1,3 +1,4 @@
+import { randomUUID } from "node:crypto";
import { FastifyReply, FastifyRequest } from "fastify";
import { importQuotaManager } from "../../services/import/importQuotaManager.js";
import { createImport } from "../../services/import/importStatusManager.js";
@@ -65,7 +66,10 @@ export async function createSiteImport(request: FastifyRequest<CreateSiteImportR
}
}
- if (!importQuotaManager.startImport(organizationId)) {
+ // The import's id owns its lock from the start, so whichever worker serves its final batch
+ // or its delete releases exactly this lock.
+ const importId = randomUUID();
+ if (!(await importQuotaManager.startImport(organizationId, importId))) {
return reply.status(429).send({ error: "Only 1 concurrent import allowed per organization" });
}
@@ -79,6 +83,7 @@ export async function createSiteImport(request: FastifyRequest<CreateSiteImportR
const latestAllowedDate = DateTime.utc().toFormat("yyyy-MM-dd");
const importRecord = await createImport({
+ importId,
siteId,
organizationId,
platform,
@@ -94,7 +99,7 @@ export async function createSiteImport(request: FastifyRequest<CreateSiteImportR
},
});
} catch (error) {
- importQuotaManager.completeImport(organizationId);
+ await importQuotaManager.completeImport(organizationId, importId);
request.log.error({ err: error }, "Error creating import");
return reply.status(500).send({ error: "Internal server error" });
}
diff --git a/server/src/api/sites/deleteSiteImport.test.ts b/server/src/api/sites/deleteSiteImport.test.ts
index 03364801d..357d68f1e 100644
--- a/server/src/api/sites/deleteSiteImport.test.ts
+++ b/server/src/api/sites/deleteSiteImport.test.ts
@@ -20,6 +20,7 @@ vi.mock("../../db/postgres/postgres.js", () => ({ db: {} }));
vi.mock("../../lib/subscriptionUtils.js", () => ({ getBestSubscription: vi.fn() }));
vi.mock("../../services/import/importQuotaTracker.js", () => ({ ImportQuotaTracker: {} }));
vi.mock("../../lib/const.js", () => ({ IS_CLOUD: false }));
+vi.mock("../../db/redis/redis.js", () => ({}));
import { deleteSiteImport } from "./deleteSiteImport.js";
diff --git a/server/src/api/sites/deleteSiteImport.ts b/server/src/api/sites/deleteSiteImport.ts
index 0897c8195..2bbed7d49 100644
--- a/server/src/api/sites/deleteSiteImport.ts
+++ b/server/src/api/sites/deleteSiteImport.ts
@@ -52,8 +52,8 @@ export async function deleteSiteImport(request: FastifyRequest<DeleteImportReque
}
// Only the client's final batch completes an import, so one whose client stopped early would
- // otherwise be undeletable. Judged from startedAt in Postgres rather than the in-memory import
- // lock, which only the worker that created the import holds (CLUSTER_WORKERS) and a restart drops.
+ // otherwise be undeletable. Judged from startedAt in Postgres rather than the import lock,
+ // which can be missing (Redis restarted, or the in-process fallback on another worker).
if (importRecord.completedAt === null && !isAbandoned(importRecord.startedAt)) {
return reply.status(400).send({ error: "Cannot delete active import" });
}
@@ -98,7 +98,9 @@ export async function deleteSiteImport(request: FastifyRequest<DeleteImportReque
return reply.status(500).send({ error: "Failed to delete import record" });
}
- importQuotaManager.completeImport(importRecord.organizationId);
+ // Releases the lock only if this import still holds it (an abandoned one whose lock
+ // hasn't expired yet), never the lock of another import that is running now.
+ await importQuotaManager.completeImport(importRecord.organizationId, importId);
return reply.send();
} catch (error) {
diff --git a/server/src/api/sites/updateSiteConfig.test.ts b/server/src/api/sites/updateSiteConfig.test.ts
index 4cd128ea3..323bf9eff 100644
--- a/server/src/api/sites/updateSiteConfig.test.ts
+++ b/server/src/api/sites/updateSiteConfig.test.ts
@@ -99,7 +99,7 @@ beforeEach(() => {
mocks.getSubscriptionInner.mockResolvedValue(subscription(false));
});
-describe("updateSiteConfig β€” session replay pro gating (the only subscription-gated field)", () => {
+describe("updateSiteConfig β€” session replay pro gating", () => {
it("rejects enabling session replay on a non-pro plan and writes nothing", async () => {
mocks.getSubscriptionInner.mockResolvedValue(subscription(false));
const reply = replyStub();
@@ -196,8 +196,6 @@ describe("updateSiteConfig β€” non-gated fields update without subscription chec
name: "Renamed",
public: true,
blockBots: false,
- webVitals: true,
- trackErrors: true,
trackUrlFragments: true,
excludedCountries: ["US", "GB"],
tags: ["prod"],
@@ -211,8 +209,6 @@ describe("updateSiteConfig β€” non-gated fields update without subscription chec
name: "Renamed",
public: true,
blockBots: false,
- webVitals: true,
- trackErrors: true,
trackUrlFragments: true,
excludedCountries: ["US", "GB"],
tags: ["prod"],
@@ -220,6 +216,17 @@ describe("updateSiteConfig β€” non-gated fields update without subscription chec
expect(mocks.invalidate).toHaveBeenCalledWith(state.site);
});
+ it("rejects turning on Standard features without an active subscription", async () => {
+ mocks.getSubscriptionInner.mockResolvedValue({ ...subscription(false), status: "free" });
+ const reply = replyStub();
+
+ await updateSiteConfig(makeRequest({ webVitals: true, trackErrors: true }), reply);
+
+ expect(reply.statusCode).toBe(403);
+ expect(reply.body.error).toContain("require an active subscription");
+ expect(state.updates).toHaveLength(0);
+ });
+
it("cleans and stores an updated domain", async () => {
const reply = replyStub();
diff --git a/server/src/api/stripe/createCheckoutSession.test.ts b/server/src/api/stripe/createCheckoutSession.test.ts
index e8d56c2a8..54e77f5d2 100644
--- a/server/src/api/stripe/createCheckoutSession.test.ts
+++ b/server/src/api/stripe/createCheckoutSession.test.ts
@@ -124,6 +124,48 @@ describe("createCheckoutSession β€” one trial per organization", () => {
});
});
+describe("createCheckoutSession β€” existing subscribers", () => {
+ it.each(["active", "trialing"])("refuses a second checkout when the org has a %s subscription", async status => {
+ mocks.subscriptionsList.mockResolvedValue({ data: [{ id: "sub_live", status }] });
+ const reply = replyStub();
+
+ await createCheckoutSession(requestStub("u_owner", validBody), reply);
+
+ expect(reply.statusCode).toBe(409);
+ expect(reply.body.error).toContain("already has an active subscription");
+ expect(mocks.checkoutSessionsCreate).not.toHaveBeenCalled();
+ });
+
+ it("finds an active subscription behind a full page of newer canceled ones", async () => {
+ const canceled = Array.from({ length: 100 }, (_, i) => ({ id: `sub_old_${i}`, status: "canceled" }));
+ mocks.subscriptionsList.mockImplementation(async ({ status }: { status: string }) => ({
+ data: status === "all" ? canceled : status === "active" ? [{ id: "sub_live", status: "active" }] : [],
+ }));
+ const reply = replyStub();
+
+ await createCheckoutSession(requestStub("u_owner", validBody), reply);
+
+ expect(reply.statusCode).toBe(409);
+ expect(mocks.subscriptionsList).toHaveBeenCalledWith(
+ expect.objectContaining({ customer: "cus_1", status: "active" })
+ );
+ expect(mocks.subscriptionsList).toHaveBeenCalledWith(
+ expect.objectContaining({ customer: "cus_1", status: "trialing" })
+ );
+ expect(mocks.checkoutSessionsCreate).not.toHaveBeenCalled();
+ });
+
+ it("lets an org whose subscription lapsed subscribe again", async () => {
+ mocks.subscriptionsList.mockResolvedValue({ data: [{ id: "sub_old", status: "canceled" }] });
+ const reply = replyStub();
+
+ await createCheckoutSession(requestStub("u_owner", validBody), reply);
+
+ expect(reply.statusCode).toBe(200);
+ expect(mocks.checkoutSessionsCreate).toHaveBeenCalledOnce();
+ });
+});
+
describe("createCheckoutSession β€” authorization", () => {
it("creates a checkout session for the org owner with metadata.organizationId from the authorized org", async () => {
const reply = replyStub();
diff --git a/server/src/api/stripe/createCheckoutSession.ts b/server/src/api/stripe/createCheckoutSession.ts
index 1b40842cf..1f8e7d290 100644
--- a/server/src/api/stripe/createCheckoutSession.ts
+++ b/server/src/api/stripe/createCheckoutSession.ts
@@ -15,6 +15,17 @@ interface CheckoutRequestBody {
referral?: string;
}
+/** Whether the customer has an active or trialing subscription. Throws if Stripe can't be read. */
+async function hasLiveSubscription(stripeCustomerId: string): Promise<boolean> {
+ // Filter by status on Stripe's side: a status "all" page holds only the newest 100, so a
+ // live subscription behind a long tail of canceled ones would be missed.
+ const statuses = ["active", "trialing"] as const;
+ const pages = await Promise.all(
+ statuses.map(status => (stripe as Stripe).subscriptions.list({ customer: stripeCustomerId, status, limit: 1 }))
+ );
+ return pages.some((page, i) => page.data.some(sub => sub.status === statuses[i]));
+}
+
export async function createCheckoutSession(
request: FastifyRequest<{ Body: CheckoutRequestBody }>,
reply: FastifyReply
@@ -71,6 +82,17 @@ export async function createCheckoutSession(
}
let stripeCustomerId = org.stripeCustomerId;
+
+ // A second checkout would start a second subscription billed alongside the first; plan
+ // changes go through update-subscription instead. Read fresh rather than from the
+ // subscription cache so a just-finished checkout is seen.
+ if (stripeCustomerId && (await hasLiveSubscription(stripeCustomerId))) {
+ return reply.status(409).send({
+ error:
+ "This organization already has an active subscription. To change plans, use the Billing page in your organization settings.",
+ });
+ }
+
// One free trial per organization. A customer created just below has no history. If the
// history can't be read this throws, so checkout fails and can be retried rather than
// handing a returning customer another trial.
diff --git a/server/src/api/user/unsubscribeMarketing.test.ts b/server/src/api/user/unsubscribeMarketing.test.ts
index c1d26559d..2148cdd52 100644
--- a/server/src/api/user/unsubscribeMarketing.test.ts
+++ b/server/src/api/user/unsubscribeMarketing.test.ts
@@ -169,14 +169,25 @@ describe("one-click marketing unsubscribe", () => {
}
});
- it("keeps the 200 response for email clients when unsubscribing fails", async () => {
- mocks.unsubscribeContact.mockRejectedValue(new Error("Contact update failed"));
+ it("reports a failed unsubscribe to the email client so it can retry", async () => {
+ mocks.unsubscribeContact.mockRejectedValue(new Error("Resend contact unsubscribe failed (rate_limit_exceeded)"));
const request = requestStub("POST", signedQuery());
const reply = replyStub();
await oneClickUnsubscribeMarketing(request, reply);
expect(request.log.error).toHaveBeenCalled();
- expect(reply.status).toHaveBeenCalledWith(200);
- expect(reply.send).toHaveBeenCalledWith();
+ expect(reply.status).toHaveBeenCalledWith(500);
+ expect(reply.send).toHaveBeenCalledWith({ error: "Failed to unsubscribe" });
+ });
+
+ it("does not show the confirmation page when a link click fails to unsubscribe", async () => {
+ mocks.unsubscribeContact.mockRejectedValue(new Error("Resend contact unsubscribe failed (rate_limit_exceeded)"));
+ const reply = replyStub();
+ await oneClickUnsubscribeMarketing(requestStub("GET", signedQuery()), reply);
+
+ expect(reply.status).toHaveBeenCalledWith(500);
+ expect(reply.type).toHaveBeenCalledWith("text/html");
+ expect(reply.send).not.toHaveBeenCalledWith(expect.stringContaining("<h1>Unsubscribed</h1>"));
+ expect(reply.send).toHaveBeenCalledWith(expect.stringContaining("try the link again"));
});
});
diff --git a/server/src/api/user/unsubscribeMarketing.ts b/server/src/api/user/unsubscribeMarketing.ts
index 332103f10..42abd7843 100644
--- a/server/src/api/user/unsubscribeMarketing.ts
+++ b/server/src/api/user/unsubscribeMarketing.ts
@@ -39,6 +39,8 @@ export const unsubscribeMarketing = async (request: FastifyRequest, reply: Fasti
// One-click unsubscribe handler
// GET: User clicks link in email - show confirmation page
// POST: Email client's List-Unsubscribe-Post - return 200 OK
+// If the unsubscribe can't be recorded, both return a 500: neither the reader nor the email
+// client is told it worked, and the client can try again.
// Unsigned links are honored only until this date: emails sent before link
// signing existed (2026-09) carry no signature, and unsubscribe links must
// keep working for a while. After the cutoff every link must carry a valid
@@ -97,7 +99,16 @@ export const oneClickUnsubscribeMarketing = async (
return reply.status(200).send();
} catch (error) {
request.log.error({ err: error }, "Error in one-click unsubscribe");
- // Still return 200 to not confuse email clients
- return reply.status(200).send();
+ if (request.method === "GET") {
+ return reply.status(500).type("text/html").send(`
+ <!DOCTYPE html>
+ <html><head><title>Unsubscribe - Rybbit</title></head>
+ <body style="font-family: sans-serif; max-width: 600px; margin: 50px auto; text-align: center;">
+ <h1>Something went wrong</h1>
+ <p>We couldn't unsubscribe you just now. Please try the link again in a few minutes.</p>
+ </body></html>
+ `);
+ }
+ return reply.status(500).send({ error: "Failed to unsubscribe" });
}
};
diff --git a/server/src/cluster.ts b/server/src/cluster.ts
index 74fd51868..b97ce1297 100644
--- a/server/src/cluster.ts
+++ b/server/src/cluster.ts
@@ -7,7 +7,11 @@ import { lifecycleEmailService } from "./services/lifecycleEmails/lifecycleEmail
import { sessionsService } from "./services/sessions/sessionsService.js";
import { telemetryService } from "./services/telemetryService.js";
import { unclaimedSiteCleanupService } from "./services/sites/unclaimedSiteCleanupService.js";
-import { REFRESH_ORGANIZATION_USAGE_MESSAGE, usageService } from "./services/usageService.js";
+import {
+ REFRESH_ORGANIZATION_USAGE_MESSAGE,
+ USAGE_STATE_REQUEST_MESSAGE,
+ usageService,
+} from "./services/usageService.js";
import { weeklyReportService } from "./services/weekyReports/weeklyReportService.js";
const logger = createServiceLogger("cluster");
@@ -64,20 +68,22 @@ if (workerCount === 0) {
cluster.fork();
}
- // Workers ask the primary to re-evaluate an organization's blocked sites when a trial
- // starts or a site is added, so the change reaches every worker without waiting for the cron
- cluster.on("message", (_worker, message: { type?: string; organizationId?: unknown }) => {
+ cluster.on("message", (worker, message: { type?: string; organizationId?: unknown }) => {
+ // Workers ask the primary to re-evaluate an organization's blocked sites when a trial
+ // starts or a site is added, so the change reaches every worker without waiting for the cron
if (message?.type === REFRESH_ORGANIZATION_USAGE_MESSAGE && typeof message.organizationId === "string") {
void usageService.refreshOrganizationWithRetry(message.organizationId);
}
+ // A starting worker asks for the current usage state once its listener is installed and
+ // waits for it before serving. Sending on "online" instead raced the worker's module
+ // loading, and a message that arrives before the listener exists is dropped.
+ if (message?.type === USAGE_STATE_REQUEST_MESSAGE && worker.isConnected()) {
+ worker.send(usageService.getUsageStateMessage());
+ }
});
- // Send current usage state to a worker when it comes online
cluster.on("online", worker => {
logger.info(`Worker ${worker.process.pid} is online`);
- worker.send({ type: "sites-over-limit", siteIds: Array.from(usageService.getSitesOverLimit()) });
- worker.send({ type: "sites-without-plan", siteIds: Array.from(usageService.getSitesWithoutPlan()) });
- worker.send({ type: "sites-without-replay", siteIds: Array.from(usageService.getSitesWithoutReplay()) });
});
// Auto-restart crashed workers (unless shutting down)
@@ -91,22 +97,18 @@ if (workerCount === 0) {
});
/**
- * Broadcast the current sitesOverLimit and sitesWithoutReplay sets to all alive workers via IPC
+ * Broadcast the current sitesOverLimit, sitesWithoutPlan and sitesWithoutReplay sets to all alive workers via IPC
*/
function broadcastUsageState() {
- const overLimitIds = Array.from(usageService.getSitesOverLimit());
- const withoutPlanIds = Array.from(usageService.getSitesWithoutPlan());
- const withoutReplayIds = Array.from(usageService.getSitesWithoutReplay());
+ const message = usageService.getUsageStateMessage();
for (const id in cluster.workers) {
const worker = cluster.workers[id];
if (worker && !worker.isDead()) {
- worker.send({ type: "sites-over-limit", siteIds: overLimitIds });
- worker.send({ type: "sites-without-plan", siteIds: withoutPlanIds });
- worker.send({ type: "sites-without-replay", siteIds: withoutReplayIds });
+ worker.send(message);
}
}
logger.debug(
- `Broadcasted ${overLimitIds.length} sites-over-limit, ${withoutPlanIds.length} sites-without-plan and ${withoutReplayIds.length} sites-without-replay to workers`
+ `Broadcasted ${message.sitesOverLimit.length} sites-over-limit, ${message.sitesWithoutPlan.length} sites-without-plan and ${message.sitesWithoutReplay.length} sites-without-replay to workers`
);
}
diff --git a/server/src/db/redis/importQuota.integration.test.ts b/server/src/db/redis/importQuota.integration.test.ts
new file mode 100644
index 000000000..d2817c137
--- /dev/null
+++ b/server/src/db/redis/importQuota.integration.test.ts
@@ -0,0 +1,102 @@
+import RedisMock from "ioredis-mock";
+import { beforeEach, describe, expect, it } from "vitest";
+import { IMPORT_LOCK_RELEASE_LUA, IMPORT_QUOTA_RESERVE_LUA } from "./importQuotaLua.js";
+
+// Behavioral tests for the import lock and quota Lua, executed for real by
+// ioredis-mock's embedded Lua runtime.
+
+const LOCK = "import:lock:org_1";
+const QUOTA = "import:quota:org_1";
+const TTL_MS = 60_000;
+
+interface ImportMockRedis {
+ importLockRelease(key: string, owner: string): Promise<number>;
+ importQuotaReserve(key: string, ...args: (string | number)[]): Promise<number[]>;
+ set(key: string, value: string, ...args: (string | number)[]): Promise<string | null>;
+ get(key: string): Promise<string | null>;
+ hget(key: string, field: string): Promise<string | null>;
+ pttl(key: string): Promise<number>;
+ flushall(): Promise<unknown>;
+}
+
+let redis: ImportMockRedis;
+
+const reserve = (limit: number, ...triples: [string, number, number][]) =>
+ redis.importQuotaReserve(QUOTA, limit, TTL_MS, ...triples.flat());
+
+describe("import lock Lua scripts", () => {
+ beforeEach(async () => {
+ const client = new RedisMock();
+ client.defineCommand("importLockRelease", { numberOfKeys: 1, lua: IMPORT_LOCK_RELEASE_LUA });
+ redis = client as unknown as ImportMockRedis;
+ await redis.flushall();
+ });
+
+ it("only one caller acquires the lock", async () => {
+ expect(await redis.set(LOCK, "import_a", "PX", TTL_MS, "NX")).toBe("OK");
+ expect(await redis.set(LOCK, "import_b", "PX", TTL_MS, "NX")).toBeNull();
+ });
+
+ it("only the import that holds the lock releases it", async () => {
+ await redis.set(LOCK, "import_a", "PX", TTL_MS, "NX");
+
+ // Deleting an older import leaves the running import's lock alone.
+ expect(await redis.importLockRelease(LOCK, "import_old")).toBe(0);
+ expect(await redis.get(LOCK)).toBe("import_a");
+
+ expect(await redis.importLockRelease(LOCK, "import_a")).toBe(1);
+ expect(await redis.get(LOCK)).toBeNull();
+ });
+});
+
+describe("import quota Lua script", () => {
+ beforeEach(async () => {
+ const client = new RedisMock();
+ client.defineCommand("importQuotaReserve", { numberOfKeys: 1, lua: IMPORT_QUOTA_RESERVE_LUA });
+ redis = client as unknown as ImportMockRedis;
+ await redis.flushall();
+ });
+
+ it("seeds a month from the baseline and grants only what is left of the limit", async () => {
+ expect(await reserve(10, ["202405", 7, 5])).toEqual([3]);
+ expect(await redis.hget(QUOTA, "202405")).toBe("10");
+ });
+
+ it("shares reservations between callers that bring a lower baseline", async () => {
+ // Two workers with the same stale ClickHouse baseline: the second sees the first's reservation.
+ expect(await reserve(10, ["202405", 4, 5])).toEqual([5]);
+ expect(await reserve(10, ["202405", 4, 5])).toEqual([1]);
+ expect(await reserve(10, ["202405", 4, 5])).toEqual([0]);
+ });
+
+ it("adopts a refreshed baseline that is higher than the stored count", async () => {
+ expect(await reserve(10, ["202405", 2, 3])).toEqual([3]);
+ expect(await redis.hget(QUOTA, "202405")).toBe("5");
+
+ // A rebuilt tracker sees live traffic in ClickHouse that the stored count never counted.
+ expect(await reserve(10, ["202405", 9, 3])).toEqual([1]);
+ expect(await redis.hget(QUOTA, "202405")).toBe("10");
+
+ // Live traffic alone has reached the limit: nothing more is granted.
+ await redis.flushall();
+ await reserve(10, ["202405", 2, 1]);
+ expect(await reserve(10, ["202405", 10, 4])).toEqual([0]);
+ expect(await redis.hget(QUOTA, "202405")).toBe("10");
+ });
+
+ it("tracks months independently and returns grants in request order", async () => {
+ expect(await reserve(3, ["202405", 3, 2], ["202406", 0, 2])).toEqual([0, 2]);
+ expect(await redis.hget(QUOTA, "202405")).toBe("3");
+ expect(await redis.hget(QUOTA, "202406")).toBe("2");
+ });
+
+ it("never grants a negative count when a month is already past its limit", async () => {
+ expect(await reserve(5, ["202405", 9, 1])).toEqual([0]);
+ expect(await redis.hget(QUOTA, "202405")).toBe("9");
+ });
+
+ it("slides the TTL on every call", async () => {
+ await reserve(5, ["202405", 0, 1]);
+ expect(await redis.pttl(QUOTA)).toBeGreaterThan(0);
+ });
+});
diff --git a/server/src/db/redis/importQuotaLua.ts b/server/src/db/redis/importQuotaLua.ts
new file mode 100644
index 000000000..7df810645
--- /dev/null
+++ b/server/src/db/redis/importQuotaLua.ts
@@ -0,0 +1,59 @@
+/**
+ * Shared state for data imports, so every cluster worker enforces the same
+ * one-import-per-organization lock and monthly quota.
+ *
+ * The lock is a plain `SET key importId NX PX ttl`; the release script is its
+ * owner-checked follow-up, so a late or stale caller can't release a lock that
+ * now belongs to a different import.
+ */
+
+/**
+ * KEYS[1] lock key
+ * ARGV[1] expected owner
+ *
+ * Deletes the lock only while it still belongs to ARGV[1]. Returns 1 if deleted.
+ */
+export const IMPORT_LOCK_RELEASE_LUA = `
+if redis.call('GET', KEYS[1]) == ARGV[1] then
+ return redis.call('DEL', KEYS[1])
+end
+return 0
+`;
+
+/**
+ * Per-organization monthly import quota, checked and reserved in one atomic
+ * round trip.
+ *
+ * KEYS[1] hash of month (yyyyMM) -> events counted against that month
+ *
+ * ARGV[1] monthly event limit
+ * ARGV[2] TTL in milliseconds (slides on every call)
+ * ARGV[3..] triples of (month, baseline, requested)
+ *
+ * A month's usage is the larger of its stored count, which includes every
+ * worker's reservations, and the caller's baseline (ClickHouse usage when its
+ * tracker was built, plus its own reservations since). Trackers are rebuilt
+ * regularly, so events that reached ClickHouse after the count was seeded,
+ * live traffic included, are adopted instead of undercounted. Each month
+ * grants min(requested, limit - usage), and the stored count becomes usage
+ * plus the grant. Returns the granted count per triple, in order.
+ */
+export const IMPORT_QUOTA_RESERVE_LUA = `
+local limit = tonumber(ARGV[1])
+local ttlMs = tonumber(ARGV[2])
+local granted = {}
+
+for i = 3, #ARGV, 3 do
+ local month = ARGV[i]
+ local stored = tonumber(redis.call('HGET', KEYS[1], month) or 0)
+ local used = math.max(stored, tonumber(ARGV[i + 1]))
+ local grant = math.max(0, math.min(tonumber(ARGV[i + 2]), limit - used))
+ redis.call('HSET', KEYS[1], month, string.format('%d', used + grant))
+ granted[#granted + 1] = grant
+end
+
+redis.call('PEXPIRE', KEYS[1], ttlMs)
+-- Same table either way; ioredis-mock (the tests' Lua runtime) can only return
+-- it in this form.
+return { unpack(granted) }
+`;
diff --git a/server/src/db/redis/redis.ts b/server/src/db/redis/redis.ts
index 1a2ca50c2..243a6080c 100644
--- a/server/src/db/redis/redis.ts
+++ b/server/src/db/redis/redis.ts
@@ -1,6 +1,7 @@
import { Redis } from "ioredis";
import { createServiceLogger } from "../../lib/logger/logger.js";
import { API_RATE_LIMIT_LUA } from "./apiRateLimitLua.js";
+import { IMPORT_LOCK_RELEASE_LUA, IMPORT_QUOTA_RESERVE_LUA } from "./importQuotaLua.js";
import { REPLAY_BUDGET_LUA } from "./replayBudgetLua.js";
import { STICKY_RESOLVE_LUA } from "./stickyResolveLua.js";
@@ -411,3 +412,50 @@ export async function anomalyObserve(nowMs: number, specs: AnomalyCounterSpec[])
distinct: flat[index * 3 + 2] ?? 0,
}));
}
+
+// Data import lock and monthly quota (importQuotaManager), shared so every cluster
+// worker enforces one import per organization and one quota. Imports are rare and
+// each call is a single small script, so they use the main connection.
+redis.defineCommand("importLockRelease", { numberOfKeys: 1, lua: IMPORT_LOCK_RELEASE_LUA });
+redis.defineCommand("importQuotaReserve", { numberOfKeys: 1, lua: IMPORT_QUOTA_RESERVE_LUA });
+
+interface ImportRedis extends Redis {
+ importLockRelease(key: string, owner: string): Promise<number>;
+ importQuotaReserve(key: string, ...args: (string | number)[]): Promise<number[]>;
+}
+
+/** Takes the lock for `owner` unless someone holds it. Atomic across all workers. */
+export async function importLockAcquire(key: string, owner: string, ttlMs: number): Promise<boolean> {
+ return (await redis.set(key, owner, "PX", ttlMs, "NX")) === "OK";
+}
+
+/** Releases the lock if `owner` still holds it. */
+export async function importLockRelease(key: string, owner: string): Promise<boolean> {
+ return (await (redis as ImportRedis).importLockRelease(key, owner)) === 1;
+}
+
+export interface ImportQuotaRequest {
+ /** yyyyMM */
+ month: string;
+ /** Events the caller knows are counted for the month; the shared count never drops below it. */
+ baseline: number;
+ requested: number;
+}
+
+/**
+ * Reserves up to `requested` events per month against `monthlyLimit` and returns how many
+ * were granted, in the same order. Atomic across all workers; one round-trip.
+ */
+export async function importQuotaReserve(
+ key: string,
+ monthlyLimit: number,
+ ttlMs: number,
+ requests: ImportQuotaRequest[]
+): Promise<number[]> {
+ const args: (string | number)[] = [monthlyLimit, ttlMs];
+ for (const request of requests) {
+ args.push(request.month, request.baseline, request.requested);
+ }
+ const granted = await (redis as ImportRedis).importQuotaReserve(key, ...args);
+ return requests.map((_, index) => Number(granted[index] ?? 0));
+}
diff --git a/server/src/index.ts b/server/src/index.ts
index b10d0cc64..c3e06b381 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -27,7 +27,12 @@ import { telemetryService } from "./services/telemetryService.js";
import { handleIdentify } from "./services/tracker/identifyService.js";
import { trackEvent } from "./services/tracker/trackEvent.js";
import { startSiteBaselineRefresh } from "./services/tracker/botBlocking/siteBaseline.js";
-import { usageService } from "./services/usageService.js";
+import {
+ USAGE_STATE_MESSAGE,
+ USAGE_STATE_REQUEST_MESSAGE,
+ usageService,
+ type UsageStateMessage,
+} from "./services/usageService.js";
import { unclaimedSiteCleanupService } from "./services/sites/unclaimedSiteCleanupService.js";
import { weeklyReportService } from "./services/weekyReports/weeklyReportService.js";
@@ -51,11 +56,17 @@ server.register(cors, {
server.addHook("onRequest", createRejectUntrustedOriginHook());
// Opt-in per-route rate limiting (routes declare config.rateLimit). Runs in
-// preHandler so authenticated routes can key on the user instead of the IP.
+// preHandler so authenticated routes can key on the caller instead of the IP.
+// Org API keys carry no user; without their organization in the key, every
+// MCP-proxied call (all from 127.0.0.1) would share one bucket.
server.register(rateLimit, {
global: false,
hook: "preHandler",
- keyGenerator: (request: FastifyRequest) => request.user?.id ?? request.ip,
+ keyGenerator: (request: FastifyRequest) => {
+ if (request.user?.id) return `user:${request.user.id}`;
+ if (request.apiKeyOrganizationId) return `org:${request.apiKeyOrganizationId}`;
+ return request.ip;
+ },
// Shared store so the limit holds across cluster workers; fall open if Redis
// is unreachable rather than blocking the request.
redis: apiRateLimitRedis,
@@ -125,6 +136,36 @@ server.post("/api/identify", handleIdentify);
// Register API routes with /api prefix
server.register(apiRoutes, { prefix: "/api" });
+// How long a new worker waits for the primary's usage state before serving anyway.
+const USAGE_STATE_WAIT_MS = 10_000;
+
+/**
+ * Installs the worker's listener for usage state from the cluster primary, then asks for the
+ * current state. Resolves once it has been applied, so the worker doesn't serve over-limit or
+ * plan-less sites until the next broadcast (up to 30 minutes). Never rejects: if the primary
+ * doesn't answer in time the worker serves without blocks, as it did before the handshake.
+ */
+function receiveUsageStateFromPrimary(): Promise<void> {
+ return new Promise(resolve => {
+ const timeout = setTimeout(() => {
+ server.log.warn("No usage state from the cluster primary yet; serving until it arrives");
+ resolve();
+ }, USAGE_STATE_WAIT_MS);
+
+ process.on("message", (message: UsageStateMessage) => {
+ if (message?.type !== USAGE_STATE_MESSAGE) return;
+ usageService.applyUsageStateMessage(message);
+ server.log.debug(
+ `Received ${message.sitesOverLimit.length} sites-over-limit, ${message.sitesWithoutPlan.length} sites-without-plan and ${message.sitesWithoutReplay.length} sites-without-replay from primary`
+ );
+ clearTimeout(timeout);
+ resolve();
+ });
+
+ process.send?.({ type: USAGE_STATE_REQUEST_MESSAGE });
+ });
+}
+
const start = async () => {
try {
// When running as a cluster worker, the primary process already initialized the databases
@@ -147,25 +188,13 @@ const start = async () => {
}
}
- // Start the server first
- await server.listen({ port: 3001, host: "0.0.0.0" });
- server.log.info(`Server is listening on http://0.0.0.0:3001 (PID: ${process.pid})`);
-
- // Listen for IPC messages from the cluster primary process
+ // Blocked-site sets live on the cluster primary; apply them before accepting traffic.
if (cluster.isWorker) {
- process.on("message", (message: { type: string; siteIds: number[] }) => {
- if (message?.type === "sites-over-limit") {
- usageService.setSitesOverLimit(new Set(message.siteIds));
- server.log.debug(`Received ${message.siteIds.length} sites-over-limit from primary`);
- } else if (message?.type === "sites-without-plan") {
- usageService.setSitesWithoutPlan(new Set(message.siteIds));
- server.log.debug(`Received ${message.siteIds.length} sites-without-plan from primary`);
- } else if (message?.type === "sites-without-replay") {
- usageService.setSitesWithoutReplay(new Set(message.siteIds));
- server.log.debug(`Received ${message.siteIds.length} sites-without-replay from primary`);
- }
- });
+ await receiveUsageStateFromPrimary();
}
+
+ await server.listen({ port: 3001, host: "0.0.0.0" });
+ server.log.info(`Server is listening on http://0.0.0.0:3001 (PID: ${process.pid})`);
} catch (err) {
server.log.error(err);
process.exit(1);
diff --git a/server/src/lib/email/email.test.ts b/server/src/lib/email/email.test.ts
new file mode 100644
index 000000000..d1f5c74b0
--- /dev/null
+++ b/server/src/lib/email/email.test.ts
@@ -0,0 +1,81 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+// The Resend SDK resolves failed requests as { data: null, error } rather than rejecting;
+// these tests pin that the helpers read `error` instead of treating the call as a success.
+const mocks = vi.hoisted(() => ({
+ emailsSend: vi.fn(),
+ audiencesList: vi.fn(),
+ audiencesCreate: vi.fn(),
+ contactsGet: vi.fn(),
+ contactsUpdate: vi.fn(),
+}));
+
+vi.mock("resend", () => ({
+ Resend: class {
+ emails = { send: mocks.emailsSend };
+ audiences = { list: mocks.audiencesList, create: mocks.audiencesCreate };
+ contacts = { get: mocks.contactsGet, update: mocks.contactsUpdate };
+ },
+}));
+
+vi.mock("../const.js", () => ({ IS_CLOUD: true }));
+
+const { isContactUnsubscribed, sendEmail, unsubscribeContact } = await import("./email.js");
+
+const rateLimited = { data: null, error: { name: "rate_limit_exceeded", message: "Too many requests" } };
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ mocks.audiencesList.mockResolvedValue({ data: { data: [{ id: "aud_1", name: "Marketing" }] }, error: null });
+});
+
+describe("sendEmail", () => {
+ it("resolves when Resend accepts the email", async () => {
+ mocks.emailsSend.mockResolvedValue({ data: { id: "email_1" }, error: null });
+
+ await expect(sendEmail("[email protected]", "Hi", "<p>Hi</p>")).resolves.toMatchObject({ data: { id: "email_1" } });
+ });
+
+ it("throws when Resend reports an error instead of logging it as sent", async () => {
+ mocks.emailsSend.mockResolvedValue(rateLimited);
+
+ await expect(sendEmail("[email protected]", "Hi", "<p>Hi</p>")).rejects.toThrow("rate_limit_exceeded");
+ });
+});
+
+describe("isContactUnsubscribed", () => {
+ it("reports the contact's unsubscribed flag", async () => {
+ mocks.contactsGet.mockResolvedValue({ data: { unsubscribed: false }, error: null });
+ await expect(isContactUnsubscribed("[email protected]")).resolves.toBe(false);
+
+ mocks.contactsGet.mockResolvedValue({ data: { unsubscribed: true }, error: null });
+ await expect(isContactUnsubscribed("[email protected]")).resolves.toBe(true);
+ });
+
+ it("skips contacts that were never added to the audience", async () => {
+ mocks.contactsGet.mockResolvedValue({ data: null, error: { name: "not_found", message: "Contact not found" } });
+
+ await expect(isContactUnsubscribed("[email protected]")).resolves.toBe(true);
+ });
+
+ it("throws on a rate limit instead of treating the contact as unsubscribed", async () => {
+ mocks.contactsGet.mockResolvedValue(rateLimited);
+
+ await expect(isContactUnsubscribed("[email protected]")).rejects.toThrow("rate_limit_exceeded");
+ });
+});
+
+describe("unsubscribeContact", () => {
+ it("throws when Resend rejects the update, so the caller doesn't confirm it", async () => {
+ mocks.contactsUpdate.mockResolvedValue(rateLimited);
+
+ await expect(unsubscribeContact("[email protected]")).rejects.toThrow("rate_limit_exceeded");
+ });
+
+ it("treats a contact outside the audience as already unsubscribed", async () => {
+ mocks.contactsUpdate.mockResolvedValue({ data: null, error: { name: "not_found", message: "Contact not found" } });
+
+ await expect(unsubscribeContact("[email protected]")).resolves.toBeUndefined();
+ });
+});
diff --git a/server/src/lib/email/email.ts b/server/src/lib/email/email.ts
index 61fed911d..0c435958e 100644
--- a/server/src/lib/email/email.ts
+++ b/server/src/lib/email/email.ts
@@ -17,13 +17,29 @@ if (IS_CLOUD) {
resend = new Resend(process.env.RESEND_API_KEY);
}
+/**
+ * The Resend SDK resolves failed requests (HTTP errors, rate limits, network failures) as
+ * { data: null, error } instead of rejecting, so every call must check `error` itself.
+ */
+class ResendRequestError extends Error {
+ constructor(
+ action: string,
+ readonly resendError: { name: string; message: string }
+ ) {
+ super(`Resend ${action} failed (${resendError.name}): ${resendError.message}`);
+ this.name = "ResendRequestError";
+ }
+}
+
// Marketing audience management
export const getOrCreateMarketingAudience = async (): Promise<string> => {
if (marketingAudienceId) return marketingAudienceId;
if (!resend) throw new Error("Resend not initialized");
- // List existing audiences to check if "Marketing" exists
- const { data: audiences } = await resend.audiences.list();
+ // List existing audiences to check if "Marketing" exists. A failed list must not fall
+ // through to creating (and caching) a second, empty "Marketing" audience.
+ const { data: audiences, error: listError } = await resend.audiences.list();
+ if (listError) throw new ResendRequestError("audience list", listError);
const existing = audiences?.data?.find((a: { name: string }) => a.name === "Marketing");
if (existing) {
@@ -32,45 +48,58 @@ export const getOrCreateMarketingAudience = async (): Promise<string> => {
}
// Create new audience
- const { data } = await resend.audiences.create({ name: "Marketing" });
- marketingAudienceId = data!.id;
- return data!.id;
+ const { data, error: createError } = await resend.audiences.create({ name: "Marketing" });
+ if (createError || !data) {
+ throw new ResendRequestError("audience create", createError ?? { name: "application_error", message: "no data" });
+ }
+ marketingAudienceId = data.id;
+ return data.id;
};
export const addContactToAudience = async (email: string, firstName?: string): Promise<void> => {
if (!resend) return;
try {
const audienceId = await getOrCreateMarketingAudience();
- await resend.contacts.create({ audienceId, email, firstName, unsubscribed: false });
+ const { error } = await resend.contacts.create({ audienceId, email, firstName, unsubscribed: false });
+ if (error) throw new ResendRequestError("contact create", error);
} catch (error) {
console.error("Failed to add contact to audience:", error);
}
};
+/**
+ * Whether marketing email to this address must be skipped: the contact unsubscribed, or was
+ * never added to the audience. Throws when Resend can't answer (rate limit, outage), so the
+ * caller skips this send and logs it instead of recording the contact as unsubscribed.
+ */
export const isContactUnsubscribed = async (email: string): Promise<boolean> => {
if (!resend) return false;
- try {
- const audienceId = await getOrCreateMarketingAudience();
- const { data: contact } = await resend.contacts.get({ audienceId, email });
- // If contact doesn't exist or is unsubscribed, return true to skip sending
- if (!contact) return true;
- return contact.unsubscribed ?? false;
- } catch (error) {
- // If contact doesn't exist (404), don't send email
- return true;
+ const audienceId = await getOrCreateMarketingAudience();
+ const { data: contact, error } = await resend.contacts.get({ audienceId, email });
+ if (error) {
+ if (error.name === "not_found") return true;
+ throw new ResendRequestError("contact lookup", error);
}
+ // If contact doesn't exist or is unsubscribed, return true to skip sending
+ if (!contact) return true;
+ return contact.unsubscribed ?? false;
};
+/**
+ * Marks the contact unsubscribed. Throws if Resend rejects the update so the caller can
+ * report the failure instead of confirming an unsubscribe that didn't happen. A contact
+ * that isn't in the audience already receives no marketing email.
+ */
export const unsubscribeContact = async (email: string): Promise<void> => {
if (!resend) return;
- try {
- const audienceId = await getOrCreateMarketingAudience();
- await resend.contacts.update({ audienceId, email, unsubscribed: true });
- } catch (error) {
- console.error("Failed to unsubscribe contact:", error);
+ const audienceId = await getOrCreateMarketingAudience();
+ const { error } = await resend.contacts.update({ audienceId, email, unsubscribed: true });
+ if (error && error.name !== "not_found") {
+ throw new ResendRequestError("contact unsubscribe", error);
}
};
+/** Sends a transactional email. Throws when Resend does not accept it. */
export const sendEmail = async (email: string, subject: string, html: string) => {
if (!resend) {
return;
@@ -84,6 +113,9 @@ export const sendEmail = async (email: string, subject: string, html: string) =>
subject,
html,
});
+ if (response.error) {
+ throw new ResendRequestError("email send", response.error);
+ }
return response;
} catch (error) {
console.error(error);
@@ -242,13 +274,14 @@ Cheers,
Bill`;
try {
- await resend.emails.send({
+ const { error } = await resend.emails.send({
from: "Bill from Rybbit <[email protected]>",
replyTo: "[email protected]",
to: email,
subject: "Welcome to Rybbit!",
text,
});
+ if (error) throw new ResendRequestError("welcome email send", error);
} catch (error) {
console.error("Failed to send welcome email:", error);
}
diff --git a/server/src/mcp/mcp.test.ts b/server/src/mcp/mcp.test.ts
index de6c39e94..ab6ae0be0 100644
--- a/server/src/mcp/mcp.test.ts
+++ b/server/src/mcp/mcp.test.ts
@@ -129,6 +129,19 @@ describe("mcp endpoint", () => {
fastify.get("/sites/:siteId/overview", async request => {
captured.url = request.url;
captured.query = request.query as Record<string, unknown>;
+ if ((request.params as { siteId: string }).siteId === "6") {
+ // An empty window: ClickHouse returns NaN averages, serialized as null.
+ return {
+ data: {
+ sessions: 0,
+ pageviews: 0,
+ users: 0,
+ pages_per_session: null,
+ bounce_rate: null,
+ session_duration: null,
+ },
+ };
+ }
return { data: { sessions: 100, pageviews: 250 } };
});
@@ -153,6 +166,14 @@ describe("mcp endpoint", () => {
fastify.post("/sites/:siteId/funnels/analyze", async request => {
captured.body = request.body;
captured.query = request.query as Record<string, unknown>;
+ if ((request.params as { siteId: string }).siteId === "6") {
+ return {
+ data: [
+ { step_number: 1, step_name: "Step 1", sessions: 0, conversion_rate: null, dropoff_rate: 0 },
+ { step_number: 2, step_name: "Step 2", sessions: 0, conversion_rate: null, dropoff_rate: null },
+ ],
+ };
+ }
// The API returns `sessions` plus the deprecated `visitors` alias.
return {
data: [{ step_number: 1, step_name: "Step 1", sessions: 10, visitors: 10, conversion_rate: 100, dropoff_rate: 0 }],
@@ -694,6 +715,23 @@ describe("mcp endpoint", () => {
expect(captured.query).toMatchObject({ past_minutes_start: "1440" });
});
+ it("passes null averages and rates from an empty window through output validation", async () => {
+ const overview = await callTool(app, "get_overview", { site_id: 6, past_minutes: 60 });
+ expect(overview.isError).toBeFalsy();
+ expect(overview.structuredContent.data).toMatchObject({ bounce_rate: null, session_duration: null });
+
+ const funnel = await callTool(app, "analyze_funnel", {
+ site_id: 6,
+ steps: [
+ { type: "page", value: "/pricing" },
+ { type: "event", value: "signup" },
+ ],
+ past_minutes: 60,
+ });
+ expect(funnel.isError).toBeFalsy();
+ expect(funnel.structuredContent.data[1]).toMatchObject({ conversion_rate: null, dropoff_rate: null });
+ });
+
it("surfaces REST errors as tool errors with a hint", async () => {
const result = await callTool(app, "get_goals", { site_id: 5 });
diff --git a/server/src/mcp/tools/analytics.ts b/server/src/mcp/tools/analytics.ts
index cc951b1dd..255395ec1 100644
--- a/server/src/mcp/tools/analytics.ts
+++ b/server/src/mcp/tools/analytics.ts
@@ -4,14 +4,15 @@ import { RybbitApiClient } from "../apiClient.js";
import { FILTER_PARAMETERS, TIME_BUCKETS, filtersInput, siteIdInput, timeInputs } from "../inputs.js";
import { looseRow, ok, readOnly, siteQuery, type ScopeCheck, type ToolGuard } from "./shared.js";
+// Averages and rates come back null (ClickHouse NaN) for a window with no sessions.
const overviewMetrics = z
.object({
sessions: z.number(),
pageviews: z.number(),
users: z.number(),
- pages_per_session: z.number(),
- bounce_rate: z.number(),
- session_duration: z.number(),
+ pages_per_session: z.number().nullable(),
+ bounce_rate: z.number().nullable(),
+ session_duration: z.number().nullable(),
})
.partial()
.passthrough();
diff --git a/server/src/mcp/tools/funnels.ts b/server/src/mcp/tools/funnels.ts
index 274fa034d..a73b42eec 100644
--- a/server/src/mcp/tools/funnels.ts
+++ b/server/src/mcp/tools/funnels.ts
@@ -22,8 +22,9 @@ const funnelStepResult = z
sessions: z.number(),
/** Deprecated alias of `sessions`. */
visitors: z.number().optional(),
- conversion_rate: z.number(),
- dropoff_rate: z.number(),
+ // null (ClickHouse NaN) when the step it divides by had no sessions.
+ conversion_rate: z.number().nullable(),
+ dropoff_rate: z.number().nullable(),
})
.partial()
.passthrough();
diff --git a/server/src/services/import/importQuotaManager.test.ts b/server/src/services/import/importQuotaManager.test.ts
new file mode 100644
index 000000000..2f8adad68
--- /dev/null
+++ b/server/src/services/import/importQuotaManager.test.ts
@@ -0,0 +1,125 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ importLockAcquire: vi.fn(),
+ importLockRelease: vi.fn(),
+ getImportById: vi.fn(),
+}));
+
+vi.mock("../../db/redis/redis.js", () => mocks);
+vi.mock("../../lib/const.js", () => ({ IS_CLOUD: true }));
+vi.mock("./importQuotaTracker.js", () => ({ ImportQuotaTracker: {} }));
+vi.mock("./importStatusManager.js", () => ({ getImportById: mocks.getImportById }));
+vi.mock("../../lib/logger/logger.js", () => ({
+ createServiceLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn() }),
+}));
+
+import { IMPORT_TIMEOUT_MS, importQuotaManager } from "./importQuotaManager.js";
+
+let orgCounter = 0;
+let org: string;
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ // The manager is a singleton; a fresh organization keeps fallback locks from leaking between tests.
+ org = `org_${++orgCounter}`;
+ mocks.importLockAcquire.mockResolvedValue(true);
+ mocks.importLockRelease.mockResolvedValue(true);
+ // Imports are running unless a test says otherwise.
+ mocks.getImportById.mockResolvedValue({ completedAt: null });
+});
+
+describe("importQuotaManager lock", () => {
+ it("takes the shared lock for the import id and releases it by the same id", async () => {
+ expect(await importQuotaManager.startImport(org, "import_1")).toBe(true);
+ expect(mocks.importLockAcquire).toHaveBeenCalledWith(`import:lock:${org}`, "import_1", IMPORT_TIMEOUT_MS);
+
+ await importQuotaManager.completeImport(org, "import_1");
+ expect(mocks.importLockRelease).toHaveBeenCalledWith(`import:lock:${org}`, "import_1");
+ });
+
+ it("refuses a second import while another worker holds the lock", async () => {
+ mocks.importLockAcquire.mockResolvedValue(false);
+
+ expect(await importQuotaManager.startImport(org, "import_1")).toBe(false);
+ });
+
+ it("falls back to an in-process lock when Redis fails, without refusing the first import", async () => {
+ mocks.importLockAcquire.mockRejectedValue(new Error("redis down"));
+
+ expect(await importQuotaManager.startImport(org, "import_1")).toBe(true);
+ expect(await importQuotaManager.startImport(org, "import_2")).toBe(false);
+
+ // Only the import that holds the fallback lock releases it.
+ await importQuotaManager.completeImport(org, "import_other");
+ expect(await importQuotaManager.startImport(org, "import_2")).toBe(false);
+
+ await importQuotaManager.completeImport(org, "import_1");
+ expect(await importQuotaManager.startImport(org, "import_3")).toBe(true);
+ });
+
+ it("lets only one of two concurrent starts through while Redis is down", async () => {
+ mocks.importLockAcquire.mockRejectedValue(new Error("redis down"));
+
+ const results = await Promise.all([
+ importQuotaManager.startImport(org, "import_1"),
+ importQuotaManager.startImport(org, "import_2"),
+ ]);
+ expect(results.filter(Boolean)).toHaveLength(1);
+ });
+
+ it("keeps an import started while Redis was down exclusive after Redis recovers", async () => {
+ mocks.importLockAcquire.mockRejectedValueOnce(new Error("redis down"));
+ expect(await importQuotaManager.startImport(org, "import_1")).toBe(true);
+
+ // Redis is back and has never heard of import_1.
+ mocks.importLockAcquire.mockResolvedValue(true);
+ expect(await importQuotaManager.startImport(org, "import_2")).toBe(false);
+ expect(mocks.importLockAcquire).toHaveBeenCalledTimes(1);
+
+ await importQuotaManager.completeImport(org, "import_1");
+ expect(await importQuotaManager.startImport(org, "import_3")).toBe(true);
+ });
+
+ it("releases a fallback lock once another worker has finished that import", async () => {
+ mocks.importLockAcquire.mockRejectedValueOnce(new Error("redis down"));
+ expect(await importQuotaManager.startImport(org, "import_1")).toBe(true);
+
+ // import_1's final batch landed on another worker, which marked the row complete.
+ mocks.getImportById.mockResolvedValue({ completedAt: "2026-10-24 10:00:00" });
+ mocks.importLockAcquire.mockResolvedValue(true);
+ expect(await importQuotaManager.startImport(org, "import_2")).toBe(true);
+ });
+
+ it("keeps the fallback lock when the import row can't be read", async () => {
+ mocks.importLockAcquire.mockRejectedValueOnce(new Error("redis down"));
+ expect(await importQuotaManager.startImport(org, "import_1")).toBe(true);
+
+ mocks.getImportById.mockRejectedValue(new Error("postgres down"));
+ mocks.importLockAcquire.mockResolvedValue(true);
+ expect(await importQuotaManager.startImport(org, "import_2")).toBe(false);
+ });
+
+ it("keeps an import started through Redis exclusive when Redis goes down", async () => {
+ expect(await importQuotaManager.startImport(org, "import_1")).toBe(true);
+
+ mocks.importLockAcquire.mockRejectedValue(new Error("redis down"));
+ expect(await importQuotaManager.startImport(org, "import_2")).toBe(false);
+
+ await importQuotaManager.completeImport(org, "import_1");
+ expect(await importQuotaManager.startImport(org, "import_3")).toBe(true);
+ });
+
+ it("follows Redis while it is up, so an import another worker finished doesn't block this one", async () => {
+ expect(await importQuotaManager.startImport(org, "import_1")).toBe(true);
+
+ // import_1's last batch was served (and its Redis lock released) by a different worker.
+ expect(await importQuotaManager.startImport(org, "import_2")).toBe(true);
+ });
+
+ it("never throws when releasing fails", async () => {
+ mocks.importLockRelease.mockRejectedValue(new Error("redis down"));
+
+ await expect(importQuotaManager.completeImport(org, "import_1")).resolves.toBeUndefined();
+ });
+});
diff --git a/server/src/services/import/importQuotaManager.ts b/server/src/services/import/importQuotaManager.ts
index 514b1b6cc..8e9088ef5 100644
--- a/server/src/services/import/importQuotaManager.ts
+++ b/server/src/services/import/importQuotaManager.ts
@@ -1,31 +1,62 @@
-import cluster from "node:cluster";
import { ImportQuotaTracker } from "./importQuotaTracker.js";
import { IS_CLOUD } from "../../lib/const.js";
+import { createServiceLogger } from "../../lib/logger/logger.js";
+import { importLockAcquire, importLockRelease } from "../../db/redis/redis.js";
+import { getImportById } from "./importStatusManager.js";
// How long an import may run before it no longer counts as active. Imports are only marked
// complete by the client's final batch, so this is also when one whose client stopped early
// (tab closed, network lost) is treated as abandoned and can be deleted.
export const IMPORT_TIMEOUT_MS = 2 * 60 * 60 * 1000; // 2 hours
+const logger = createServiceLogger("import-quota");
+
interface CachedTracker {
tracker: ImportQuotaTracker;
- lastAccessed: number;
+ createdAt: number;
+}
+
+interface LocalLock {
+ owner: string;
+ startedAt: number;
+ // False for a lock taken while Redis was down: Redis never saw it, so only this record
+ // keeps a second import out once Redis is back.
+ inRedis: boolean;
}
+const lockKey = (organizationId: string) => `import:lock:${organizationId}`;
+
+// The lock is taken before the import row is written, so a missing row only means the import
+// is gone (deleted) once the lock is older than this.
+const MISSING_ROW_GRACE_MS = 60 * 1000;
+
+/**
+ * One import at a time per organization, and the trackers that enforce its monthly quota.
+ *
+ * The lock lives in Redis so every cluster worker sees it: the batches and the delete for an
+ * import can land on any worker, not just the one that started it. It belongs to the import's
+ * id, so finishing or deleting some other import never releases it, and it expires after
+ * IMPORT_TIMEOUT_MS like an abandoned import does. If Redis is unavailable the lock falls back
+ * to this process, as it worked before, so imports are never refused because of Redis.
+ *
+ * Every lock this process takes is also recorded locally, so switching between Redis and the
+ * fallback never lets a second import past one this process already started.
+ */
class ImportQuotaManager {
private trackers = new Map<string, CachedTracker>();
- private activeImports = new Map<string, number>(); // stores startedAt timestamp
+ private localLocks = new Map<string, LocalLock>();
private trackerCreationLocks = new Map<string, Promise<ImportQuotaTracker>>(); // prevents duplicate tracker creation
- private readonly CONCURRENT_IMPORT_LIMIT = 1;
+ // Not sliding: the tracker's ClickHouse baseline must stay younger than the Redis quota
+ // counts it seeds (IMPORT_QUOTA_TTL_MS), or an expired count could be reseeded from usage
+ // read before events this organization has since imported.
private readonly TRACKER_TTL_MS = 30 * 60 * 1000; // 30 minutes
async getTracker(organizationId: string): Promise<ImportQuotaTracker> {
const now = Date.now();
const cached = this.trackers.get(organizationId);
- if (cached && now - cached.lastAccessed < this.TRACKER_TTL_MS) {
- cached.lastAccessed = now;
+ if (cached && now - cached.createdAt < this.TRACKER_TTL_MS) {
return cached.tracker;
}
@@ -39,7 +70,7 @@ class ImportQuotaManager {
const creationPromise = (async () => {
try {
const tracker = await ImportQuotaTracker.create(organizationId);
- this.trackers.set(organizationId, { tracker, lastAccessed: now });
+ this.trackers.set(organizationId, { tracker, createdAt: now });
return tracker;
} finally {
this.trackerCreationLocks.delete(organizationId);
@@ -50,24 +81,74 @@ class ImportQuotaManager {
return await creationPromise;
}
- startImport(organizationId: string): boolean {
- if (!IS_CLOUD) return true;
+ private liveLocalLock(organizationId: string): LocalLock | undefined {
+ const local = this.localLocks.get(organizationId);
+ return local && Date.now() - local.startedAt < IMPORT_TIMEOUT_MS ? local : undefined;
+ }
- const now = Date.now();
- const startedAt = this.activeImports.get(organizationId);
+ // A local record may outlive its import: the final batch or the delete can land on another
+ // worker, which can only clear its own record. The import row says whether it is still
+ // running. If the lookup fails the record keeps holding, which errs on the side of refusing.
+ private async heldLocalLock(organizationId: string): Promise<LocalLock | undefined> {
+ const local = this.liveLocalLock(organizationId);
+ if (!local) return undefined;
+ try {
+ const row = await getImportById(local.owner);
+ const finished = row ? row.completedAt !== null : Date.now() - local.startedAt > MISSING_ROW_GRACE_MS;
+ if (finished) {
+ if (this.localLocks.get(organizationId) === local) this.localLocks.delete(organizationId);
+ return undefined;
+ }
+ } catch (error) {
+ logger.warn({ err: error, organizationId }, "Could not check the import holding the local lock");
+ }
+ return local;
+ }
- // If an import exists and hasn't timed out, limit reached
- if (startedAt && now - startedAt < IMPORT_TIMEOUT_MS && this.CONCURRENT_IMPORT_LIMIT === 1) {
+ /**
+ * Takes the organization's import lock for `importId`, generated before the import row so the
+ * lock has one owner throughout. Returns false while another import is active.
+ */
+ async startImport(organizationId: string, importId: string): Promise<boolean> {
+ if (!IS_CLOUD) return true;
+
+ // Taken while Redis was down, so Redis can't refuse on its behalf.
+ if (this.liveLocalLock(organizationId)?.inRedis === false && (await this.heldLocalLock(organizationId))) {
return false;
}
- // Otherwise start a new import
- this.activeImports.set(organizationId, now);
- return true;
+ let acquired: boolean;
+ try {
+ acquired = await importLockAcquire(lockKey(organizationId), importId, IMPORT_TIMEOUT_MS);
+ } catch (error) {
+ logger.warn({ err: error, organizationId }, "Import lock Redis call failed; locking in this process only");
+ // Redis's locks can't be seen now, but this process's own still count, including ones
+ // taken through Redis. Re-read after the lookup so concurrent starts can't both pass.
+ if (await this.heldLocalLock(organizationId)) return false;
+ if (this.liveLocalLock(organizationId)) return false;
+ this.localLocks.set(organizationId, { owner: importId, startedAt: Date.now(), inRedis: false });
+ return true;
+ }
+
+ // Redis decides while it's up: a local record it no longer holds was released by whichever
+ // worker finished that import.
+ if (acquired) this.localLocks.set(organizationId, { owner: importId, startedAt: Date.now(), inRedis: true });
+ return acquired;
}
- completeImport(organizationId: string): void {
- this.activeImports.delete(organizationId);
+ /** Releases the organization's import lock, in Redis and here, if `importId` still holds it. Never throws. */
+ async completeImport(organizationId: string, importId: string): Promise<void> {
+ if (!IS_CLOUD) return;
+
+ if (this.localLocks.get(organizationId)?.owner === importId) {
+ this.localLocks.delete(organizationId);
+ }
+
+ try {
+ await importLockRelease(lockKey(organizationId), importId);
+ } catch (error) {
+ logger.warn({ err: error, organizationId }, "Could not release the import lock; it expires on its own");
+ }
}
cleanup(): void {
@@ -75,15 +156,15 @@ class ImportQuotaManager {
// Cleanup stale trackers
for (const [orgId, cached] of this.trackers) {
- if (now - cached.lastAccessed > this.TRACKER_TTL_MS) {
+ if (now - cached.createdAt > this.TRACKER_TTL_MS) {
this.trackers.delete(orgId);
}
}
- // Cleanup timed-out imports
- for (const [orgId, startedAt] of this.activeImports) {
- if (now - startedAt > IMPORT_TIMEOUT_MS) {
- this.activeImports.delete(orgId);
+ // Cleanup timed-out local lock records
+ for (const [orgId, lock] of this.localLocks) {
+ if (now - lock.startedAt > IMPORT_TIMEOUT_MS) {
+ this.localLocks.delete(orgId);
}
}
}
@@ -91,7 +172,8 @@ class ImportQuotaManager {
export const importQuotaManager = new ImportQuotaManager();
-// Background cleanup loop
-if (IS_CLOUD && !cluster.isWorker) {
- setInterval(() => importQuotaManager.cleanup(), 15 * 60 * 1000);
+// Background cleanup loop. Trackers and fallback locks live in whichever process serves
+// imports, which is every cluster worker, so each process cleans up its own.
+if (IS_CLOUD) {
+ setInterval(() => importQuotaManager.cleanup(), 15 * 60 * 1000).unref();
}
diff --git a/server/src/services/import/importQuotaTracker.test.ts b/server/src/services/import/importQuotaTracker.test.ts
index 502c45daf..8a855ead6 100644
--- a/server/src/services/import/importQuotaTracker.test.ts
+++ b/server/src/services/import/importQuotaTracker.test.ts
@@ -3,6 +3,7 @@ import { ImportQuotaTracker } from "./importQuotaTracker.js";
import { db } from "../../db/postgres/postgres.js";
import { getBestSubscription } from "../../lib/subscriptionUtils.js";
import { processResults } from "../../api/analytics/utils/utils.js";
+import { importQuotaReserve } from "../../db/redis/redis.js";
vi.mock("../../db/clickhouse/clickhouse.js", () => ({
clickhouse: { query: vi.fn() },
@@ -24,10 +25,19 @@ vi.mock("../../api/analytics/utils/utils.js", () => ({
processResults: vi.fn(),
}));
+vi.mock("../../db/redis/redis.js", () => ({
+ importQuotaReserve: vi.fn(),
+}));
+
// The constructor is private; tests build trackers directly with injected state
// instead of going through the DB/ClickHouse-backed static create().
-function makeTracker(usage: Record<string, number>, limit: number, oldestAllowedMonth: string): ImportQuotaTracker {
- return new (ImportQuotaTracker as any)(new Map(Object.entries(usage)), limit, oldestAllowedMonth);
+function makeTracker(
+ usage: Record<string, number>,
+ limit: number,
+ oldestAllowedMonth: string,
+ organizationId: string | null = null
+): ImportQuotaTracker {
+ return new (ImportQuotaTracker as any)(new Map(Object.entries(usage)), limit, oldestAllowedMonth, organizationId);
}
// Drizzle query builders are thenable; create() awaits `.limit(1)` for the org
@@ -184,6 +194,59 @@ describe("ImportQuotaTracker", () => {
});
});
+ describe("reserveBatch", () => {
+ it("admits what the shared Redis counts grant, per month and in batch order", async () => {
+ const tracker = makeTracker({ "202405": 3 }, 5, "202312", "org-1");
+ vi.mocked(importQuotaReserve).mockResolvedValueOnce([1, 2]);
+
+ const result = await tracker.reserveBatch([
+ "2024-05-01T10:00:00.000Z",
+ "2024-06-01T10:00:00.000Z",
+ "2024-05-02T10:00:00.000Z",
+ "garbage",
+ "2024-06-02T10:00:00.000Z",
+ "2024-06-03T10:00:00.000Z",
+ ]);
+
+ expect(importQuotaReserve).toHaveBeenCalledWith("import:quota:org-1", 5, expect.any(Number), [
+ { month: "202405", baseline: 3, requested: 2 },
+ { month: "202406", baseline: 0, requested: 3 },
+ ]);
+ expect(result).toEqual([0, 1, 4]);
+ });
+
+ it("seeds later months from local usage, including earlier grants", async () => {
+ const tracker = makeTracker({}, 5, "202312", "org-1");
+ vi.mocked(importQuotaReserve).mockResolvedValueOnce([2]).mockResolvedValueOnce([0]);
+
+ await tracker.reserveBatch(["2024-06-01T10:00:00.000Z", "2024-06-02T10:00:00.000Z"]);
+ await tracker.reserveBatch(["2024-06-03T10:00:00.000Z"]);
+
+ expect(vi.mocked(importQuotaReserve).mock.calls[1][3]).toEqual([{ month: "202406", baseline: 2, requested: 1 }]);
+ });
+
+ it("falls back to this process's counts when Redis fails", async () => {
+ const tracker = makeTracker({ "202406": 4 }, 5, "202312", "org-1");
+ vi.mocked(importQuotaReserve).mockRejectedValueOnce(new Error("redis down"));
+
+ expect(await tracker.reserveBatch(["2024-06-01T10:00:00.000Z", "2024-06-02T10:00:00.000Z"])).toEqual([0]);
+ });
+
+ it("skips Redis when nothing in the batch is importable", async () => {
+ const tracker = makeTracker({}, 5, "202312", "org-1");
+
+ expect(await tracker.reserveBatch(["garbage", "2099-01-01T00:00:00.000Z"])).toEqual([]);
+ expect(importQuotaReserve).not.toHaveBeenCalled();
+ });
+
+ it("never calls Redis for unlimited (self-hosted) trackers", async () => {
+ const tracker = makeTracker({}, Infinity, "190001", "org-1");
+
+ expect(await tracker.reserveBatch(["2024-06-01T10:00:00.000Z"])).toEqual([0]);
+ expect(importQuotaReserve).not.toHaveBeenCalled();
+ });
+ });
+
describe("getHistoricalWindowMonths (via create)", () => {
async function createTrackerForSubscription(subscription: Record<string, unknown>) {
mockDbForCreate();
diff --git a/server/src/services/import/importQuotaTracker.ts b/server/src/services/import/importQuotaTracker.ts
index 15e25e7f0..6d18e75a9 100644
--- a/server/src/services/import/importQuotaTracker.ts
+++ b/server/src/services/import/importQuotaTracker.ts
@@ -6,6 +6,15 @@ import { eq } from "drizzle-orm";
import { processResults } from "../../api/analytics/utils/utils.js";
import { getBestSubscription, type SubscriptionInfo } from "../../lib/subscriptionUtils.js";
import { IS_CLOUD } from "../../lib/const.js";
+import { createServiceLogger } from "../../lib/logger/logger.js";
+import { importQuotaReserve } from "../../db/redis/redis.js";
+
+const logger = createServiceLogger("import-quota");
+
+// How long an organization's shared quota counts live in Redis after its last batch. It must
+// outlast a cached tracker (see importQuotaManager): when the counts expire, a new tracker
+// reseeds them from ClickHouse, which by then holds every event reserved before.
+export const IMPORT_QUOTA_TTL_MS = 2 * 60 * 60 * 1000;
/**
* Get the number of months of historical data allowed for import based on subscription tier
@@ -33,11 +42,19 @@ export class ImportQuotaTracker {
private monthlyUsage: Map<string, number>;
private readonly monthlyLimit: number;
private readonly oldestAllowedMonth: string;
-
- private constructor(monthlyUsage: Map<string, number>, monthlyLimit: number, oldestAllowedMonth: string) {
+ // Keys the shared Redis counts; null keeps all accounting in this process.
+ private readonly organizationId: string | null;
+
+ private constructor(
+ monthlyUsage: Map<string, number>,
+ monthlyLimit: number,
+ oldestAllowedMonth: string,
+ organizationId: string | null = null
+ ) {
this.monthlyUsage = monthlyUsage;
this.monthlyLimit = monthlyLimit;
this.oldestAllowedMonth = oldestAllowedMonth;
+ this.organizationId = organizationId;
}
static async create(organizationId: string): Promise<ImportQuotaTracker> {
@@ -71,12 +88,12 @@ export class ImportQuotaTracker {
const siteIds = siteRecords.map(s => s.siteId);
if (siteIds.length === 0) {
- return new ImportQuotaTracker(new Map(), monthlyLimit, oldestAllowedMonth);
+ return new ImportQuotaTracker(new Map(), monthlyLimit, oldestAllowedMonth, organizationId);
}
const monthlyUsage = await ImportQuotaTracker.queryMonthlyUsage(siteIds, oldestAllowedDate.toFormat("yyyy-MM-dd"));
- return new ImportQuotaTracker(monthlyUsage, monthlyLimit, oldestAllowedMonth);
+ return new ImportQuotaTracker(monthlyUsage, monthlyLimit, oldestAllowedMonth, organizationId);
}
private static async queryMonthlyUsage(siteIds: number[], startDate: string): Promise<Map<string, number>> {
@@ -154,57 +171,122 @@ export class ImportQuotaTracker {
}
/**
- * Atomically check and reserve quota for a batch of events.
- * This method prevents race conditions by checking all events in a batch
- * and atomically updating usage counts for all months at once.
- *
- * @param timestamps - Array of event timestamps to check
- * @returns Array of indices indicating which events can be imported
+ * The month (yyyyMM) each timestamp counts against, or null when it can't be imported:
+ * malformed, in the future, or older than the plan's historical window. Self-hosted
+ * (unlimited) has no window, so any past timestamp maps to "".
*/
- canImportBatch(timestamps: string[]): number[] {
- const allowedIndices: number[] = [];
- const monthlyIncrements = new Map<string, number>();
+ private monthsFor(timestamps: string[]): Array<string | null> {
const now = DateTime.utc();
const unlimited = this.monthlyLimit === Infinity;
- for (let i = 0; i < timestamps.length; i++) {
- const timestamp = timestamps[i];
+ return timestamps.map(timestamp => {
// Mappers emit the explicit-UTC ISO form that ClickHouse is given.
const dt = DateTime.fromISO(timestamp, { zone: "utc", setZone: true });
+ if (!dt.isValid || dt > now) return null;
+ if (unlimited) return "";
- if (!dt.isValid || dt > now) {
- continue;
- }
+ const month = dt.toFormat("yyyyMM");
+ return month < this.oldestAllowedMonth ? null : month;
+ });
+ }
- if (unlimited) {
- // Self-hosted: no quota accounting and no tier-based historical window,
- // but malformed and future timestamps are still rejected.
- allowedIndices.push(i);
- continue;
- }
+ private static countPerMonth(months: Array<string | null>): Map<string, number> {
+ const counts = new Map<string, number>();
+ for (const month of months) {
+ if (month !== null) counts.set(month, (counts.get(month) ?? 0) + 1);
+ }
+ return counts;
+ }
- const month = dt.toFormat("yyyyMM");
- if (month < this.oldestAllowedMonth) {
- continue;
+ /** Indices of the events admitted: the first `granted` events of each month, in batch order. */
+ private static admit(months: Array<string | null>, granted: Map<string, number>): number[] {
+ const admitted = new Map<string, number>();
+ const allowedIndices: number[] = [];
+
+ months.forEach((month, index) => {
+ if (month === null) return;
+ const count = admitted.get(month) ?? 0;
+ if (count < (granted.get(month) ?? 0)) {
+ allowedIndices.push(index);
+ admitted.set(month, count + 1);
}
+ });
- const currentUsage = this.monthlyUsage.get(month) || 0;
- const incrementInBatch = monthlyIncrements.get(month) || 0;
- const totalUsage = currentUsage + incrementInBatch;
+ return allowedIndices;
+ }
- if (totalUsage < this.monthlyLimit) {
- allowedIndices.push(i);
- monthlyIncrements.set(month, incrementInBatch + 1);
- }
+ private addUsage(granted: Map<string, number>): void {
+ for (const [month, grant] of granted) {
+ this.monthlyUsage.set(month, (this.monthlyUsage.get(month) ?? 0) + grant);
}
+ }
- // Atomically apply all increments at once
- for (const [month, increment] of monthlyIncrements) {
- const current = this.monthlyUsage.get(month) || 0;
- this.monthlyUsage.set(month, current + increment);
+ /**
+ * Check and reserve quota for a batch of events using only this process's accounting.
+ * Every month in the batch is granted at most what's left of its limit, all at once,
+ * so a batch can't overshoot.
+ *
+ * With several cluster workers this only sees reservations made on this worker; use
+ * reserveBatch, which falls back to this when Redis is unavailable.
+ *
+ * @param timestamps - Array of event timestamps to check
+ * @returns Array of indices indicating which events can be imported
+ */
+ canImportBatch(timestamps: string[]): number[] {
+ const months = this.monthsFor(timestamps);
+ const requested = ImportQuotaTracker.countPerMonth(months);
+ const granted = new Map<string, number>();
+
+ for (const [month, count] of requested) {
+ // Self-hosted: no quota accounting, but malformed and future timestamps are still rejected.
+ const remaining = this.monthlyLimit - (this.monthlyUsage.get(month) ?? 0);
+ granted.set(month, Math.max(0, Math.min(count, remaining)));
}
- return allowedIndices;
+ if (this.monthlyLimit !== Infinity) this.addUsage(granted);
+ return ImportQuotaTracker.admit(months, granted);
+ }
+
+ /**
+ * Check and reserve quota for a batch against the organization's counts in Redis, which
+ * every cluster worker shares, so parallel batches can't each spend the same remaining
+ * quota. Falls back to this process's accounting if Redis fails, so imports never stall.
+ */
+ async reserveBatch(timestamps: string[]): Promise<number[]> {
+ if (this.monthlyLimit === Infinity || !this.organizationId) {
+ return this.canImportBatch(timestamps);
+ }
+
+ const months = this.monthsFor(timestamps);
+ const requests = [...ImportQuotaTracker.countPerMonth(months)].map(([month, requested]) => ({
+ month,
+ // ClickHouse usage when this tracker was built plus anything this process has reserved
+ // since. The shared count adopts it when higher, e.g. once live traffic has landed.
+ baseline: this.monthlyUsage.get(month) ?? 0,
+ requested,
+ }));
+ if (requests.length === 0) return [];
+
+ let grantedCounts: number[];
+ try {
+ grantedCounts = await importQuotaReserve(
+ `import:quota:${this.organizationId}`,
+ this.monthlyLimit,
+ IMPORT_QUOTA_TTL_MS,
+ requests
+ );
+ } catch (error) {
+ logger.warn(
+ { err: error, organizationId: this.organizationId },
+ "Import quota Redis call failed; using this process's counts"
+ );
+ return this.canImportBatch(timestamps);
+ }
+
+ const granted = new Map(requests.map((request, index) => [request.month, grantedCounts[index]]));
+ // Mirror the reservation so the in-process fallback stays close if Redis drops out later.
+ this.addUsage(granted);
+ return ImportQuotaTracker.admit(months, granted);
}
getOldestAllowedMonth(): string {
diff --git a/server/src/services/import/importStatusManager.ts b/server/src/services/import/importStatusManager.ts
index 7c0d35067..f0fcdb261 100644
--- a/server/src/services/import/importStatusManager.ts
+++ b/server/src/services/import/importStatusManager.ts
@@ -6,6 +6,7 @@ import { DateTime } from "luxon";
export type SelectImportStatus = typeof importStatus.$inferSelect;
export async function createImport(data: {
+ importId: string;
siteId: number;
organizationId: string;
platform: (typeof importPlatforms)[number];
@@ -13,6 +14,7 @@ export async function createImport(data: {
const [result] = await db
.insert(importStatus)
.values({
+ importId: data.importId,
siteId: data.siteId,
organizationId: data.organizationId,
platform: data.platform,
diff --git a/server/src/services/replay/replayBotDetection.test.ts b/server/src/services/replay/replayBotDetection.test.ts
new file mode 100644
index 000000000..58a691258
--- /dev/null
+++ b/server/src/services/replay/replayBotDetection.test.ts
@@ -0,0 +1,61 @@
+import { CLIENT_BOT_SIGNAL_MASKS } from "@rybbit/shared";
+import { describe, expect, it } from "vitest";
+import { isBlockedReplayBot, type ReplayBotCheckInput } from "./replayBotDetection.js";
+
+const CHROME_UA =
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36";
+
+const browser: ReplayBotCheckInput = {
+ blockBots: true,
+ isMobileSite: false,
+ headers: {
+ "user-agent": CHROME_UA,
+ accept: "*/*",
+ "accept-language": "en-US,en;q=0.9",
+ "accept-encoding": "gzip, deflate, br",
+ "sec-fetch-site": "cross-site",
+ "sec-fetch-mode": "cors",
+ },
+ userAgent: CHROME_UA,
+ ipAddress: "198.51.100.10",
+ screenWidth: 1920,
+ screenHeight: 1080,
+ lookupAsn: () => null,
+};
+
+describe("isBlockedReplayBot", () => {
+ it("passes an ordinary browser", () => {
+ expect(isBlockedReplayBot(browser)).toBe(false);
+ });
+
+ it("never blocks when the Site doesn't block bots", () => {
+ expect(isBlockedReplayBot({ ...browser, blockBots: false, userAgent: "GPTBot/1.0", clientBotSignalMask: 1 })).toBe(
+ false
+ );
+ });
+
+ it("convicts on the user agent and header layers of a web Site only", () => {
+ const headless = { ...browser, userAgent: "Mozilla/5.0 HeadlessChrome/120" };
+ expect(isBlockedReplayBot(headless)).toBe(true);
+ expect(isBlockedReplayBot({ ...headless, isMobileSite: true })).toBe(false);
+ expect(isBlockedReplayBot({ ...browser, headers: { "user-agent": CHROME_UA } })).toBe(true);
+ });
+
+ it("convicts on strong client signals, sent or re-derived from the screen", () => {
+ expect(isBlockedReplayBot({ ...browser, clientBotSignalMask: CLIENT_BOT_SIGNAL_MASKS.automationApi })).toBe(true);
+ expect(isBlockedReplayBot({ ...browser, screenWidth: 1280, screenHeight: 1200 })).toBe(true);
+ });
+
+ it("does not convict on weak client signals alone", () => {
+ const weak =
+ CLIENT_BOT_SIGNAL_MASKS.emptyPlugins |
+ CLIENT_BOT_SIGNAL_MASKS.swiftShader |
+ CLIENT_BOT_SIGNAL_MASKS.missingChrome;
+ expect(isBlockedReplayBot({ ...browser, clientBotSignalMask: weak })).toBe(false);
+ });
+
+ it("convicts curated bot-provider ASNs but not generic hosting", () => {
+ expect(isBlockedReplayBot({ ...browser, lookupAsn: () => ({ asn: 4167, organization: "Anthropic" }) })).toBe(true);
+ expect(isBlockedReplayBot({ ...browser, lookupAsn: () => ({ asn: 16509, organization: "Amazon" }) })).toBe(false);
+ });
+});
diff --git a/server/src/services/replay/replayBotDetection.ts b/server/src/services/replay/replayBotDetection.ts
new file mode 100644
index 000000000..9f3ebdcb5
--- /dev/null
+++ b/server/src/services/replay/replayBotDetection.ts
@@ -0,0 +1,78 @@
+import {
+ CLIENT_BOT_SIGNAL_MASKS,
+ getScreenDimensionSignals,
+ scoreFromMask,
+ STRONG_CLIENT_BOT_SIGNAL_BITS,
+} from "@rybbit/shared";
+import type { IncomingHttpHeaders } from "http";
+import { lookupAsn, type AsnLookup } from "../../db/geolocation/asn.js";
+import { classifyBotAsn } from "../tracker/botBlocking/botProviderAsns.js";
+import { CLIENT_BOT_SCORE_THRESHOLD } from "../tracker/botBlocking/config.js";
+import { detectBot } from "../tracker/botBlocking/headerHeuristics.js";
+import { classifyStaleBrowserVersion } from "../tracker/botBlocking/staleBrowserVersion.js";
+import { classifyUA } from "../tracker/botBlocking/uaBots/index.js";
+
+export interface ReplayBotCheckInput {
+ /** The Site's setting. Off means replays are stored whatever the visitor is. */
+ blockBots: boolean;
+ /** App/mobile Site: the browser-shaped UA and header layers are skipped. */
+ isMobileSite: boolean;
+ headers: IncomingHttpHeaders;
+ userAgent: string;
+ ipAddress: string;
+ screenWidth?: number;
+ screenHeight?: number;
+ /** The tracker's `_bsm`, sent with the batch as it is with events. */
+ clientBotSignalMask?: number;
+ lookupAsn?: AsnLookup;
+}
+
+/**
+ * Whether a replay batch comes from a visitor that event ingestion keeps out of
+ * `events` as a bot, so its replay should not be stored either.
+ *
+ * This is `checkBotBlocking`'s decision for the layers that judge a request on
+ * its own, with the same rules: user agent (stale browser versions included)
+ * and header heuristics on web Sites, strong client signals, and curated
+ * bot-provider ASNs. Evidence that only corroborates there (weak client
+ * signals, generic hosting ASNs) cannot convict here either.
+ *
+ * Rate anomaly is left out. Its sliding-window counters are fed by tracked
+ * events; counting replay batches would cost a Redis round-trip per batch and
+ * push real visitors toward its thresholds. Nothing is recorded to
+ * `bot_observations` or the detection stats, which describe `/track` traffic.
+ */
+export function isBlockedReplayBot(input: ReplayBotCheckInput): boolean {
+ if (!input.blockBots) {
+ return false;
+ }
+
+ const { userAgent } = input;
+
+ if (!input.isMobileSite) {
+ if (classifyUA(userAgent).isBot || classifyStaleBrowserVersion(userAgent).isStale) {
+ return true;
+ }
+ if (detectBot(input.headers, userAgent).isBot) {
+ return true;
+ }
+ }
+
+ // Client signals: the screen rules are re-derived from the reported
+ // dimensions exactly as for events, then only the strong bits may convict.
+ let mask = input.clientBotSignalMask ?? 0;
+ if (input.screenWidth !== undefined || input.screenHeight !== undefined) {
+ for (const signal of getScreenDimensionSignals(input.screenWidth ?? NaN, input.screenHeight ?? NaN, userAgent)) {
+ mask |= CLIENT_BOT_SIGNAL_MASKS[signal];
+ }
+ }
+ if (scoreFromMask(mask & STRONG_CLIENT_BOT_SIGNAL_BITS) >= CLIENT_BOT_SCORE_THRESHOLD) {
+ return true;
+ }
+
+ if (!input.ipAddress) {
+ return false;
+ }
+ const asnInfo = (input.lookupAsn ?? lookupAsn)(input.ipAddress);
+ return classifyBotAsn(asnInfo?.asn).source === "curated_bot_provider";
+}
diff --git a/server/src/services/replay/sessionReplayIngestService.test.ts b/server/src/services/replay/sessionReplayIngestService.test.ts
index e93822365..7c438f52e 100644
--- a/server/src/services/replay/sessionReplayIngestService.test.ts
+++ b/server/src/services/replay/sessionReplayIngestService.test.ts
@@ -4,8 +4,9 @@ import type { RecordSessionReplayRequest } from "../../types/sessionReplay.js";
const mocks = vi.hoisted(() => ({
generateUserId: vi.fn(),
+ generateUserIdFromClientId: vi.fn(),
insert: vi.fn(),
- updateSession: vi.fn(),
+ resolveReplaySession: vi.fn(),
chargeReplaySessionBudget: vi.fn(),
storeBatch: vi.fn(),
r2Enabled: false,
@@ -19,13 +20,14 @@ vi.mock("../../db/clickhouse/clickhouse.js", () => ({
vi.mock("../sessions/sessionsService.js", () => ({
sessionsService: {
- updateSession: mocks.updateSession,
+ resolveReplaySession: mocks.resolveReplaySession,
},
}));
vi.mock("../userId/userIdService.js", () => ({
userIdService: {
generateUserId: mocks.generateUserId,
+ generateUserIdFromClientId: mocks.generateUserIdFromClientId,
},
}));
@@ -64,7 +66,7 @@ describe("SessionReplayIngestService identity", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.generateUserId.mockResolvedValue("shared-fingerprint");
- mocks.updateSession.mockImplementation(
+ mocks.resolveReplaySession.mockImplementation(
async ({ userId, identifiedUserId }: { userId: string; identifiedUserId?: string }) => ({
sessionId: `session-${userId}-${identifiedUserId || "anonymous"}`,
})
@@ -80,15 +82,17 @@ describe("SessionReplayIngestService identity", () => {
await service.recordEvents(42, replayRequest("employee-alice"), requestMeta);
await service.recordEvents(42, replayRequest("employee-bob"), requestMeta);
- expect(mocks.updateSession).toHaveBeenNthCalledWith(1, {
+ expect(mocks.resolveReplaySession).toHaveBeenNthCalledWith(1, {
userId: "shared-fingerprint",
identifiedUserId: "employee-alice",
siteId: 42,
+ hasFullSnapshot: true,
});
- expect(mocks.updateSession).toHaveBeenNthCalledWith(2, {
+ expect(mocks.resolveReplaySession).toHaveBeenNthCalledWith(2, {
userId: "shared-fingerprint",
identifiedUserId: "employee-bob",
siteId: 42,
+ hasFullSnapshot: true,
});
const insertedRows = mocks.insert.mock.calls.flatMap(call => call[0].values);
@@ -104,12 +108,84 @@ describe("SessionReplayIngestService identity", () => {
await service.recordEvents(42, replayRequest(""), requestMeta);
- expect(mocks.generateUserId).toHaveBeenCalledWith(requestMeta.ipAddress, requestMeta.userAgent, 42);
- expect(mocks.updateSession).toHaveBeenCalledWith({
+ expect(mocks.generateUserId).toHaveBeenCalledWith(
+ requestMeta.ipAddress,
+ requestMeta.userAgent,
+ 42,
+ expect.objectContaining({ saltUserIds: undefined })
+ );
+ expect(mocks.resolveReplaySession).toHaveBeenCalledWith({
userId: "shared-fingerprint",
identifiedUserId: "",
siteId: 42,
+ hasFullSnapshot: true,
+ });
+ });
+
+ it("hashes the persistent client id, as event ingestion does, instead of the fingerprint", async () => {
+ mocks.generateUserIdFromClientId.mockResolvedValue("client-hash");
+ const receivedAt = new Date("2026-01-01T23:59:59.900Z");
+ const service = new SessionReplayIngestService();
+
+ await service.recordEvents(42, replayRequest(""), {
+ ...requestMeta,
+ anonymousId: "client-123",
+ saltUserIds: false,
+ receivedAt,
+ });
+
+ expect(mocks.generateUserId).not.toHaveBeenCalled();
+ expect(mocks.generateUserIdFromClientId).toHaveBeenCalledWith("client-123", 42, {
+ saltUserIds: false,
+ receivedAt,
+ });
+ expect(mocks.resolveReplaySession).toHaveBeenCalledWith({
+ userId: "client-hash",
+ identifiedUserId: "",
+ siteId: 42,
+ hasFullSnapshot: true,
+ });
+ expect(new Set(mocks.insert.mock.calls.flatMap(call => call[0].values).map(row => row.user_id))).toEqual(
+ new Set(["client-hash"])
+ );
+ });
+});
+
+describe("SessionReplayIngestService session rollover", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.generateUserId.mockResolvedValue("fingerprint");
+ mocks.insert.mockResolvedValue(undefined);
+ mocks.chargeReplaySessionBudget.mockResolvedValue({ accepted: true, usedBytes: 0 });
+ mocks.r2Enabled = false;
+ });
+
+ it("tells session resolution whether the batch carries a full snapshot", async () => {
+ mocks.resolveReplaySession.mockResolvedValue({ sessionId: "session-1", needsFullSnapshot: false });
+ const service = new SessionReplayIngestService();
+ const batch = (types: (number | string)[]) => ({
+ userId: "",
+ events: types.map(type => ({ type, data: {}, timestamp: Date.now() })),
});
+
+ await service.recordEvents(42, batch([3, 3]), requestMeta);
+ await service.recordEvents(42, batch([3, "4", "2", 3]), requestMeta);
+
+ expect(mocks.resolveReplaySession.mock.calls.map(([args]) => args.hasFullSnapshot)).toEqual([false, true]);
+ });
+
+ it("stores the batch where resolution put it and passes on its snapshot request", async () => {
+ mocks.resolveReplaySession.mockResolvedValue({ sessionId: "session-previous", needsFullSnapshot: true });
+ const service = new SessionReplayIngestService();
+
+ const result = await service.recordEvents(
+ 42,
+ { userId: "", events: [{ type: 3, data: {}, timestamp: Date.now() }] },
+ requestMeta
+ );
+
+ expect(result).toEqual({ capped: false, needsFullSnapshot: true });
+ expect(mocks.insert.mock.calls[0][0].values[0].session_id).toBe("session-previous");
});
});
@@ -117,7 +193,7 @@ describe("SessionReplayIngestService size cap", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.generateUserId.mockResolvedValue("fingerprint");
- mocks.updateSession.mockResolvedValue({ sessionId: "session-1" });
+ mocks.resolveReplaySession.mockResolvedValue({ sessionId: "session-1" });
mocks.insert.mockResolvedValue(undefined);
mocks.storeBatch.mockResolvedValue("r2-key");
mocks.r2Enabled = true;
@@ -172,7 +248,7 @@ describe("SessionReplayIngestService batch serialization", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.generateUserId.mockResolvedValue("fingerprint");
- mocks.updateSession.mockResolvedValue({ sessionId: "session-1" });
+ mocks.resolveReplaySession.mockResolvedValue({ sessionId: "session-1" });
mocks.insert.mockResolvedValue(undefined);
mocks.chargeReplaySessionBudget.mockResolvedValue({ accepted: true, usedBytes: 0 });
mocks.storeBatch.mockResolvedValue("r2-key");
diff --git a/server/src/services/replay/sessionReplayIngestService.ts b/server/src/services/replay/sessionReplayIngestService.ts
index 789545721..750d87fe7 100644
--- a/server/src/services/replay/sessionReplayIngestService.ts
+++ b/server/src/services/replay/sessionReplayIngestService.ts
@@ -1,5 +1,6 @@
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { toClickHouseDateTime } from "../../db/clickhouse/dateTime.js";
+import type { AsnLookup } from "../../db/geolocation/asn.js";
import { RecordSessionReplayRequest } from "../../types/sessionReplay.js";
import { createServiceLogger } from "../../lib/logger/logger.js";
import { correctReplayClockSkew } from "./replayClockSkew.js";
@@ -25,13 +26,32 @@ type BatchStats = {
export type RecordEventsResult = {
/** The session is over its replay size cap; nothing from this batch was stored. */
capped: boolean;
+ /**
+ * The recorder should take a full snapshot: the replay is waiting to follow
+ * the visitor's new session, or has none where it is.
+ */
+ needsFullSnapshot?: boolean;
};
+// rrweb's EventType.FullSnapshot: the page's DOM, which playback starts from.
+const FULL_SNAPSHOT_EVENT_TYPE = 2;
+
export interface RequestMetadata {
userAgent: string;
ipAddress: string;
origin: string;
referrer: string;
+ /**
+ * Persistent client id sent with the batch. Like `anonymous_id` on `/track`,
+ * it replaces the IP + user agent fingerprint, so replay and events agree on
+ * the visitor and therefore on the session.
+ */
+ anonymousId?: string;
+ /** The Site's salting setting, from the configuration the handler loaded. */
+ saltUserIds?: boolean;
+ /** When the batch arrived: picks a salted fingerprint's day, as on `/track`. */
+ receivedAt?: Date;
+ lookupAsn?: AsnLookup;
}
/**
@@ -69,12 +89,16 @@ export class SessionReplayIngestService {
logger.warn({ siteId, skewMs, eventCount: events.length }, "Corrected replay clock skew");
}
- // Always generate device fingerprint (anonymous user ID) server-side
- const deviceFingerprint = await userIdService.generateUserId(
- requestMeta?.ipAddress || "",
- requestMeta?.userAgent || "",
- siteId
- );
+ // The anonymous user ID, derived exactly as event ingestion derives it
+ // (createBasePayload): from the persistent client id when one is sent,
+ // otherwise from the IP + user agent fingerprint.
+ const identityOptions = { saltUserIds: requestMeta?.saltUserIds, receivedAt: requestMeta?.receivedAt };
+ const deviceFingerprint = requestMeta?.anonymousId
+ ? await userIdService.generateUserIdFromClientId(requestMeta.anonymousId, siteId, identityOptions)
+ : await userIdService.generateUserId(requestMeta?.ipAddress || "", requestMeta?.userAgent || "", siteId, {
+ ...identityOptions,
+ lookupAsn: requestMeta?.lookupAsn,
+ });
// Check if client provided an identified user ID (different from device fingerprint)
const trimmedClientUserId = clientUserId?.trim() || "";
@@ -85,11 +109,15 @@ export class SessionReplayIngestService {
// scopes session assignment so stored user identity semantics stay unchanged.
const userId = deviceFingerprint;
- // Get or create a session ID from the sessions service
- const { sessionId } = await sessionsService.updateSession({
+ // Join the visitor's analytics session without renewing it: an open tab
+ // sends batches on DOM churn alone, and that used to keep the session alive
+ // indefinitely. A replay moves to a new session only on a full snapshot,
+ // which every replay needs to be played or listed.
+ const { sessionId, needsFullSnapshot } = await sessionsService.resolveReplaySession({
userId,
identifiedUserId,
siteId,
+ hasFullSnapshot: events.some(event => Number(event.type) === FULL_SNAPSHOT_EVENT_TYPE),
});
// Serialize once: the sizes gate the batch before anything is written, and
@@ -196,7 +224,7 @@ export class SessionReplayIngestService {
);
}
- return { capped: false };
+ return { capped: false, needsFullSnapshot };
}
private async updateSessionMetadata(
diff --git a/server/src/services/sessions/replaySession.integration.test.ts b/server/src/services/sessions/replaySession.integration.test.ts
new file mode 100644
index 000000000..f4df33501
--- /dev/null
+++ b/server/src/services/sessions/replaySession.integration.test.ts
@@ -0,0 +1,156 @@
+import RedisMock from "ioredis-mock";
+import { beforeEach, describe, expect, it } from "vitest";
+import { pickReplaySession, REPLAY_SESSION_LUA } from "./replaySession.js";
+
+// The Lua runs for real in ioredis-mock's embedded Lua runtime, against the
+// same cases as its in-process copy, so the two can't drift apart.
+
+const ANALYTICS_KEY = "session:42:visitor";
+const REPLAY_KEY = "replay-session:42:visitor";
+const TTL_MS = 30 * 60 * 1000;
+
+type Case = {
+ name: string;
+ analytics: string | null;
+ replay: string | null;
+ hasFullSnapshot: boolean;
+ sessionId: string;
+ needsFullSnapshot: boolean;
+};
+
+const CASES: Case[] = [
+ {
+ name: "a first batch starts the session its pageview will join",
+ analytics: null,
+ replay: null,
+ hasFullSnapshot: true,
+ sessionId: "candidate",
+ needsFullSnapshot: false,
+ },
+ {
+ name: "a first batch without a snapshot asks for one",
+ analytics: null,
+ replay: null,
+ hasFullSnapshot: false,
+ sessionId: "candidate",
+ needsFullSnapshot: true,
+ },
+ {
+ name: "a replay joins the analytics session",
+ analytics: "A",
+ replay: null,
+ hasFullSnapshot: true,
+ sessionId: "A",
+ needsFullSnapshot: false,
+ },
+ {
+ name: "a replay joining without a snapshot (a new identity) asks for one",
+ analytics: "A",
+ replay: null,
+ hasFullSnapshot: false,
+ sessionId: "A",
+ needsFullSnapshot: true,
+ },
+ {
+ name: "a replay continues in the analytics session",
+ analytics: "A",
+ replay: "A",
+ hasFullSnapshot: false,
+ sessionId: "A",
+ needsFullSnapshot: false,
+ },
+ {
+ name: "a replay outlives its lapsed analytics session",
+ analytics: null,
+ replay: "A",
+ hasFullSnapshot: false,
+ sessionId: "A",
+ needsFullSnapshot: false,
+ },
+ {
+ name: "a snapshot alone doesn't start a session once analytics has lapsed",
+ analytics: null,
+ replay: "A",
+ hasFullSnapshot: true,
+ sessionId: "A",
+ needsFullSnapshot: false,
+ },
+ {
+ name: "a replay waits in its session for a snapshot before following a new one",
+ analytics: "B",
+ replay: "A",
+ hasFullSnapshot: false,
+ sessionId: "A",
+ needsFullSnapshot: true,
+ },
+ {
+ name: "a replay follows a new analytics session from a batch with a snapshot",
+ analytics: "B",
+ replay: "A",
+ hasFullSnapshot: true,
+ sessionId: "B",
+ needsFullSnapshot: false,
+ },
+];
+
+interface SessionMockRedis {
+ eval(script: string, keyCount: number, ...args: (string | number)[]): Promise<[string, number]>;
+ get(key: string): Promise<string | null>;
+ set(key: string, value: string, mode: "PX", ttlMs: number): Promise<unknown>;
+ pttl(key: string): Promise<number>;
+ flushall(): Promise<unknown>;
+}
+
+describe("REPLAY_SESSION_LUA", () => {
+ let redis: SessionMockRedis;
+
+ beforeEach(async () => {
+ redis = new RedisMock() as unknown as SessionMockRedis;
+ await redis.flushall();
+ });
+
+ async function resolve(hasFullSnapshot: boolean) {
+ const [sessionId, needsFullSnapshot] = await redis.eval(
+ REPLAY_SESSION_LUA,
+ 2,
+ ANALYTICS_KEY,
+ REPLAY_KEY,
+ "candidate",
+ TTL_MS,
+ hasFullSnapshot ? "1" : "0"
+ );
+ return { sessionId, needsFullSnapshot: needsFullSnapshot === 1 };
+ }
+
+ for (const testCase of CASES) {
+ it(testCase.name, async () => {
+ if (testCase.analytics) await redis.set(ANALYTICS_KEY, testCase.analytics, "PX", 1000);
+ if (testCase.replay) await redis.set(REPLAY_KEY, testCase.replay, "PX", 1000);
+
+ expect(await resolve(testCase.hasFullSnapshot)).toEqual({
+ sessionId: testCase.sessionId,
+ needsFullSnapshot: testCase.needsFullSnapshot,
+ });
+
+ // The replay's session is the one it's in now, on a fresh window.
+ expect(await redis.get(REPLAY_KEY)).toBe(testCase.sessionId);
+ expect(await redis.pttl(REPLAY_KEY)).toBeGreaterThan(1000);
+ // The analytics session is only ever created, never renewed or moved.
+ expect(await redis.get(ANALYTICS_KEY)).toBe(testCase.analytics ?? (testCase.replay ? null : testCase.sessionId));
+ if (testCase.analytics) {
+ expect(await redis.pttl(ANALYTICS_KEY)).toBeLessThanOrEqual(1000);
+ }
+ });
+ }
+});
+
+describe("pickReplaySession", () => {
+ for (const testCase of CASES) {
+ it(testCase.name, () => {
+ expect(pickReplaySession(testCase.analytics, testCase.replay, testCase.hasFullSnapshot)).toEqual({
+ sessionId: testCase.sessionId === "candidate" ? null : testCase.sessionId,
+ needsFullSnapshot: testCase.needsFullSnapshot,
+ });
+ });
+ }
+});
diff --git a/server/src/services/sessions/replaySession.ts b/server/src/services/sessions/replaySession.ts
new file mode 100644
index 000000000..7c7678d66
--- /dev/null
+++ b/server/src/services/sessions/replaySession.ts
@@ -0,0 +1,75 @@
+/**
+ * Which session a session replay batch records into. Two keys take part: the
+ * visitor's analytics session, which only tracked events renew, and the
+ * session the replay is recording into, renewed by each replay batch.
+ *
+ * - While both name the same session, or the analytics one has lapsed: the
+ * replay's own session, so an idle tab's replay doesn't fragment into a new
+ * session β€” counted against the replay quota, with a fresh size budget β€”
+ * every 30 minutes.
+ * - Once a tracked event has started a new analytics session: the replay
+ * follows it only from a batch that carries a full snapshot. Until then
+ * batches stay in the previous session and the response asks the recorder
+ * for a snapshot; a session that opened on incremental events alone could
+ * never be played, and the replay list leaves it out.
+ * - No replay session yet (the first batch, a new identity): the analytics
+ * session, created if need be so the pageview joins it, and a snapshot is
+ * asked for if the batch has none.
+ *
+ * `pickReplaySession` is the in-process copy of `REPLAY_SESSION_LUA`, for when
+ * Redis is unavailable; the two must agree.
+ */
+
+export type ReplaySessionChoice = {
+ /** null: no session exists, so the caller's new candidate becomes it. */
+ sessionId: string | null;
+ needsFullSnapshot: boolean;
+};
+
+export function pickReplaySession(
+ analyticsSessionId: string | null,
+ replaySessionId: string | null,
+ hasFullSnapshot: boolean
+): ReplaySessionChoice {
+ if (replaySessionId && replaySessionId !== analyticsSessionId) {
+ if (analyticsSessionId && hasFullSnapshot) {
+ return { sessionId: analyticsSessionId, needsFullSnapshot: false };
+ }
+ return { sessionId: replaySessionId, needsFullSnapshot: analyticsSessionId !== null };
+ }
+ if (replaySessionId) {
+ return { sessionId: replaySessionId, needsFullSnapshot: false };
+ }
+ return { sessionId: analyticsSessionId, needsFullSnapshot: !hasFullSnapshot };
+}
+
+// KEYS[1] the analytics session, which this never renews unless it creates
+// it; KEYS[2] the replay's session. ARGV: candidate id, TTL in ms, and "1" when
+// the batch carries a full snapshot. Returns { sessionId, needsFullSnapshot }.
+// Atomic for the same reason as `sessionGetOrCreate`.
+export const REPLAY_SESSION_LUA = `
+ local analyticsId = redis.call('GET', KEYS[1])
+ local replayId = redis.call('GET', KEYS[2])
+ local hasFullSnapshot = ARGV[3] == '1'
+ local sessionId
+ local needsFullSnapshot = 0
+ if replayId and replayId ~= analyticsId then
+ if analyticsId and hasFullSnapshot then
+ sessionId = analyticsId
+ else
+ sessionId = replayId
+ if analyticsId then needsFullSnapshot = 1 end
+ end
+ elseif replayId then
+ sessionId = replayId
+ else
+ sessionId = analyticsId
+ if not sessionId then
+ sessionId = ARGV[1]
+ redis.call('SET', KEYS[1], sessionId, 'PX', ARGV[2])
+ end
+ if not hasFullSnapshot then needsFullSnapshot = 1 end
+ end
+ redis.call('SET', KEYS[2], sessionId, 'PX', ARGV[2])
+ return { sessionId, needsFullSnapshot }
+`;
diff --git a/server/src/services/sessions/sessionsService.test.ts b/server/src/services/sessions/sessionsService.test.ts
index 6ea6220a1..629005d08 100644
--- a/server/src/services/sessions/sessionsService.test.ts
+++ b/server/src/services/sessions/sessionsService.test.ts
@@ -2,11 +2,12 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
sessionGetOrCreate: vi.fn(),
+ eval: vi.fn(),
quit: vi.fn(),
}));
vi.mock("../../db/redis/redis.js", () => ({
- sessionRedis: { quit: mocks.quit },
+ sessionRedis: { quit: mocks.quit, eval: (...args: unknown[]) => mocks.eval(...args) },
sessionGetOrCreate: (...args: unknown[]) => mocks.sessionGetOrCreate(...args),
}));
@@ -21,6 +22,7 @@ describe("SessionsService (Redis-backed)", () => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
mocks.sessionGetOrCreate.mockReset();
+ mocks.eval.mockReset();
mocks.quit.mockReset();
service = new SessionsService();
});
@@ -143,3 +145,100 @@ describe("SessionsService (Redis-backed)", () => {
expect(mocks.quit).toHaveBeenCalledTimes(1);
});
});
+
+describe("SessionsService.resolveReplaySession", () => {
+ let service: SessionsService;
+
+ beforeEach(() => {
+ vi.useFakeTimers();
+ vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
+ mocks.sessionGetOrCreate.mockReset();
+ mocks.eval.mockReset();
+ service = new SessionsService();
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ const replayBatch = (hasFullSnapshot = false) =>
+ service.resolveReplaySession({ userId: "user-e", siteId: 9, hasFullSnapshot });
+
+ it("resolves through the analytics key without the renewing command", async () => {
+ mocks.eval.mockResolvedValue(["sess-existing", 0]);
+
+ const result = await service.resolveReplaySession({ userId: "user-a", siteId: 42, hasFullSnapshot: true });
+
+ expect(result).toEqual({ sessionId: "sess-existing", needsFullSnapshot: false });
+ expect(mocks.sessionGetOrCreate).not.toHaveBeenCalled();
+ const [script, keyCount, key, replayKey, candidate, ttl, hasFullSnapshot] = mocks.eval.mock.calls[0];
+ // Nothing in the script slides a TTL it didn't just set.
+ expect(script).not.toMatch(/EXPIRE/i);
+ expect(keyCount).toBe(2);
+ expect(key).toBe("session:42:user-a");
+ expect(replayKey).toBe("replay-session:42:user-a");
+ expect(candidate).toHaveLength(14);
+ expect(ttl).toBe(SESSION_TTL_MS);
+ expect(hasFullSnapshot).toBe("1");
+ });
+
+ it("passes on the script's request for a full snapshot", async () => {
+ mocks.eval.mockResolvedValue(["sess-previous", 1]);
+
+ expect(await replayBatch()).toEqual({ sessionId: "sess-previous", needsFullSnapshot: true });
+ expect(mocks.eval.mock.calls[0][6]).toBe("0");
+ });
+
+ it("uses the same identified session key as updateSession", async () => {
+ mocks.sessionGetOrCreate.mockResolvedValue("x");
+ mocks.eval.mockResolvedValue(["x", 0]);
+
+ await service.updateSession({ userId: "fingerprint", identifiedUserId: "employee-alice", siteId: 42 });
+ await service.resolveReplaySession({
+ userId: "fingerprint",
+ identifiedUserId: "employee-alice",
+ siteId: 42,
+ hasFullSnapshot: false,
+ });
+
+ expect(mocks.eval.mock.calls[0][2]).toBe(mocks.sessionGetOrCreate.mock.calls[0][0]);
+ });
+
+ it("keeps a replay in the session Redis resolved when a later lookup fails", async () => {
+ // An existing session: this worker never saw an event for the visitor.
+ mocks.eval.mockResolvedValueOnce(["sess-real", 0]);
+ await replayBatch();
+
+ mocks.eval.mockRejectedValue(new Error("redis blip"));
+ expect(await replayBatch()).toEqual({ sessionId: "sess-real", needsFullSnapshot: false });
+ });
+
+ it("keeps a replay in one session through an outage longer than the analytics window", async () => {
+ mocks.eval.mockRejectedValue(new Error("redis down"));
+ const first = await replayBatch(true);
+
+ for (let minute = 20; minute <= 80; minute += 20) {
+ vi.setSystemTime(new Date(Date.parse("2026-01-01T00:00:00.000Z") + minute * 60_000));
+ expect(await replayBatch()).toEqual({ sessionId: first.sessionId, needsFullSnapshot: false });
+ }
+ });
+
+ it("does not slide the analytics fallback window during an outage", async () => {
+ mocks.sessionGetOrCreate.mockResolvedValueOnce("sess-real");
+ await service.updateSession({ userId: "user-e", siteId: 9 });
+ mocks.sessionGetOrCreate.mockRejectedValue(new Error("redis down"));
+ mocks.eval.mockRejectedValue(new Error("redis down"));
+
+ vi.setSystemTime(new Date(Date.now() + SESSION_TTL_MS - 1000));
+ expect((await replayBatch()).sessionId).toBe("sess-real");
+
+ // Replay didn't extend the analytics session, so the next event starts another.
+ vi.setSystemTime(new Date(Date.now() + 2000));
+ const event = await service.updateSession({ userId: "user-e", siteId: 9 });
+ expect(event.sessionId).not.toBe("sess-real");
+
+ // The replay waits for a full snapshot before following it.
+ expect(await replayBatch()).toEqual({ sessionId: "sess-real", needsFullSnapshot: true });
+ expect(await replayBatch(true)).toEqual({ sessionId: event.sessionId, needsFullSnapshot: false });
+ });
+});
diff --git a/server/src/services/sessions/sessionsService.ts b/server/src/services/sessions/sessionsService.ts
index 37cfd2165..ab7f79cf1 100644
--- a/server/src/services/sessions/sessionsService.ts
+++ b/server/src/services/sessions/sessionsService.ts
@@ -2,6 +2,7 @@ import crypto from "crypto";
import { nanoid } from "nanoid";
import { createServiceLogger } from "../../lib/logger/logger.js";
import { sessionRedis, sessionGetOrCreate } from "../../db/redis/redis.js";
+import { pickReplaySession, REPLAY_SESSION_LUA } from "./replaySession.js";
// Sessions expire after this much inactivity. Redis refreshes the TTL on every
// event (sliding window) and evicts the key automatically once it lapses β€” there
@@ -25,6 +26,11 @@ interface CachedSession {
export class SessionsService {
private logger = createServiceLogger("sessions");
private fallbackCache = new Map<string, CachedSession>();
+ // Like fallbackCache, for the session each replay is recording into: kept on
+ // every replay batch Redis resolves, and slid only by replay batches, so a
+ // Redis outage neither moves a replay to an invented id nor splits it every
+ // 30 minutes (each split a fresh size budget).
+ private replayFallbackCache = new Map<string, CachedSession>();
private getSessionKey(userId: string, siteId: number, identifiedUserId?: string): string {
if (identifiedUserId) {
@@ -63,7 +69,7 @@ export class SessionsService {
try {
const sessionId = await sessionGetOrCreate(key, candidate, SESSION_TTL_MS);
- this.rememberSession(key, sessionId);
+ this.rememberSession(this.fallbackCache, key, sessionId);
return { sessionId };
} catch (error) {
// A Redis blip must never drop ingestion β€” and must never split a session.
@@ -75,17 +81,67 @@ export class SessionsService {
}
}
+ /**
+ * The session a session replay batch belongs to (see replaySession.ts),
+ * resolved without renewing the analytics session: an open tab sends batches
+ * on background DOM churn alone, and that used to keep the visitor's session
+ * alive indefinitely. Only tracked events renew it now.
+ * `needsFullSnapshot` asks the recorder for one: the replay is waiting to
+ * follow a new session, or has none in the session it opened.
+ */
+ async resolveReplaySession({
+ userId,
+ identifiedUserId,
+ siteId,
+ hasFullSnapshot,
+ }: {
+ userId: string;
+ identifiedUserId?: string;
+ siteId: number;
+ hasFullSnapshot: boolean;
+ }): Promise<{ sessionId: string; needsFullSnapshot: boolean }> {
+ const key = this.getSessionKey(userId, siteId, identifiedUserId);
+ const candidate = nanoid(14);
+
+ try {
+ const [sessionId, needsFullSnapshot] = (await sessionRedis.eval(
+ REPLAY_SESSION_LUA,
+ 2,
+ key,
+ `replay-${key}`,
+ candidate,
+ SESSION_TTL_MS,
+ hasFullSnapshot ? "1" : "0"
+ )) as [string, number];
+ // Only a session this created starts a fresh window in the fallback
+ // mirror; an existing one keeps the expiry its events gave it.
+ if (sessionId === candidate) {
+ this.rememberSession(this.fallbackCache, key, sessionId);
+ }
+ this.rememberSession(this.replayFallbackCache, key, sessionId);
+ return { sessionId, needsFullSnapshot: Number(needsFullSnapshot) === 1 };
+ } catch (error) {
+ this.logger.error(error as Error, "Redis session lookup failed; using in-process fallback session id");
+ return this.fallbackReplaySession(key, candidate, hasFullSnapshot);
+ }
+ }
+
/** Cache the resolved session id with a fresh sliding expiry, LRU-bounded. */
- private rememberSession(key: string, sessionId: string): void {
+ private rememberSession(cache: Map<string, CachedSession>, key: string, sessionId: string): void {
// Re-insert to mark as most-recently-used (Map preserves insertion order).
- this.fallbackCache.delete(key);
- this.fallbackCache.set(key, { sessionId, expiresAt: Date.now() + SESSION_TTL_MS });
- if (this.fallbackCache.size > FALLBACK_CACHE_MAX) {
- const oldest = this.fallbackCache.keys().next().value;
- if (oldest !== undefined) this.fallbackCache.delete(oldest);
+ cache.delete(key);
+ cache.set(key, { sessionId, expiresAt: Date.now() + SESSION_TTL_MS });
+ if (cache.size > FALLBACK_CACHE_MAX) {
+ const oldest = cache.keys().next().value;
+ if (oldest !== undefined) cache.delete(oldest);
}
}
+ private liveSessionId(cache: Map<string, CachedSession>, key: string): string | null {
+ const cached = cache.get(key);
+ return cached && cached.expiresAt > Date.now() ? cached.sessionId : null;
+ }
+
/**
* Resolve a session id without Redis. Reuses the last id seen for this user if
* it's still within the sliding window (refreshing it), otherwise adopts
@@ -94,12 +150,30 @@ export class SessionsService {
* that stays stable across the 30-minute boundary instead of resetting on it.
*/
private fallbackSessionId(key: string, candidate: string): string {
- const cached = this.fallbackCache.get(key);
- const sessionId = cached && cached.expiresAt > Date.now() ? cached.sessionId : candidate;
- this.rememberSession(key, sessionId);
+ const sessionId = this.liveSessionId(this.fallbackCache, key) ?? candidate;
+ this.rememberSession(this.fallbackCache, key, sessionId);
return sessionId;
}
+ /** `resolveReplaySession` without Redis, on the in-process mirrors. */
+ private fallbackReplaySession(
+ key: string,
+ candidate: string,
+ hasFullSnapshot: boolean
+ ): { sessionId: string; needsFullSnapshot: boolean } {
+ const choice = pickReplaySession(
+ this.liveSessionId(this.fallbackCache, key),
+ this.liveSessionId(this.replayFallbackCache, key),
+ hasFullSnapshot
+ );
+ const sessionId = choice.sessionId ?? candidate;
+ if (choice.sessionId === null) {
+ this.rememberSession(this.fallbackCache, key, sessionId);
+ }
+ this.rememberSession(this.replayFallbackCache, key, sessionId);
+ return { sessionId, needsFullSnapshot: choice.needsFullSnapshot };
+ }
+
/** Close the dedicated session Redis connection during graceful shutdown. */
async close(): Promise<void> {
try {
diff --git a/server/src/services/siteMetrics/siteMetrics.test.ts b/server/src/services/siteMetrics/siteMetrics.test.ts
index 1d7f67911..19edac71f 100644
--- a/server/src/services/siteMetrics/siteMetrics.test.ts
+++ b/server/src/services/siteMetrics/siteMetrics.test.ts
@@ -166,6 +166,15 @@ describe("site metrics", () => {
expect(sql.match(/INNER JOIN FilteredSessions USING \(session_id\)/g)).toHaveLength(2);
expect(sql).toContain("countIf(type = 'pageview') AS pageviews");
});
+
+ it("reads weekly (Date) buckets as DateTimes in the report timezone", () => {
+ const sql = buildChartQuery(pdfSpec, "toStartOfWeek");
+
+ expect(sql).toContain(
+ "toString(toDateTime(toStartOfWeek(toTimeZone(session_start, {timeZone:String})), {timeZone:String}))"
+ );
+ expect(sql).not.toContain("toTimeZone(toStartOfWeek(");
+ });
});
describe("buildMetricsSpec", () => {
diff --git a/server/src/services/siteMetrics/siteMetrics.ts b/server/src/services/siteMetrics/siteMetrics.ts
index 66f5d9e37..3a4d0b847 100644
--- a/server/src/services/siteMetrics/siteMetrics.ts
+++ b/server/src/services/siteMetrics/siteMetrics.ts
@@ -186,8 +186,10 @@ export function buildBreakdownQuery(dimension: BreakdownDimension, spec: SiteMet
export function buildChartQuery(spec: SiteMetricsSpec, bucketFn: ChartBucketFunction): string {
const { timeStatement, filteredSessionsCTE } = spec;
const sessionJoin = filteredSessionsCTE ? "INNER JOIN FilteredSessions USING (session_id)" : "";
+ // toStartOfWeek returns a Date, which toTimeZone rejects; toDateTime takes
+ // both and reads a Date as midnight in the report timezone.
const bucket = (column: string) =>
- `toString(toTimeZone(${bucketFn}(toTimeZone(${column}, {timeZone:String})), {timeZone:String}))`;
+ `toString(toDateTime(${bucketFn}(toTimeZone(${column}, {timeZone:String})), {timeZone:String}))`;
return `
WITH
diff --git a/server/src/services/sites/siteConfigurationLifecycle.test.ts b/server/src/services/sites/siteConfigurationLifecycle.test.ts
index 8c7e623e9..66e4df111 100644
--- a/server/src/services/sites/siteConfigurationLifecycle.test.ts
+++ b/server/src/services/sites/siteConfigurationLifecycle.test.ts
@@ -6,6 +6,7 @@ const state = vi.hoisted(() => ({
deleteError: null as Error | null,
updates: [] as Record<string, unknown>[],
deletes: 0,
+ isCloud: false,
}));
const mocks = vi.hoisted(() => ({
@@ -61,6 +62,14 @@ vi.mock("../../api/stripe/getSubscription.js", () => ({
getSubscriptionInner: vi.fn(),
}));
+vi.mock("../../lib/const.js", async importOriginal => ({
+ ...(await importOriginal<typeof import("../../lib/const.js")>()),
+ get IS_CLOUD() {
+ return state.isCloud;
+ },
+}));
+
+import { getSubscriptionInner } from "../../api/stripe/getSubscription.js";
import { siteConfigurationLifecycle } from "./siteConfigurationLifecycle.js";
function makeSite() {
@@ -80,6 +89,7 @@ beforeEach(() => {
state.deleteError = null;
state.updates.length = 0;
state.deletes = 0;
+ state.isCloud = false;
mocks.clickhouseCommand.mockResolvedValue(undefined);
mocks.getConfig.mockResolvedValue({ siteId: 1, id: "abcdef123456", name: "Renamed" });
});
@@ -112,6 +122,48 @@ describe("siteConfigurationLifecycle", () => {
expect(mocks.getConfig).not.toHaveBeenCalled();
});
+ describe("Standard feature entitlement on cloud", () => {
+ beforeEach(() => {
+ state.isCloud = true;
+ });
+
+ it("refuses to turn on a Standard feature without an active subscription", async () => {
+ vi.mocked(getSubscriptionInner).mockResolvedValue({ status: "free", includesReplay: false } as any);
+
+ await expect(siteConfigurationLifecycle.update(1, { webVitals: true })).rejects.toMatchObject({
+ code: "standard_features_not_entitled",
+ statusCode: 403,
+ });
+ expect(state.updates).toEqual([]);
+ });
+
+ it.each(["active", "trialing"])("turns a Standard feature on for a %s subscription", async status => {
+ vi.mocked(getSubscriptionInner).mockResolvedValue({ status, includesReplay: false } as any);
+
+ await siteConfigurationLifecycle.update(1, { trackErrors: true });
+
+ expect(state.updates).toEqual([expect.objectContaining({ trackErrors: true })]);
+ });
+
+ it("always lets a Standard feature be turned off", async () => {
+ state.site = { ...makeSite(), trackFormInteractions: true };
+
+ await siteConfigurationLifecycle.update(1, { trackFormInteractions: false });
+
+ expect(getSubscriptionInner).not.toHaveBeenCalled();
+ expect(state.updates).toEqual([expect.objectContaining({ trackFormInteractions: false })]);
+ });
+
+ it("keeps accepting a Standard feature the site already has, e.g. a full-form save", async () => {
+ state.site = { ...makeSite(), webVitals: true };
+
+ await siteConfigurationLifecycle.update(1, { webVitals: true, name: "Renamed" });
+
+ expect(getSubscriptionInner).not.toHaveBeenCalled();
+ expect(state.updates).toEqual([expect.objectContaining({ webVitals: true, name: "Renamed" })]);
+ });
+ });
+
it("owns private-link persistence and invalidation", async () => {
const privateLinkKey = await siteConfigurationLifecycle.updatePrivateLink(1, "generate_private_link_key");
diff --git a/server/src/services/sites/siteConfigurationLifecycle.ts b/server/src/services/sites/siteConfigurationLifecycle.ts
index 46e972ec3..f2edc6a1e 100644
--- a/server/src/services/sites/siteConfigurationLifecycle.ts
+++ b/server/src/services/sites/siteConfigurationLifecycle.ts
@@ -190,6 +190,20 @@ function validateMobileFeatures(type: SiteType, input: Pick<CreateSiteInput, "se
}
}
+function assertStandardFeaturesEntitled(
+ subscription: { status: string } | null,
+ requestedFeatures: ReadonlyArray<(typeof STANDARD_FEATURES)[number]>
+): void {
+ const hasActiveSubscription = subscription?.status === "active" || subscription?.status === "trialing";
+ if (requestedFeatures.length > 0 && !hasActiveSubscription) {
+ throw new SiteLifecycleError(
+ "standard_features_not_entitled",
+ 403,
+ `The following features require an active subscription: ${requestedFeatures.join(", ")}`
+ );
+ }
+}
+
function validateSiteId(siteId: number): void {
if (!Number.isInteger(siteId) || siteId <= 0) {
throw new SiteLifecycleError("invalid_site_id", 400, "Invalid site ID: must be a positive integer");
@@ -237,15 +251,10 @@ class SiteConfigurationLifecycle {
throw new SiteLifecycleError("replay_not_entitled", 403, "Session replay requires a Pro subscription");
}
- const requestedStandardFeatures = STANDARD_FEATURES.filter(feature => input[feature]);
- const hasActiveSubscription = subscription.status === "active" || subscription.status === "trialing";
- if (requestedStandardFeatures.length > 0 && !hasActiveSubscription) {
- throw new SiteLifecycleError(
- "standard_features_not_entitled",
- 403,
- `The following features require an active subscription: ${requestedStandardFeatures.join(", ")}`
- );
- }
+ assertStandardFeaturesEntitled(
+ subscription,
+ STANDARD_FEATURES.filter(feature => input[feature])
+ );
const siteLimit = subscription.siteLimit ?? null;
if (siteLimit !== null) {
@@ -450,11 +459,16 @@ class SiteConfigurationLifecycle {
}
validateMobileFeatures(nextSiteType, input);
- if (IS_CLOUD && input.sessionReplay === true) {
+ // Same entitlements as creation. Standard features are checked only when this update turns
+ // one on, so turning them off, or saving other settings on a site that already has them
+ // (from before they needed a plan), keeps working.
+ const enabledStandardFeatures = STANDARD_FEATURES.filter(feature => input[feature] === true && !site[feature]);
+ if (IS_CLOUD && (input.sessionReplay === true || enabledStandardFeatures.length > 0)) {
const subscription = site.organizationId ? await getSubscriptionInner(site.organizationId) : null;
- if (!subscription?.includesReplay) {
+ if (input.sessionReplay === true && !subscription?.includesReplay) {
throw new SiteLifecycleError("replay_not_entitled", 403, "Session replay requires a Pro plan");
}
+ assertStandardFeaturesEntitled(subscription, enabledStandardFeatures);
}
if (input.excludedIPs) {
diff --git a/server/src/services/sites/siteExclusionDecision.test.ts b/server/src/services/sites/siteExclusionDecision.test.ts
index f1d25ad9f..158a6ac77 100644
--- a/server/src/services/sites/siteExclusionDecision.test.ts
+++ b/server/src/services/sites/siteExclusionDecision.test.ts
@@ -4,8 +4,27 @@ const mocks = vi.hoisted(() => ({
getLocation: vi.fn(),
lookupAsn: vi.fn(),
loggerWarn: vi.fn(),
+ addressParses: 0,
}));
+// The real parser, counting each address it is asked to read.
+vi.mock("ip-address", async importOriginal => {
+ const actual = await importOriginal<typeof import("ip-address")>();
+ class Address4 extends actual.Address4 {
+ constructor(address: string) {
+ mocks.addressParses++;
+ super(address);
+ }
+ }
+ class Address6 extends actual.Address6 {
+ constructor(address: string, optionalGroups?: number) {
+ mocks.addressParses++;
+ super(address, optionalGroups);
+ }
+ }
+ return { ...actual, Address4, Address6 };
+});
+
vi.mock("../../db/geolocation/geolocation.js", () => ({
getLocation: mocks.getLocation,
}));
@@ -65,6 +84,70 @@ describe("decideSiteExclusion", () => {
});
});
+ it.each([
+ ["a zero-padded IPv4 rule", "198.051.100.010", "198.51.100.10"],
+ ["an upper-case IPv6 rule", "2001:DB8::1", "2001:db8::1"],
+ ["an expanded IPv6 rule", "2001:0db8:0000:0000:0000:0000:0000:0001", "2001:db8::1"],
+ ["an upper-case IPv6 request address", "2001:db8::1", "2001:DB8::1"],
+ // Ranges and CIDR blocks always parsed their addresses; single rules now agree.
+ ["a zero-padded IPv4 range", "198.51.100.001-198.51.100.020", "198.51.100.10"],
+ ])("matches %s by address rather than spelling", async (_description, pattern, ipAddress) => {
+ await expect(
+ decideSiteExclusion(configuration({ excludedIPs: [` ${pattern} `] }), { ...request, ipAddress })
+ ).resolves.toMatchObject({ excluded: true, reason: "ip", value: ipAddress });
+ });
+
+ it("still tells different addresses apart after canonicalizing", async () => {
+ await expect(
+ decideSiteExclusion(configuration({ excludedIPs: ["198.51.100.011", "2001:db8::2", "unknown"] }), {
+ ...request,
+ ipAddress: "198.51.100.10",
+ candidateIps: ["2001:db8::1", "UNKNOWN"],
+ })
+ ).resolves.toEqual({ excluded: false });
+ });
+
+ it("parses each address once rather than once per address and rule pair", async () => {
+ // A long forwarded chain against many single-address rules, all IPv6 so every
+ // parse is an IPv4 attempt that throws and then an IPv6 one.
+ const excludedIPs = Array.from({ length: 100 }, (_, i) => `2001:db8::${(i + 1).toString(16)}`);
+ const candidateIps = Array.from({ length: 500 }, (_, i) => `2001:db8:1::${(i + 1).toString(16)}`);
+ const rules = configuration({ excludedIPs });
+ const chain = { ...request, ipAddress: "2001:db8:2::1", candidateIps };
+ const perAddress = 2;
+
+ mocks.addressParses = 0;
+ await expect(decideSiteExclusion(rules, chain)).resolves.toEqual({ excluded: false });
+ expect(mocks.addressParses).toBeLessThanOrEqual(perAddress * (excludedIPs.length + candidateIps.length + 1));
+
+ // The same configuration again: its rules are already parsed.
+ mocks.addressParses = 0;
+ await expect(decideSiteExclusion(rules, chain)).resolves.toEqual({ excluded: false });
+ expect(mocks.addressParses).toBeLessThanOrEqual(perAddress * (candidateIps.length + 1));
+
+ // ...and still match, by spelling or by address, and follow a changed rule list.
+ await expect(
+ decideSiteExclusion(rules, { ...chain, candidateIps: [...candidateIps, "2001:DB8::0064"] })
+ ).resolves.toMatchObject({ excluded: true, reason: "ip", value: "2001:DB8::0064" });
+ await expect(decideSiteExclusion(configuration({ excludedIPs: ["2001:db8:2::1"] }), chain)).resolves.toMatchObject({
+ excluded: true,
+ reason: "ip",
+ value: "2001:db8:2::1",
+ });
+ });
+
+ it("keeps matching an unparseable rule by its exact spelling", async () => {
+ const rules = configuration({ excludedIPs: [" unknown "] });
+ await expect(decideSiteExclusion(rules, { ...request, ipAddress: "unknown" })).resolves.toMatchObject({
+ excluded: true,
+ reason: "ip",
+ value: "unknown",
+ });
+ await expect(decideSiteExclusion(rules, { ...request, ipAddress: "UNKNOWN" })).resolves.toEqual({
+ excluded: false,
+ });
+ });
+
it("matches an excluded IP among the candidates when resolution picked a different one", async () => {
// The visitor-splitting regression: the resolver lands on a proxy egress
// IP, but the owner's real IP is still present among forwarded candidates.
diff --git a/server/src/services/sites/siteExclusionDecision.ts b/server/src/services/sites/siteExclusionDecision.ts
index 2c24a8bff..3ff39a6b2 100644
--- a/server/src/services/sites/siteExclusionDecision.ts
+++ b/server/src/services/sites/siteExclusionDecision.ts
@@ -1,3 +1,4 @@
+import { Address4, Address6 } from "ip-address";
import { lookupAsn, type AsnLookup } from "../../db/geolocation/asn.js";
import { getLocation } from "../../db/geolocation/geolocation.js";
import { matchesCIDR, matchesRange } from "../../lib/ipUtils.js";
@@ -90,13 +91,73 @@ function matchesGlob(value: string, pattern: string): boolean {
return globIndex === glob.length;
}
-function matchesIPPattern(ipAddress: string, pattern: string): boolean {
+/**
+ * One spelling per address β€” "192.168.001.1" is 192.168.1.1, "2001:DB8:0::1"
+ * is 2001:db8::1 β€” so equal addresses compare equal as strings. Leading zeros
+ * read as decimal, the way CIDR and range rules already parse them. Anything
+ * unparseable is returned as given.
+ */
+function canonicalizeIP(ip: string): string {
try {
- const trimmedPattern = pattern.trim();
+ return new Address4(ip).correctForm();
+ } catch {
+ try {
+ return new Address6(ip).correctForm();
+ } catch {
+ return ip;
+ }
+ }
+}
+
+const isSingleIPPattern = (pattern: string) => !pattern.includes("/") && !pattern.includes("-");
- if (!trimmedPattern.includes("/") && !trimmedPattern.includes("-")) {
- return ipAddress === trimmedPattern;
+type CompiledIPRules = {
+ /** Single-address rules, each as written (trimmed) and in canonical form. */
+ addresses: Set<string>;
+ /** CIDR and range rules, matched one by one. */
+ patterns: string[];
+};
+
+// Keyed by the rules array: the site configuration cache hands every request the
+// same array until the configuration changes, so rules are parsed once per change,
+// not once per request.
+const compiledIPRules = new WeakMap<string[], CompiledIPRules>();
+
+function compileIPRules(excludedIPs: string[]): CompiledIPRules {
+ let compiled = compiledIPRules.get(excludedIPs);
+ if (!compiled) {
+ compiled = { addresses: new Set(), patterns: [] };
+ for (const pattern of excludedIPs) {
+ const trimmedPattern = pattern.trim();
+ if (isSingleIPPattern(trimmedPattern)) {
+ compiled.addresses.add(trimmedPattern);
+ compiled.addresses.add(canonicalizeIP(trimmedPattern));
+ } else {
+ compiled.patterns.push(pattern);
+ }
}
+ compiledIPRules.set(excludedIPs, compiled);
+ }
+ return compiled;
+}
+
+/**
+ * Settings accept any spelling ip-address parses, and saved rules were never
+ * rewritten, so single addresses match by canonical form as well as by the exact
+ * string. Each side is parsed once: the rules per configuration, the address per
+ * call, rather than once per address and rule pair, which a long forwarded chain
+ * against many rules made expensive on the ingestion path.
+ */
+function matchesIPRules(ipAddress: string, rules: CompiledIPRules): boolean {
+ if (rules.addresses.size > 0 && (rules.addresses.has(ipAddress) || rules.addresses.has(canonicalizeIP(ipAddress)))) {
+ return true;
+ }
+ return rules.patterns.some(pattern => matchesIPPattern(ipAddress, pattern));
+}
+
+function matchesIPPattern(ipAddress: string, pattern: string): boolean {
+ try {
+ const trimmedPattern = pattern.trim();
if (trimmedPattern.includes("/")) {
return matchesCIDR(ipAddress, trimmedPattern);
@@ -163,7 +224,8 @@ export async function decideSiteExclusion(
request: SiteExclusionRequest
): Promise<SiteExclusionDecision> {
const ipsToCheck = [...new Set([request.ipAddress, ...(request.candidateIps ?? [])])];
- const matchedIp = ipsToCheck.find(ip => configuration.excludedIPs.some(pattern => matchesIPPattern(ip, pattern)));
+ const ipRules = compileIPRules(configuration.excludedIPs);
+ const matchedIp = ipsToCheck.find(ip => matchesIPRules(ip, ipRules));
if (matchedIp) {
return excluded("ip", "IP", matchedIp);
}
diff --git a/server/src/services/tracker/ingestEvent.test.ts b/server/src/services/tracker/ingestEvent.test.ts
index 2acad452c..73b7e2528 100644
--- a/server/src/services/tracker/ingestEvent.test.ts
+++ b/server/src/services/tracker/ingestEvent.test.ts
@@ -156,6 +156,23 @@ describe("ingestEvent", () => {
});
});
+ it("matches exclusions against the whole URL, and stores it cut to the column limits", async () => {
+ // A query string cut first would lose `preview` and slip past its exclusion.
+ const querystring = `?padding=${"x".repeat(2050)}&preview=1`;
+ const pathname = `/${"p".repeat(3000)}`;
+
+ await ingestEvent(
+ trackingRequest({
+ payload: { type: "pageview", site_id: "site_abc", pathname, querystring } as TrackingRequest["payload"],
+ })
+ );
+
+ expect(mocks.decideSiteExclusion).toHaveBeenCalledWith(site, expect.objectContaining({ pathname, querystring }));
+ expect(mocks.addPageview).toHaveBeenCalledWith(
+ expect.objectContaining({ pathname: pathname.slice(0, 2048), querystring: querystring.slice(0, 2048) })
+ );
+ });
+
// The ordering fix: exclusion used to run *after* bot detection, so a Site
// owner's own excluded office IP still drove every anomaly counter.
it("decides exclusion before bot detection, so excluded traffic leaves no trace", async () => {
diff --git a/server/src/services/tracker/ingestEvent.ts b/server/src/services/tracker/ingestEvent.ts
index 0ed556144..9a12711ad 100644
--- a/server/src/services/tracker/ingestEvent.ts
+++ b/server/src/services/tracker/ingestEvent.ts
@@ -5,6 +5,7 @@ import { usageService } from "../usageService.js";
import { botEventQueue, botObservationQueue } from "./botBlocking/botEventQueue.js";
import { checkBotBlocking } from "./botBlocking/index.js";
import { pageviewQueue } from "./pageviewQueue.js";
+import { truncateUrlForStorage } from "./trackingPayload.js";
import type { TrackingRequest } from "./trackingRequest.js";
import { createBasePayload } from "./utils.js";
@@ -45,18 +46,23 @@ export type IngestOutcome =
* `events` plus, when something was detected, `bot_observations`.
*/
export async function ingestEvent(trackingRequest: TrackingRequest): Promise<IngestOutcome> {
- const { payload, site } = trackingRequest;
+ const { site } = trackingRequest;
+ // Matched against the URL whole: a query string cut to its column limit
+ // first can lose the very parameter an exclusion names.
const exclusionDecision = await decideSiteExclusion(site, {
ipAddress: trackingRequest.ipAddress,
candidateIps: trackingRequest.candidateIps,
- pathname: payload.pathname,
- querystring: payload.querystring,
- hostname: payload.hostname,
+ pathname: trackingRequest.payload.pathname,
+ querystring: trackingRequest.payload.querystring,
+ hostname: trackingRequest.payload.hostname,
userAgent: trackingRequest.userAgent,
lookupAsn: trackingRequest.lookupAsn,
});
+ // Everything past the exclusion works on the event as it will be stored.
+ const payload = truncateUrlForStorage(trackingRequest.payload);
+
if (exclusionDecision.excluded) {
logger.debug({ siteId: payload.site_id, exclusionReason: exclusionDecision.reason }, "Site exclusion matched");
return { status: "excluded", exclusion: exclusionDecision };
@@ -93,7 +99,7 @@ export async function ingestEvent(trackingRequest: TrackingRequest): Promise<Ing
},
});
- const eventPayload = await createBasePayload(trackingRequest);
+ const eventPayload = await createBasePayload({ ...trackingRequest, payload });
if (botDetectionResult?.enforced) {
await botEventQueue.add({
diff --git a/server/src/services/tracker/trackingPayload.test.ts b/server/src/services/tracker/trackingPayload.test.ts
index f9b2bd2f9..4638c5f48 100644
--- a/server/src/services/tracker/trackingPayload.test.ts
+++ b/server/src/services/tracker/trackingPayload.test.ts
@@ -1,6 +1,6 @@
import { ALL_CLIENT_BOT_SIGNAL_BITS, CLIENT_BOT_SIGNAL_MASKS, MAX_CLIENT_BOT_SCORE } from "@rybbit/shared";
import { describe, expect, it } from "vitest";
-import { trackingPayloadSchema } from "./trackingPayload.js";
+import { trackingPayloadSchema, truncateUrlForStorage } from "./trackingPayload.js";
function pageview(fields: Record<string, unknown>) {
return trackingPayloadSchema.safeParse({ type: "pageview", site_id: "site_abc", ...fields });
@@ -29,3 +29,53 @@ describe("the bot signal bounds on an ingested payload", () => {
expect(pageview({}).success).toBe(true);
});
});
+
+describe("the page metadata limits on an ingested payload", () => {
+ // A long title or URL used to fail validation and drop the whole event.
+ it("cuts the page's title and referrer to length instead of rejecting the event", () => {
+ const result = pageview({
+ page_title: "t".repeat(600),
+ referrer: `https://example.com/${"r".repeat(3000)}`,
+ });
+
+ expect(result.success).toBe(true);
+ if (!result.success) return;
+ expect(result.data.page_title).toHaveLength(512);
+ expect(result.data.referrer).toHaveLength(2048);
+ });
+
+ // Site Exclusions match the path and query string, so validation keeps them
+ // whole; a parameter past the storage limit must still be seen.
+ it("keeps a long path and query string whole, and cuts them only for storage", () => {
+ const querystring = `?padding=${"q".repeat(3000)}&preview=1`;
+ const result = pageview({ pathname: `/${"p".repeat(3000)}`, querystring });
+
+ expect(result.success).toBe(true);
+ if (!result.success) return;
+ expect(result.data.querystring).toBe(querystring);
+
+ const stored = truncateUrlForStorage(result.data);
+ expect(stored.pathname).toHaveLength(2048);
+ expect(stored.querystring).toBe(querystring.slice(0, 2048));
+ });
+
+ it("rejects a path or query string past any real page's URL", () => {
+ expect(pageview({ pathname: `/${"p".repeat(16 * 1024)}` }).success).toBe(false);
+ expect(pageview({ querystring: `?${"q".repeat(16 * 1024)}` }).success).toBe(false);
+ });
+
+ it("never leaves half of a surrogate pair at the cut", () => {
+ const result = pageview({ page_title: `${"t".repeat(511)}πŸ˜€ more` });
+
+ expect(result.success).toBe(true);
+ if (!result.success) return;
+ expect(result.data.page_title).toBe("t".repeat(511));
+ });
+
+ it("keeps values within the limits as sent", () => {
+ const result = pageview({ pathname: "/pricing", page_title: "Pricing πŸ˜€" });
+
+ expect(result.success && result.data).toMatchObject({ pathname: "/pricing", page_title: "Pricing πŸ˜€" });
+ expect(result.success && truncateUrlForStorage(result.data)).toBe(result.success && result.data);
+ });
+});
diff --git a/server/src/services/tracker/trackingPayload.ts b/server/src/services/tracker/trackingPayload.ts
index cde7a6f9f..5cafd7af4 100644
--- a/server/src/services/tracker/trackingPayload.ts
+++ b/server/src/services/tracker/trackingPayload.ts
@@ -1,17 +1,40 @@
import { ALL_CLIENT_BOT_SIGNAL_BITS, MAX_CLIENT_BOT_SCORE } from "@rybbit/shared";
import { z } from "zod";
+/**
+ * Cut a string to `maxLength` UTF-16 units without splitting a surrogate pair:
+ * half an emoji serializes to an escape ClickHouse refuses, losing the row.
+ */
+export function truncateString(value: string, maxLength: number): string {
+ if (value.length <= maxLength) return value;
+ const lastCode = value.charCodeAt(maxLength - 1);
+ return value.slice(0, lastCode >= 0xd800 && lastCode <= 0xdbff ? maxLength - 1 : maxLength);
+}
+
+// The page's own URL, title and referrer are cut to length rather than
+// rejected. They come from whatever page the visitor is on, not from the
+// integration, and a 400 dropped every event on a page whose title or URL ran
+// long β€” whichever client (tracker, SDK, older cached script) sent it.
+const truncatedString = (maxLength: number) => z.string().transform(value => truncateString(value, maxLength));
+
+// The path and query string, though, are cut only after Site Exclusions have
+// matched them whole (truncateUrlForStorage): cut first, `?padding=…&preview=1`
+// lost the very parameter a "preview" exclusion names. The validation bound
+// keeps that matching cheap and sits far past any real page's URL.
+const MAX_URL_PART_LENGTH = 2048;
+const MAX_RAW_URL_PART_LENGTH = 16 * 1024;
+
// Shared fields for all event types
const baseEventFields = {
site_id: z.string().min(1),
hostname: z.string().max(253).optional(),
- pathname: z.string().max(2048).optional(),
- querystring: z.string().max(2048).optional(),
+ pathname: z.string().max(MAX_RAW_URL_PART_LENGTH).optional(),
+ querystring: z.string().max(MAX_RAW_URL_PART_LENGTH).optional(),
screenWidth: z.number().int().nonnegative().optional(),
screenHeight: z.number().int().nonnegative().optional(),
language: z.string().max(35).optional(),
- page_title: z.string().max(512).optional(),
- referrer: z.string().max(2048).optional(),
+ page_title: truncatedString(512).optional(),
+ referrer: truncatedString(2048).optional(),
anonymous_id: z.string().min(1).max(255).optional(),
user_id: z.string().max(255).optional(),
tag: z.string().max(256).optional(),
@@ -240,3 +263,16 @@ export const trackingPayloadSchema = z.discriminatedUnion("type", [
/** The event body after validation β€” the only shape ingestion ever sees. */
export type ValidatedTrackingPayload = z.infer<typeof trackingPayloadSchema>;
+
+/** The payload with its path and query string cut to what the events table keeps. */
+export function truncateUrlForStorage(payload: ValidatedTrackingPayload): ValidatedTrackingPayload {
+ const { pathname, querystring } = payload;
+ if ((pathname?.length ?? 0) <= MAX_URL_PART_LENGTH && (querystring?.length ?? 0) <= MAX_URL_PART_LENGTH) {
+ return payload;
+ }
+ return {
+ ...payload,
+ pathname: pathname && truncateString(pathname, MAX_URL_PART_LENGTH),
+ querystring: querystring && truncateString(querystring, MAX_URL_PART_LENGTH),
+ };
+}
diff --git a/server/src/services/usageService.test.ts b/server/src/services/usageService.test.ts
index 6b13e5edf..80c176949 100644
--- a/server/src/services/usageService.test.ts
+++ b/server/src/services/usageService.test.ts
@@ -317,14 +317,30 @@ describe("updateOrganizationsMonthlyUsage β€” event limit boundary", () => {
expect(usageService.getSitesOverLimit().size).toBe(0);
});
- it("fails open when ClickHouse cannot be queried", async () => {
- singleOrg({ subscription: stripeSub({ eventLimit: 1_000 }), eventCount: 10_000_000 });
+ it("skips the run and keeps the last counts and blocks when ClickHouse cannot be queried", async () => {
+ usageService.setSitesOverLimit(new Set([1]));
+ singleOrg({
+ subscription: stripeSub({ eventLimit: 1_000 }),
+ eventCount: 10_000_000,
+ orgOverrides: { overMonthlyLimit: true },
+ });
state.clickhouseError = new Error("clickhouse is down");
+ const onUpdated = vi.fn();
+ usageService.onUsageUpdated(onUpdated);
await usageService.updateOrganizationsMonthlyUsage();
- expect(state.updates[0]).toMatchObject({ monthlyEventCount: 0, overMonthlyLimit: false });
- expect(usageService.getSitesOverLimit().size).toBe(0);
+ expect(state.updates).toEqual([]);
+ expect(usageService.isSiteOverLimit(1)).toBe(true);
+ expect(mocks.sendLimitExceededEmail).not.toHaveBeenCalled();
+ expect(onUpdated).not.toHaveBeenCalled();
+
+ // Once ClickHouse is back, the org is still over and was already notified: no repeat email.
+ state.clickhouseError = null;
+ await usageService.updateOrganizationsMonthlyUsage();
+
+ expect(state.updates[0]).toMatchObject({ monthlyEventCount: 10_000_000, overMonthlyLimit: true });
+ expect(mocks.sendLimitExceededEmail).not.toHaveBeenCalled();
});
});
diff --git a/server/src/services/usageService.ts b/server/src/services/usageService.ts
index d3164a80c..e40fc0084 100644
--- a/server/src/services/usageService.ts
+++ b/server/src/services/usageService.ts
@@ -28,6 +28,18 @@ type OrgSite = { siteId: number; createdAt: string | null };
// IPC message a worker sends the cluster primary, which owns the blocked-site sets.
export const REFRESH_ORGANIZATION_USAGE_MESSAGE = "refresh-organization-usage";
+// A worker asks for the current blocked-site sets once its listener is installed; the
+// primary answers with USAGE_STATE_MESSAGE, which it also broadcasts after every update.
+export const USAGE_STATE_REQUEST_MESSAGE = "usage-state-request";
+export const USAGE_STATE_MESSAGE = "usage-state";
+
+export type UsageStateMessage = {
+ type: typeof USAGE_STATE_MESSAGE;
+ sitesOverLimit: number[];
+ sitesWithoutPlan: number[];
+ sitesWithoutReplay: number[];
+};
+
const REFRESH_RETRY_DELAYS_MS = [0, 15_000, 60_000];
class UsageService {
@@ -75,6 +87,23 @@ class UsageService {
this.sitesWithoutReplay = sites;
}
+ /** The blocked-site sets as one IPC message, so a worker never applies half of an update. */
+ public getUsageStateMessage(): UsageStateMessage {
+ return {
+ type: USAGE_STATE_MESSAGE,
+ sitesOverLimit: Array.from(this.sitesOverLimit),
+ sitesWithoutPlan: Array.from(this.sitesWithoutPlan),
+ sitesWithoutReplay: Array.from(this.sitesWithoutReplay),
+ };
+ }
+
+ /** Applies a USAGE_STATE_MESSAGE from the cluster primary (workers only). */
+ public applyUsageStateMessage(message: UsageStateMessage): void {
+ this.sitesOverLimit = new Set(message.sitesOverLimit);
+ this.sitesWithoutPlan = new Set(message.sitesWithoutPlan);
+ this.sitesWithoutReplay = new Set(message.sitesWithoutReplay);
+ }
+
/**
* Register a callback to be invoked after usage data is updated.
* Used by the cluster primary to broadcast sitesOverLimit to workers.
@@ -182,24 +211,20 @@ class UsageService {
}
/**
- * Gets all sites with their organization IDs (excludes sites without an organization)
+ * Gets all sites with their organization IDs (excludes sites without an organization).
+ * Throws on failure for the same reason as getAllSiteEventCounts: no sites means zero usage.
*/
private async getAllSites(): Promise<Array<OrgSite & { organizationId: string }>> {
- try {
- const allSites = await db
- .select({
- siteId: sites.siteId,
- organizationId: sites.organizationId,
- createdAt: sites.createdAt,
- })
- .from(sites);
-
- // Filter out sites without an organization ID
- return allSites.filter(site => site.organizationId !== null) as Array<OrgSite & { organizationId: string }>;
- } catch (error) {
- this.logger.error(error as Error, `Error getting all sites`);
- return [];
- }
+ const allSites = await db
+ .select({
+ siteId: sites.siteId,
+ organizationId: sites.organizationId,
+ createdAt: sites.createdAt,
+ })
+ .from(sites);
+
+ // Filter out sites without an organization ID
+ return allSites.filter(site => site.organizationId !== null) as Array<OrgSite & { organizationId: string }>;
}
/**
@@ -239,41 +264,37 @@ class UsageService {
/**
* Gets monthly event counts for all sites in a single query (for current month)
- * Returns a map of site_id -> event count
+ * Returns a map of site_id -> event count. Throws on failure: an empty map would read as
+ * "every organization is at zero" and lift every over-limit block.
*/
private async getAllSiteEventCounts(): Promise<Map<number, number>> {
- try {
- const periodStart = this.getStartOfMonth();
-
- const result = await clickhouse.query({
- query: `
- SELECT
- site_id,
- COUNT(*) as count
- FROM events
- WHERE type IN {types:Array(String)}
- AND timestamp >= toDate({periodStart:String})
- GROUP BY site_id
- `,
- format: "JSONEachRow",
- query_params: {
- periodStart: periodStart,
- types: [...USAGE_COUNTED_EVENT_TYPES],
- },
- });
-
- const rows = await processResults<{ site_id: number; count: string }>(result);
-
- const eventCountMap = new Map<number, number>();
- for (const row of rows) {
- eventCountMap.set(row.site_id, parseInt(row.count, 10));
- }
-
- return eventCountMap;
- } catch (error) {
- this.logger.error(error as Error, "Error querying ClickHouse for event counts");
- return new Map();
+ const periodStart = this.getStartOfMonth();
+
+ const result = await clickhouse.query({
+ query: `
+ SELECT
+ site_id,
+ COUNT(*) as count
+ FROM events
+ WHERE type IN {types:Array(String)}
+ AND timestamp >= toDate({periodStart:String})
+ GROUP BY site_id
+ `,
+ format: "JSONEachRow",
+ query_params: {
+ periodStart: periodStart,
+ types: [...USAGE_COUNTED_EVENT_TYPES],
+ },
+ });
+
+ const rows = await processResults<{ site_id: number; count: string }>(result);
+
+ const eventCountMap = new Map<number, number>();
+ for (const row of rows) {
+ eventCountMap.set(row.site_id, parseInt(row.count, 10));
}
+
+ return eventCountMap;
}
/**
@@ -338,11 +359,20 @@ class UsageService {
// Step 1: Get all sites with their organization IDs
const allSites = await this.getAllSites();
- // Step 2: Get event and replay counts for all sites (current month, one query each)
- const [eventCountMap, replayCountMap] = await Promise.all([
- this.getAllSiteEventCounts(),
- this.getAllSiteReplayCounts(),
- ]);
+ // Step 2: Get event and replay counts for all sites (current month, one query each). If
+ // the event count fails, skip the run like a Stripe failure: writing zeros would clear
+ // every over-limit block and re-send "limit exceeded" emails once the counts come back.
+ let eventCountMap: Map<number, number>;
+ let replayCountMap: Map<number, number>;
+ try {
+ [eventCountMap, replayCountMap] = await Promise.all([
+ this.getAllSiteEventCounts(),
+ this.getAllSiteReplayCounts(),
+ ]);
+ } catch (error) {
+ this.logger.error(error as Error, "Skipping usage check: failed to query ClickHouse for event counts");
+ return;
+ }
// Step 3: Build a map of organizationId -> { sites, eventCount, replayCount }
const orgDataMap = new Map<string, { sites: OrgSite[]; eventCount: number; replayCount: number }>();