Diff -- /var/www/vhosts/3dshawn.com/admin.3dshawn.com/MODS/MetaAdmin/Visitors.pm
Diff
/var/www/vhosts/3dshawn.com/admin.3dshawn.com/MODS/MetaAdmin/Visitors.pm
added on local at 2026-06-25 01:50:29
Added
+0
lines
Removed
-0
lines
Context
661
unchanged
Blobs
from 29228eeddc3c
to 29228eeddc3c
to 29228eeddc3c
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | 1 | package MODS::MetaAdmin::Visitors; |
| 2 | 2 | #====================================================================== |
| 3 | 3 | # Meta-Admin -- portfolio-wide visitor rollup. |
| 4 | 4 | # |
| 5 | 5 | # Read-only aggregator that queries each sister site's tracking_sessions |
| 6 | 6 | # table via SiteConn and merges results into a single dashboard view. |
| 7 | 7 | # |
| 8 | 8 | # Each site has its own schema flavor: |
| 9 | 9 | # * 5 PT-shape sites (WebSTLs, PTMatrix, AffSoft, ShopCart, RePricer) |
| 10 | 10 | # use `tracking_sessions` + `tracking_events` with country CHAR(2). |
| 11 | 11 | # * ABForge uses `mkt_tracking_*` (marketing site tables, NOT the |
| 12 | 12 | # product's heatmap tables). |
| 13 | 13 | # * ContactForge uses a CRM-flavored split: `tracking_sessions` keys |
| 14 | 14 | # by visitor_id rather than session_token; country lives in |
| 15 | 15 | # `country_iso CHAR(2)`. |
| 16 | 16 | # |
| 17 | 17 | # Per-site SQL templates handle the differences. |
| 18 | 18 | #====================================================================== |
| 19 | 19 | use strict; |
| 20 | 20 | use warnings; |
| 21 | 21 | |
| 22 | 22 | use MODS::MetaAdmin::SiteConn; |
| 23 | 23 | |
| 24 | 24 | sub new { |
| 25 | 25 | my $class = shift; |
| 26 | 26 | return bless({ sc => MODS::MetaAdmin::SiteConn->new }, $class); |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | # Per-slug schema template |
| 30 | 30 | my %TPL = ( |
| 31 | 31 | contactforge => { |
| 32 | 32 | sessions => 'tracking_sessions', |
| 33 | 33 | events => 'pageviews', |
| 34 | 34 | sess_id => 'visitor_id', |
| 35 | 35 | country => 'country_iso', |
| 36 | 36 | page_filter => '', |
| 37 | 37 | screen_w_col => 'screen_w', |
| 38 | 38 | screen_h_col => 'screen_h', |
| 39 | 39 | }, |
| 40 | 40 | abforge => { |
| 41 | 41 | sessions => 'mkt_tracking_sessions', |
| 42 | 42 | events => 'mkt_tracking_events', |
| 43 | 43 | sess_id => 'session_token', |
| 44 | 44 | country => 'country', |
| 45 | 45 | page_filter => "AND event_type='page_view'", |
| 46 | 46 | screen_w_col => 'screen_width', |
| 47 | 47 | screen_h_col => 'screen_height', |
| 48 | 48 | }, |
| 49 | 49 | ); |
| 50 | 50 | # Other 5 sites default to PT-shape |
| 51 | 51 | my %DEFAULT_TPL = ( |
| 52 | 52 | sessions => 'tracking_sessions', |
| 53 | 53 | events => 'tracking_events', |
| 54 | 54 | sess_id => 'session_token', |
| 55 | 55 | country => 'country', |
| 56 | 56 | page_filter => "AND event_type='page_view'", |
| 57 | 57 | screen_w_col => 'screen_width', |
| 58 | 58 | screen_h_col => 'screen_height', |
| 59 | 59 | ); |
| 60 | 60 | |
| 61 | 61 | sub _tpl { |
| 62 | 62 | my ($slug) = @_; |
| 63 | 63 | return $TPL{$slug} || \%DEFAULT_TPL; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | # Country centroid table for the rollup globe |
| 67 | 67 | my %COUNTRY_CENTROIDS = ( |
| 68 | 68 | US=>[39.5,-98.5,'United States'], CA=>[56.1,-106.3,'Canada'], MX=>[23.6,-102.5,'Mexico'], |
| 69 | 69 | GT=>[15.8,-90.2,'Guatemala'], CR=>[9.7,-83.7,'Costa Rica'], PA=>[8.5,-80.7,'Panama'], |
| 70 | 70 | CU=>[21.5,-77.7,'Cuba'], DO=>[18.7,-70.1,'Dominican Republic'], BR=>[-14.2,-51.9,'Brazil'], |
| 71 | 71 | AR=>[-38.4,-63.6,'Argentina'], CL=>[-35.6,-71.5,'Chile'], CO=>[4.6,-74.0,'Colombia'], |
| 72 | 72 | PE=>[-9.2,-75.0,'Peru'], VE=>[6.4,-66.6,'Venezuela'], EC=>[-1.8,-78.2,'Ecuador'], |
| 73 | 73 | UY=>[-32.5,-55.8,'Uruguay'], GB=>[55.4,-3.4,'United Kingdom'], IE=>[53.4,-8.2,'Ireland'], |
| 74 | 74 | FR=>[46.2,2.2,'France'], DE=>[51.2,10.4,'Germany'], NL=>[52.1,5.3,'Netherlands'], |
| 75 | 75 | BE=>[50.5,4.5,'Belgium'], LU=>[49.8,6.1,'Luxembourg'], CH=>[46.8,8.2,'Switzerland'], |
| 76 | 76 | AT=>[47.5,14.6,'Austria'], IT=>[41.9,12.6,'Italy'], ES=>[40.5,-3.7,'Spain'], |
| 77 | 77 | PT=>[39.4,-8.2,'Portugal'], DK=>[56.3,9.5,'Denmark'], NO=>[60.5,8.5,'Norway'], |
| 78 | 78 | SE=>[60.1,18.6,'Sweden'], FI=>[61.9,25.7,'Finland'], PL=>[51.9,19.1,'Poland'], |
| 79 | 79 | CZ=>[49.8,15.5,'Czechia'], SK=>[48.7,19.7,'Slovakia'], HU=>[47.2,19.5,'Hungary'], |
| 80 | 80 | RO=>[45.9,25.0,'Romania'], BG=>[42.7,25.5,'Bulgaria'], GR=>[39.1,21.8,'Greece'], |
| 81 | 81 | UA=>[48.4,31.2,'Ukraine'], RU=>[61.5,105.3,'Russia'], TR=>[38.9,35.2,'Turkey'], |
| 82 | 82 | IL=>[31.0,34.9,'Israel'], SA=>[23.9,45.1,'Saudi Arabia'], AE=>[23.4,53.8,'United Arab Emirates'], |
| 83 | 83 | IR=>[32.4,53.7,'Iran'], PK=>[30.4,69.3,'Pakistan'], IN=>[20.6,78.9,'India'], |
| 84 | 84 | BD=>[23.7,90.4,'Bangladesh'], LK=>[7.9,80.8,'Sri Lanka'], NP=>[28.4,84.1,'Nepal'], |
| 85 | 85 | CN=>[35.9,104.2,'China'], JP=>[36.2,138.3,'Japan'], KR=>[35.9,127.8,'South Korea'], |
| 86 | 86 | TW=>[23.7,121.0,'Taiwan'], HK=>[22.4,114.1,'Hong Kong'], SG=>[1.4,103.8,'Singapore'], |
| 87 | 87 | MY=>[4.2,101.9,'Malaysia'], TH=>[15.9,100.9,'Thailand'], VN=>[14.1,108.3,'Vietnam'], |
| 88 | 88 | PH=>[12.9,121.8,'Philippines'], ID=>[-0.8,113.9,'Indonesia'], AU=>[-25.3,133.8,'Australia'], |
| 89 | 89 | NZ=>[-40.9,174.9,'New Zealand'], ZA=>[-30.6,22.9,'South Africa'], NG=>[9.1,8.7,'Nigeria'], |
| 90 | 90 | EG=>[26.8,30.8,'Egypt'], MA=>[31.8,-7.1,'Morocco'], KE=>[-0.0,37.9,'Kenya'], |
| 91 | 91 | ET=>[9.1,40.5,'Ethiopia'], GH=>[7.9,-1.0,'Ghana'], DZ=>[28.0,1.7,'Algeria'], |
| 92 | 92 | TN=>[33.9,9.5,'Tunisia'], |
| 93 | 93 | ); |
| 94 | 94 | sub country_centroid { |
| 95 | 95 | my ($self, $code) = @_; |
| 96 | 96 | return undef unless defined $code; |
| 97 | 97 | $code = uc $code; $code =~ s/[^A-Z]//g; |
| 98 | 98 | return undef unless length($code) == 2; |
| 99 | 99 | return $COUNTRY_CENTROIDS{$code}; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | #--------------------------------------------------------------------- |
| 103 | 103 | # Roll up portfolio-wide KPIs over the last $days rolling window. |
| 104 | 104 | # Returns: { total_pageviews, total_visitors, total_sessions, days, |
| 105 | 105 | # per_site => [ { slug, name, color, pageviews, visitors, sessions, err } ] } |
| 106 | 106 | #--------------------------------------------------------------------- |
| 107 | 107 | sub kpi_rollup { |
| 108 | 108 | my ($self, $days) = @_; |
| 109 | 109 | $days = int($days || 30); $days = 1 if $days < 1; $days = 730 if $days > 730; |
| 110 | 110 | my $sites = $self->{sc}->list_sites(active => 1); |
| 111 | 111 | my %out = (total_pageviews => 0, total_visitors => 0, total_sessions => 0, days => $days, per_site => []); |
| 112 | 112 | foreach my $row (@$sites) { |
| 113 | 113 | my $slug = $row->{slug}; |
| 114 | 114 | my $tpl = _tpl($slug); |
| 115 | 115 | my $entry = { |
| 116 | 116 | slug => $slug, |
| 117 | 117 | display_name => $row->{display_name}, |
| 118 | 118 | color => $row->{brand_color} || '#807aa8', |
| 119 | 119 | pageviews => undef, |
| 120 | 120 | visitors => undef, |
| 121 | 121 | sessions => undef, |
| 122 | 122 | err => '', |
| 123 | 123 | }; |
| 124 | 124 | |
| 125 | 125 | # pageviews from the events table |
| 126 | 126 | my $pv_sql = qq~ |
| 127 | 127 | SELECT COUNT(*) AS n FROM $tpl->{events} |
| 128 | 128 | WHERE occurred_at >= DATE_SUB(NOW(), INTERVAL $days DAY) $tpl->{page_filter} |
| 129 | 129 | ~; |
| 130 | 130 | my $r = eval { $self->{sc}->query_one($slug, $pv_sql) }; |
| 131 | 131 | if ($r) { $entry->{pageviews} = ($r->{n} || 0) + 0; $out{total_pageviews} += $entry->{pageviews}; } |
| 132 | 132 | else { $entry->{err} = $self->{sc}->last_error || ''; } |
| 133 | 133 | |
| 134 | 134 | # visitors + sessions from sessions table |
| 135 | 135 | my $vs_sql = qq~ |
| 136 | 136 | SELECT COUNT(DISTINCT $tpl->{sess_id}) AS visitors, |
| 137 | 137 | COUNT(*) AS sessions |
| 138 | 138 | FROM $tpl->{sessions} |
| 139 | 139 | WHERE last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY) |
| 140 | 140 | ~; |
| 141 | 141 | $r = eval { $self->{sc}->query_one($slug, $vs_sql) }; |
| 142 | 142 | if ($r) { |
| 143 | 143 | $entry->{visitors} = ($r->{visitors} || 0) + 0; |
| 144 | 144 | $entry->{sessions} = ($r->{sessions} || 0) + 0; |
| 145 | 145 | $out{total_visitors} += $entry->{visitors}; |
| 146 | 146 | $out{total_sessions} += $entry->{sessions}; |
| 147 | 147 | } else { |
| 148 | 148 | $entry->{err} ||= $self->{sc}->last_error || ''; |
| 149 | 149 | } |
| 150 | 150 | push @{ $out{per_site} }, $entry; |
| 151 | 151 | } |
| 152 | 152 | return \%out; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | #--------------------------------------------------------------------- |
| 156 | 156 | # Roll up portfolio-wide country counts for the globe. |
| 157 | 157 | # Returns flat list of { code, name, lat, lng, count } merged across all sites. |
| 158 | 158 | #--------------------------------------------------------------------- |
| 159 | 159 | sub geo_rollup { |
| 160 | 160 | my ($self, $days) = @_; |
| 161 | 161 | $days = int($days || 30); $days = 1 if $days < 1; $days = 730 if $days > 730; |
| 162 | 162 | my $sites = $self->{sc}->list_sites(active => 1); |
| 163 | 163 | my %by_code; # ISO -> total count |
| 164 | 164 | foreach my $row (@$sites) { |
| 165 | 165 | my $slug = $row->{slug}; |
| 166 | 166 | my $tpl = _tpl($slug); |
| 167 | 167 | my $col = $tpl->{country}; |
| 168 | 168 | my $sql = qq~ |
| 169 | 169 | SELECT UPPER($col) AS code, COUNT(*) AS n |
| 170 | 170 | FROM $tpl->{sessions} |
| 171 | 171 | WHERE $col IS NOT NULL AND CHAR_LENGTH($col)=2 |
| 172 | 172 | AND last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY) |
| 173 | 173 | GROUP BY $col |
| 174 | 174 | ~; |
| 175 | 175 | my $rows = eval { $self->{sc}->query_many($slug, $sql) }; |
| 176 | 176 | next unless ref($rows) eq 'ARRAY'; |
| 177 | 177 | foreach my $r (@$rows) { |
| 178 | 178 | next unless ref($r) eq 'HASH'; |
| 179 | 179 | $by_code{ $r->{code} } += ($r->{n} || 0); |
| 180 | 180 | } |
| 181 | 181 | } |
| 182 | 182 | my @out; |
| 183 | 183 | foreach my $code (sort { $by_code{$b} <=> $by_code{$a} } keys %by_code) { |
| 184 | 184 | my $c = $self->country_centroid($code); |
| 185 | 185 | next unless $c; |
| 186 | 186 | push @out, { |
| 187 | 187 | code => $code, name => $c->[2], |
| 188 | 188 | lat => $c->[0], lng => $c->[1], |
| 189 | 189 | count => $by_code{$code}, |
| 190 | 190 | }; |
| 191 | 191 | } |
| 192 | 192 | return @out; |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | #--------------------------------------------------------------------- |
| 196 | 196 | # Per-site drill: ONE site's traffic broken down by country + region. |
| 197 | 197 | # Returns: { slug, display_name, color, total_visitors, total_sessions, |
| 198 | 198 | # countries => [ {code, name, visitors, sessions}, ... ], |
| 199 | 199 | # regions => [ {country, region, visitors, sessions}, ... ] } |
| 200 | 200 | # Returns undef if the slug isn't a known active site. |
| 201 | 201 | #--------------------------------------------------------------------- |
| 202 | 202 | sub site_drill { |
| 203 | 203 | my ($self, $slug, $days, $country, $region) = @_; |
| 204 | 204 | $days = int($days || 30); $days = 1 if $days < 1; $days = 730 if $days > 730; |
| 205 | 205 | return undef unless defined($slug) && length($slug); |
| 206 | 206 | |
| 207 | 207 | my $sites = $self->{sc}->list_sites(active => 1); |
| 208 | 208 | my $row; |
| 209 | 209 | foreach my $s (@$sites) { $row = $s if $s->{slug} eq $slug; } |
| 210 | 210 | return undef unless $row; |
| 211 | 211 | |
| 212 | 212 | # Normalize optional drill keys |
| 213 | 213 | $country =~ s/[^A-Za-z]//g if defined $country; |
| 214 | 214 | $country = uc(substr($country || '', 0, 2)); |
| 215 | 215 | $country = '' unless length($country) == 2; |
| 216 | 216 | $region //= ''; |
| 217 | 217 | # Region is a free-form display string -- only used as a literal in |
| 218 | 218 | # the GROUP BY filter. Strip dangerous chars before interpolating. |
| 219 | 219 | $region =~ s/[\\']//g; |
| 220 | 220 | $region = substr($region, 0, 120); |
| 221 | 221 | |
| 222 | 222 | my $tpl = _tpl($slug); |
| 223 | 223 | |
| 224 | 224 | # Total visitors + sessions for the active filter scope |
| 225 | 225 | my $where = "last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY)"; |
| 226 | 226 | $where .= " AND UPPER($tpl->{country})='$country'" if length($country); |
| 227 | 227 | $where .= " AND region='$region'" if length($region); |
| 228 | 228 | my $tot = eval { $self->{sc}->query_one($slug, qq~ |
| 229 | 229 | SELECT COUNT(DISTINCT $tpl->{sess_id}) AS visitors, |
| 230 | 230 | COUNT(*) AS sessions |
| 231 | 231 | FROM $tpl->{sessions} |
| 232 | 232 | WHERE $where |
| 233 | 233 | ~) }; |
| 234 | 234 | my $tv = (ref($tot) eq 'HASH' && $tot->{visitors}) ? $tot->{visitors} + 0 : 0; |
| 235 | 235 | my $ts = (ref($tot) eq 'HASH' && $tot->{sessions}) ? $tot->{sessions} + 0 : 0; |
| 236 | 236 | |
| 237 | 237 | my %out = ( |
| 238 | 238 | slug => $slug, |
| 239 | 239 | display_name => $row->{display_name}, |
| 240 | 240 | color => $row->{brand_color} || '#807aa8', |
| 241 | 241 | total_visitors => $tv, |
| 242 | 242 | total_sessions => $ts, |
| 243 | 243 | level => length($region) ? 'cities' : length($country) ? 'states' : 'countries', |
| 244 | 244 | country_code => $country, |
| 245 | 245 | country_name => length($country) ? (($self->country_centroid($country) || [0,0,$country])->[2]) : '', |
| 246 | 246 | region_name => $region, |
| 247 | 247 | countries => [], |
| 248 | 248 | states => [], |
| 249 | 249 | cities => [], |
| 250 | 250 | ); |
| 251 | 251 | |
| 252 | 252 | if (!length($country)) { |
| 253 | 253 | # Level 1 -- countries with traffic |
| 254 | 254 | my $sql = qq~ |
| 255 | 255 | SELECT UPPER($tpl->{country}) AS code, |
| 256 | 256 | COUNT(DISTINCT $tpl->{sess_id}) AS visitors, |
| 257 | 257 | COUNT(*) AS sessions |
| 258 | 258 | FROM $tpl->{sessions} |
| 259 | 259 | WHERE $tpl->{country} IS NOT NULL AND CHAR_LENGTH($tpl->{country})=2 |
| 260 | 260 | AND last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY) |
| 261 | 261 | GROUP BY $tpl->{country} |
| 262 | 262 | ORDER BY visitors DESC |
| 263 | 263 | LIMIT 60 |
| 264 | 264 | ~; |
| 265 | 265 | my $rows = eval { $self->{sc}->query_many($slug, $sql) }; |
| 266 | 266 | if (ref($rows) eq 'ARRAY') { |
| 267 | 267 | foreach my $r (@$rows) { |
| 268 | 268 | next unless ref($r) eq 'HASH'; |
| 269 | 269 | my $c = $self->country_centroid($r->{code}); |
| 270 | 270 | push @{ $out{countries} }, { |
| 271 | 271 | code => $r->{code}, |
| 272 | 272 | name => $c ? $c->[2] : $r->{code}, |
| 273 | 273 | visitors => ($r->{visitors} || 0) + 0, |
| 274 | 274 | sessions => ($r->{sessions} || 0) + 0, |
| 275 | 275 | }; |
| 276 | 276 | } |
| 277 | 277 | } |
| 278 | 278 | } |
| 279 | 279 | elsif (!length($region)) { |
| 280 | 280 | # Level 2 -- states (regions) within the selected country |
| 281 | 281 | my $sql = qq~ |
| 282 | 282 | SELECT COALESCE(NULLIF(region,''),'(unknown)') AS region, |
| 283 | 283 | COUNT(DISTINCT $tpl->{sess_id}) AS visitors, |
| 284 | 284 | COUNT(*) AS sessions |
| 285 | 285 | FROM $tpl->{sessions} |
| 286 | 286 | WHERE UPPER($tpl->{country})='$country' |
| 287 | 287 | AND last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY) |
| 288 | 288 | GROUP BY region |
| 289 | 289 | ORDER BY visitors DESC |
| 290 | 290 | LIMIT 50 |
| 291 | 291 | ~; |
| 292 | 292 | my $rows = eval { $self->{sc}->query_many($slug, $sql) }; |
| 293 | 293 | if (ref($rows) eq 'ARRAY') { |
| 294 | 294 | foreach my $r (@$rows) { |
| 295 | 295 | next unless ref($r) eq 'HASH'; |
| 296 | 296 | push @{ $out{states} }, { |
| 297 | 297 | region => $r->{region}, |
| 298 | 298 | visitors => ($r->{visitors} || 0) + 0, |
| 299 | 299 | sessions => ($r->{sessions} || 0) + 0, |
| 300 | 300 | }; |
| 301 | 301 | } |
| 302 | 302 | } |
| 303 | 303 | } |
| 304 | 304 | else { |
| 305 | 305 | # Level 3 -- cities within the selected state of the selected country |
| 306 | 306 | my $sql = qq~ |
| 307 | 307 | SELECT COALESCE(NULLIF(city,''),'(unknown)') AS city, |
| 308 | 308 | COUNT(DISTINCT $tpl->{sess_id}) AS visitors, |
| 309 | 309 | COUNT(*) AS sessions |
| 310 | 310 | FROM $tpl->{sessions} |
| 311 | 311 | WHERE UPPER($tpl->{country})='$country' |
| 312 | 312 | AND region='$region' |
| 313 | 313 | AND last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY) |
| 314 | 314 | GROUP BY city |
| 315 | 315 | ORDER BY visitors DESC |
| 316 | 316 | LIMIT 50 |
| 317 | 317 | ~; |
| 318 | 318 | my $rows = eval { $self->{sc}->query_many($slug, $sql) }; |
| 319 | 319 | if (ref($rows) eq 'ARRAY') { |
| 320 | 320 | foreach my $r (@$rows) { |
| 321 | 321 | next unless ref($r) eq 'HASH'; |
| 322 | 322 | push @{ $out{cities} }, { |
| 323 | 323 | city => $r->{city}, |
| 324 | 324 | visitors => ($r->{visitors} || 0) + 0, |
| 325 | 325 | sessions => ($r->{sessions} || 0) + 0, |
| 326 | 326 | }; |
| 327 | 327 | } |
| 328 | 328 | } |
| 329 | 329 | } |
| 330 | 330 | return \%out; |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | #--------------------------------------------------------------------- |
| 334 | 334 | # State-level rollup for the globe's state-zoom dots. Returns: |
| 335 | 335 | # { US => { 'New York' => 25, 'Colorado' => 8, ... }, GB => {...}, ... } |
| 336 | 336 | # Aggregates region counts across every active site in the portfolio. |
| 337 | 337 | #--------------------------------------------------------------------- |
| 338 | 338 | sub state_rollup { |
| 339 | 339 | my ($self, $days) = @_; |
| 340 | 340 | $days = int($days || 30); $days = 1 if $days < 1; $days = 730 if $days > 730; |
| 341 | 341 | my $sites = $self->{sc}->list_sites(active => 1); |
| 342 | 342 | my %out; |
| 343 | 343 | foreach my $row (@$sites) { |
| 344 | 344 | my $slug = $row->{slug}; |
| 345 | 345 | my $tpl = _tpl($slug); |
| 346 | 346 | my $sql = qq~ |
| 347 | 347 | SELECT UPPER($tpl->{country}) AS code, |
| 348 | 348 | region, |
| 349 | 349 | COUNT(DISTINCT $tpl->{sess_id}) AS visitors |
| 350 | 350 | FROM $tpl->{sessions} |
| 351 | 351 | WHERE $tpl->{country} IS NOT NULL AND CHAR_LENGTH($tpl->{country})=2 |
| 352 | 352 | AND region IS NOT NULL AND region<>'' |
| 353 | 353 | AND last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY) |
| 354 | 354 | GROUP BY $tpl->{country}, region |
| 355 | 355 | ~; |
| 356 | 356 | my $rows = eval { $self->{sc}->query_many($slug, $sql) }; |
| 357 | 357 | next unless ref($rows) eq 'ARRAY'; |
| 358 | 358 | foreach my $r (@$rows) { |
| 359 | 359 | next unless ref($r) eq 'HASH'; |
| 360 | 360 | $out{ $r->{code} } ||= {}; |
| 361 | 361 | $out{ $r->{code} }{ $r->{region} } += ($r->{visitors} || 0); |
| 362 | 362 | } |
| 363 | 363 | } |
| 364 | 364 | return \%out; |
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | #--------------------------------------------------------------------- |
| 368 | 368 | # Full country -> state -> city tree for accordion drill rendering. |
| 369 | 369 | # One SQL per site (or zero if slug not active). Returns: |
| 370 | 370 | # { slug, display_name, color, total_visitors, total_sessions, |
| 371 | 371 | # countries => [ |
| 372 | 372 | # { code, name, visitors, sessions, |
| 373 | 373 | # states => [ |
| 374 | 374 | # { region, visitors, sessions, |
| 375 | 375 | # cities => [ { city, visitors, sessions }, ... ] |
| 376 | 376 | # } |
| 377 | 377 | # ] |
| 378 | 378 | # } |
| 379 | 379 | # ] |
| 380 | 380 | # } |
| 381 | 381 | #--------------------------------------------------------------------- |
| 382 | 382 | sub full_drill_tree { |
| 383 | 383 | my ($self, $slug, $days) = @_; |
| 384 | 384 | $days = int($days || 30); $days = 1 if $days < 1; $days = 730 if $days > 730; |
| 385 | 385 | return undef unless defined($slug) && length($slug); |
| 386 | 386 | |
| 387 | 387 | my $sites = $self->{sc}->list_sites(active => 1); |
| 388 | 388 | my $row; |
| 389 | 389 | foreach my $s (@$sites) { $row = $s if $s->{slug} eq $slug; } |
| 390 | 390 | return undef unless $row; |
| 391 | 391 | |
| 392 | 392 | my $tpl = _tpl($slug); |
| 393 | 393 | my %out = ( |
| 394 | 394 | slug => $slug, |
| 395 | 395 | display_name => $row->{display_name}, |
| 396 | 396 | color => $row->{brand_color} || '#807aa8', |
| 397 | 397 | total_visitors => 0, |
| 398 | 398 | total_sessions => 0, |
| 399 | 399 | countries => [], |
| 400 | 400 | ); |
| 401 | 401 | |
| 402 | 402 | # One SQL pulls every (country, region, city) tuple with counts. |
| 403 | 403 | my $sql = qq~ |
| 404 | 404 | SELECT UPPER($tpl->{country}) AS code, |
| 405 | 405 | COALESCE(NULLIF(region,''),'(unknown)') AS region, |
| 406 | 406 | COALESCE(NULLIF(city,''),'(unknown)') AS city, |
| 407 | 407 | COUNT(DISTINCT $tpl->{sess_id}) AS visitors, |
| 408 | 408 | COUNT(*) AS sessions |
| 409 | 409 | FROM $tpl->{sessions} |
| 410 | 410 | WHERE $tpl->{country} IS NOT NULL AND CHAR_LENGTH($tpl->{country})=2 |
| 411 | 411 | AND last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY) |
| 412 | 412 | GROUP BY $tpl->{country}, region, city |
| 413 | 413 | ORDER BY visitors DESC |
| 414 | 414 | ~; |
| 415 | 415 | my $rows = eval { $self->{sc}->query_many($slug, $sql) }; |
| 416 | 416 | return \%out unless ref($rows) eq 'ARRAY'; |
| 417 | 417 | |
| 418 | 418 | # Aggregate rows into a nested tree. |
| 419 | 419 | my %by_code; # code -> { name, visitors, sessions, states_by_name => { ... } } |
| 420 | 420 | foreach my $r (@$rows) { |
| 421 | 421 | next unless ref($r) eq 'HASH'; |
| 422 | 422 | my $code = $r->{code}; |
| 423 | 423 | my $region = $r->{region}; |
| 424 | 424 | my $city = $r->{city}; |
| 425 | 425 | my $v = ($r->{visitors} || 0) + 0; |
| 426 | 426 | my $s = ($r->{sessions} || 0) + 0; |
| 427 | 427 | |
| 428 | 428 | $by_code{$code} ||= { name => undef, visitors => 0, sessions => 0, states => {} }; |
| 429 | 429 | $by_code{$code}{visitors} += $v; |
| 430 | 430 | $by_code{$code}{sessions} += $s; |
| 431 | 431 | $out{total_visitors} += $v; |
| 432 | 432 | $out{total_sessions} += $s; |
| 433 | 433 | |
| 434 | 434 | my $state = $by_code{$code}{states}{$region} |
| 435 | 435 | ||= { visitors => 0, sessions => 0, cities => [] }; |
| 436 | 436 | $state->{visitors} += $v; |
| 437 | 437 | $state->{sessions} += $s; |
| 438 | 438 | push @{ $state->{cities} }, { city => $city, visitors => $v, sessions => $s }; |
| 439 | 439 | } |
| 440 | 440 | |
| 441 | 441 | # Resolve country display names + sort country/state lists desc by visitors. |
| 442 | 442 | foreach my $code (sort { $by_code{$b}{visitors} <=> $by_code{$a}{visitors} } keys %by_code) { |
| 443 | 443 | my $centroid = $self->country_centroid($code); |
| 444 | 444 | my $cname = $centroid ? $centroid->[2] : $code; |
| 445 | 445 | my $cdata = $by_code{$code}; |
| 446 | 446 | my @states; |
| 447 | 447 | foreach my $rname (sort { $cdata->{states}{$b}{visitors} <=> $cdata->{states}{$a}{visitors} } keys %{$cdata->{states}}) { |
| 448 | 448 | my $st = $cdata->{states}{$rname}; |
| 449 | 449 | my @cities = sort { $b->{visitors} <=> $a->{visitors} } @{ $st->{cities} }; |
| 450 | 450 | push @states, { |
| 451 | 451 | region => $rname, |
| 452 | 452 | visitors => $st->{visitors}, |
| 453 | 453 | sessions => $st->{sessions}, |
| 454 | 454 | cities => \@cities, |
| 455 | 455 | }; |
| 456 | 456 | } |
| 457 | 457 | push @{ $out{countries} }, { |
| 458 | 458 | code => $code, |
| 459 | 459 | name => $cname, |
| 460 | 460 | visitors => $cdata->{visitors}, |
| 461 | 461 | sessions => $cdata->{sessions}, |
| 462 | 462 | states => \@states, |
| 463 | 463 | }; |
| 464 | 464 | } |
| 465 | 465 | return \%out; |
| 466 | 466 | } |
| 467 | 467 | |
| 468 | 468 | #--------------------------------------------------------------------- |
| 469 | 469 | # Cross-portfolio per-country dimension breakdowns for the modal. |
| 470 | 470 | # Returns: { US => { |
| 471 | 471 | # browsers: { 'Chrome'=>200, 'Safari'=>50, ... }, |
| 472 | 472 | # os: { 'Windows'=>150, 'macOS'=>100, ... }, |
| 473 | 473 | # devices: { 'desktop'=>300, 'mobile'=>80, ... }, |
| 474 | 474 | # resolutions: { '1920 x 1080'=>100, ... }, |
| 475 | 475 | # viewports: { ... }, |
| 476 | 476 | # languages: { 'en-US'=>200, ... }, |
| 477 | 477 | # timezones: { 'America/New_York'=>80, ... } |
| 478 | 478 | # }, ... } |
| 479 | 479 | # Aggregates across every active site. Each dimension is one SQL per site. |
| 480 | 480 | #--------------------------------------------------------------------- |
| 481 | 481 | sub country_dimensions_rollup { |
| 482 | 482 | my ($self, $days) = @_; |
| 483 | 483 | $days = int($days || 30); $days = 1 if $days < 1; $days = 730 if $days > 730; |
| 484 | 484 | my $sites = $self->{sc}->list_sites(active => 1); |
| 485 | 485 | my %out; |
| 486 | 486 | foreach my $row (@$sites) { |
| 487 | 487 | my $slug = $row->{slug}; |
| 488 | 488 | my $tpl = _tpl($slug); |
| 489 | 489 | my $sw = $tpl->{screen_w_col} || 'screen_width'; |
| 490 | 490 | my $sh = $tpl->{screen_h_col} || 'screen_height'; |
| 491 | 491 | my @dims = ( |
| 492 | 492 | { key => 'browsers', col => "COALESCE(NULLIF(browser_name,''),'Unknown')" }, |
| 493 | 493 | { key => 'os', col => "COALESCE(NULLIF(os_name,''),'Unknown')" }, |
| 494 | 494 | { key => 'devices', col => "COALESCE(device_type,'unknown')" }, |
| 495 | 495 | { key => 'resolutions', col => "CONCAT(COALESCE($sw,'?'),' x ',COALESCE($sh,'?'))" }, |
| 496 | 496 | { key => 'viewports', col => "CONCAT(COALESCE(viewport_width,'?'),' x ',COALESCE(viewport_height,'?'))" }, |
| 497 | 497 | { key => 'languages', col => "COALESCE(NULLIF(language,''),'Unknown')" }, |
| 498 | 498 | { key => 'timezones', col => "COALESCE(NULLIF(timezone,''),'Unknown')" }, |
| 499 | 499 | ); |
| 500 | 500 | foreach my $d (@dims) { |
| 501 | 501 | my $sql = qq~ |
| 502 | 502 | SELECT UPPER($tpl->{country}) AS code, $d->{col} AS v, COUNT(*) AS n |
| 503 | 503 | FROM $tpl->{sessions} |
| 504 | 504 | WHERE $tpl->{country} IS NOT NULL AND CHAR_LENGTH($tpl->{country})=2 |
| 505 | 505 | AND last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY) |
| 506 | 506 | GROUP BY $tpl->{country}, $d->{col} |
| 507 | 507 | ~; |
| 508 | 508 | my $rows = eval { $self->{sc}->query_many($slug, $sql) }; |
| 509 | 509 | next unless ref($rows) eq 'ARRAY'; |
| 510 | 510 | foreach my $r (@$rows) { |
| 511 | 511 | next unless ref($r) eq 'HASH'; |
| 512 | 512 | $out{ $r->{code} } ||= { browsers => {}, os => {}, devices => {}, resolutions => {}, viewports => {}, languages => {}, timezones => {} }; |
| 513 | 513 | my $v = defined($r->{v}) ? $r->{v} : 'Unknown'; |
| 514 | 514 | next if $v eq '? x ?'; # drop totally-empty resolution buckets |
| 515 | 515 | $out{ $r->{code} }{ $d->{key} }{ $v } += ($r->{n} || 0); |
| 516 | 516 | } |
| 517 | 517 | } |
| 518 | 518 | } |
| 519 | 519 | return \%out; |
| 520 | 520 | } |
| 521 | 521 | |
| 522 | 522 | #--------------------------------------------------------------------- |
| 523 | 523 | # Per-state dimension breakdowns (browser/OS/device/etc) for the |
| 524 | 524 | # state-level modal. Returns: |
| 525 | 525 | # { US => { 'New York' => { browsers => {...}, os => {...}, ... } } } |
| 526 | 526 | # Same dimension set as country_dimensions_rollup, just additionally |
| 527 | 527 | # grouped by region. |
| 528 | 528 | #--------------------------------------------------------------------- |
| 529 | 529 | sub state_dimensions_rollup { |
| 530 | 530 | my ($self, $days) = @_; |
| 531 | 531 | $days = int($days || 30); $days = 1 if $days < 1; $days = 730 if $days > 730; |
| 532 | 532 | my $sites = $self->{sc}->list_sites(active => 1); |
| 533 | 533 | my %out; |
| 534 | 534 | foreach my $row (@$sites) { |
| 535 | 535 | my $slug = $row->{slug}; |
| 536 | 536 | my $tpl = _tpl($slug); |
| 537 | 537 | my $sw = $tpl->{screen_w_col} || 'screen_width'; |
| 538 | 538 | my $sh = $tpl->{screen_h_col} || 'screen_height'; |
| 539 | 539 | my @dims = ( |
| 540 | 540 | { key => 'browsers', col => "COALESCE(NULLIF(browser_name,''),'Unknown')" }, |
| 541 | 541 | { key => 'os', col => "COALESCE(NULLIF(os_name,''),'Unknown')" }, |
| 542 | 542 | { key => 'devices', col => "COALESCE(device_type,'unknown')" }, |
| 543 | 543 | { key => 'resolutions', col => "CONCAT(COALESCE($sw,'?'),' x ',COALESCE($sh,'?'))" }, |
| 544 | 544 | { key => 'viewports', col => "CONCAT(COALESCE(viewport_width,'?'),' x ',COALESCE(viewport_height,'?'))" }, |
| 545 | 545 | { key => 'languages', col => "COALESCE(NULLIF(language,''),'Unknown')" }, |
| 546 | 546 | { key => 'timezones', col => "COALESCE(NULLIF(timezone,''),'Unknown')" }, |
| 547 | 547 | ); |
| 548 | 548 | foreach my $d (@dims) { |
| 549 | 549 | my $sql = qq~ |
| 550 | 550 | SELECT UPPER($tpl->{country}) AS code, region, $d->{col} AS v, COUNT(*) AS n |
| 551 | 551 | FROM $tpl->{sessions} |
| 552 | 552 | WHERE $tpl->{country} IS NOT NULL AND CHAR_LENGTH($tpl->{country})=2 |
| 553 | 553 | AND region IS NOT NULL AND region<>'' |
| 554 | 554 | AND last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY) |
| 555 | 555 | GROUP BY $tpl->{country}, region, $d->{col} |
| 556 | 556 | ~; |
| 557 | 557 | my $rows = eval { $self->{sc}->query_many($slug, $sql) }; |
| 558 | 558 | next unless ref($rows) eq 'ARRAY'; |
| 559 | 559 | foreach my $r (@$rows) { |
| 560 | 560 | next unless ref($r) eq 'HASH'; |
| 561 | 561 | my $v = defined($r->{v}) ? $r->{v} : 'Unknown'; |
| 562 | 562 | next if $v eq '? x ?'; |
| 563 | 563 | $out{ $r->{code} } ||= {}; |
| 564 | 564 | $out{ $r->{code} }{ $r->{region} } ||= { browsers => {}, os => {}, devices => {}, resolutions => {}, viewports => {}, languages => {}, timezones => {} }; |
| 565 | 565 | $out{ $r->{code} }{ $r->{region} }{ $d->{key} }{ $v } += ($r->{n} || 0); |
| 566 | 566 | } |
| 567 | 567 | } |
| 568 | 568 | } |
| 569 | 569 | return \%out; |
| 570 | 570 | } |
| 571 | 571 | |
| 572 | 572 | #--------------------------------------------------------------------- |
| 573 | 573 | # Per-country -> per-state -> per-site breakdown for the state modal. |
| 574 | 574 | # Returns: { US => { 'New York' => [ {slug,name,color,visitors,sessions}, ... ] } } |
| 575 | 575 | #--------------------------------------------------------------------- |
| 576 | 576 | sub state_per_site_rollup { |
| 577 | 577 | my ($self, $days) = @_; |
| 578 | 578 | $days = int($days || 30); $days = 1 if $days < 1; $days = 730 if $days > 730; |
| 579 | 579 | my $sites = $self->{sc}->list_sites(active => 1); |
| 580 | 580 | my %out; |
| 581 | 581 | foreach my $row (@$sites) { |
| 582 | 582 | my $slug = $row->{slug}; |
| 583 | 583 | my $tpl = _tpl($slug); |
| 584 | 584 | my $sql = qq~ |
| 585 | 585 | SELECT UPPER($tpl->{country}) AS code, |
| 586 | 586 | region, |
| 587 | 587 | COUNT(DISTINCT $tpl->{sess_id}) AS visitors, |
| 588 | 588 | COUNT(*) AS sessions |
| 589 | 589 | FROM $tpl->{sessions} |
| 590 | 590 | WHERE $tpl->{country} IS NOT NULL AND CHAR_LENGTH($tpl->{country})=2 |
| 591 | 591 | AND region IS NOT NULL AND region<>'' |
| 592 | 592 | AND last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY) |
| 593 | 593 | GROUP BY $tpl->{country}, region |
| 594 | 594 | ~; |
| 595 | 595 | my $rows = eval { $self->{sc}->query_many($slug, $sql) }; |
| 596 | 596 | next unless ref($rows) eq 'ARRAY'; |
| 597 | 597 | foreach my $r (@$rows) { |
| 598 | 598 | next unless ref($r) eq 'HASH'; |
| 599 | 599 | $out{ $r->{code} } ||= {}; |
| 600 | 600 | $out{ $r->{code} }{ $r->{region} } ||= []; |
| 601 | 601 | push @{ $out{ $r->{code} }{ $r->{region} } }, { |
| 602 | 602 | slug => $slug, |
| 603 | 603 | name => $row->{display_name}, |
| 604 | 604 | color => $row->{brand_color} || '#807aa8', |
| 605 | 605 | visitors => ($r->{visitors} || 0) + 0, |
| 606 | 606 | sessions => ($r->{sessions} || 0) + 0, |
| 607 | 607 | }; |
| 608 | 608 | } |
| 609 | 609 | } |
| 610 | 610 | foreach my $code (keys %out) { |
| 611 | 611 | foreach my $region (keys %{ $out{$code} }) { |
| 612 | 612 | $out{$code}{$region} = [ sort { $b->{visitors} <=> $a->{visitors} } @{ $out{$code}{$region} } ]; |
| 613 | 613 | } |
| 614 | 614 | } |
| 615 | 615 | return \%out; |
| 616 | 616 | } |
| 617 | 617 | |
| 618 | 618 | #--------------------------------------------------------------------- |
| 619 | 619 | # Per-country -> per-site breakdown for the globe popover. |
| 620 | 620 | # Returns a hashref keyed by ISO code: |
| 621 | 621 | # { US => [ {slug, name, color, visitors, sessions}, ... ], ... } |
| 622 | 622 | # Only countries with at least one site contribution are included. |
| 623 | 623 | #--------------------------------------------------------------------- |
| 624 | 624 | sub country_per_site_rollup { |
| 625 | 625 | my ($self, $days) = @_; |
| 626 | 626 | $days = int($days || 30); $days = 1 if $days < 1; $days = 730 if $days > 730; |
| 627 | 627 | my $sites = $self->{sc}->list_sites(active => 1); |
| 628 | 628 | my %out; |
| 629 | 629 | foreach my $row (@$sites) { |
| 630 | 630 | my $slug = $row->{slug}; |
| 631 | 631 | my $tpl = _tpl($slug); |
| 632 | 632 | my $sql = qq~ |
| 633 | 633 | SELECT UPPER($tpl->{country}) AS code, |
| 634 | 634 | COUNT(DISTINCT $tpl->{sess_id}) AS visitors, |
| 635 | 635 | COUNT(*) AS sessions |
| 636 | 636 | FROM $tpl->{sessions} |
| 637 | 637 | WHERE $tpl->{country} IS NOT NULL AND CHAR_LENGTH($tpl->{country})=2 |
| 638 | 638 | AND last_seen_at >= DATE_SUB(NOW(), INTERVAL $days DAY) |
| 639 | 639 | GROUP BY $tpl->{country} |
| 640 | 640 | ~; |
| 641 | 641 | my $rows = eval { $self->{sc}->query_many($slug, $sql) }; |
| 642 | 642 | next unless ref($rows) eq 'ARRAY'; |
| 643 | 643 | foreach my $r (@$rows) { |
| 644 | 644 | next unless ref($r) eq 'HASH'; |
| 645 | 645 | $out{ $r->{code} } ||= []; |
| 646 | 646 | push @{ $out{ $r->{code} } }, { |
| 647 | 647 | slug => $slug, |
| 648 | 648 | name => $row->{display_name}, |
| 649 | 649 | color => $row->{brand_color} || '#807aa8', |
| 650 | 650 | visitors => ($r->{visitors} || 0) + 0, |
| 651 | 651 | sessions => ($r->{sessions} || 0) + 0, |
| 652 | 652 | }; |
| 653 | 653 | } |
| 654 | 654 | } |
| 655 | 655 | foreach my $code (keys %out) { |
| 656 | 656 | $out{$code} = [ sort { $b->{visitors} <=> $a->{visitors} } @{ $out{$code} } ]; |
| 657 | 657 | } |
| 658 | 658 | return \%out; |
| 659 | 659 | } |
| 660 | 660 | |
| 661 | 661 | 1; |