Diff -- /var/www/vhosts/3dshawn.com/affiliate.3dshawn.com/admin_traffic.cgi
Diff
/var/www/vhosts/3dshawn.com/affiliate.3dshawn.com/admin_traffic.cgi
added on local at 2026-07-06 01:09:38
Added
+468
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 63b6e827bbba
to 63b6e827bbba
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # AffSoft -- Admin Traffic Reports. | |
| 4 | # Tabs: overview | sources | devices | geo | pages | |
| 5 | # All queries cover external traffic to this site's marketing pages. | |
| 6 | # Generated from canonical PT-shape admin_traffic.cgi template. | |
| 7 | #====================================================================== | |
| 8 | use strict; | |
| 9 | use warnings; | |
| 10 | ||
| 11 | use lib '/var/www/vhosts/3dshawn.com/affiliate.3dshawn.com'; | |
| 12 | use CGI; | |
| 13 | use MODS::Template; | |
| 14 | use MODS::DBConnect; | |
| 15 | use MODS::Login; | |
| 16 | use MODS::AffSoft::Config; | |
| 17 | use MODS::AffSoft::Wrapper; | |
| 18 | use MODS::AffSoft::Stats; | |
| 19 | ||
| 20 | my $q = CGI->new; | |
| 21 | my $form = $q->Vars; | |
| 22 | my $auth = MODS::Login->new; | |
| 23 | my $wrap = MODS::AffSoft::Wrapper->new; | |
| 24 | my $tfile = MODS::Template->new; | |
| 25 | my $db = MODS::DBConnect->new; | |
| 26 | my $cfg = MODS::AffSoft::Config->new; | |
| 27 | my $stats = MODS::AffSoft::Stats->new; | |
| 28 | my $DB = $cfg->settings('database_name'); | |
| 29 | ||
| 30 | $|=1; | |
| 31 | my $userinfo = $auth->login_verify(); | |
| 32 | unless ($userinfo) { | |
| 33 | print "Status: 302 Found\nLocation: /login.cgi?return=/admin_traffic.cgi\n\n"; | |
| 34 | exit; | |
| 35 | } | |
| 36 | unless ($userinfo->{is_admin} || $userinfo->{is_super_admin}) { | |
| 37 | print "Status: 403 Forbidden\nContent-Type: text/plain\n\nAdmin access required.\n"; | |
| 38 | exit; | |
| 39 | } | |
| 40 | ||
| 41 | sub _h { | |
| 42 | my $s = shift; $s = '' unless defined $s; | |
| 43 | $s =~ s/&/&/g; $s =~ s/</</g; $s =~ s/>/>/g; | |
| 44 | $s =~ s/"/"/g; $s =~ s/'/'/g; | |
| 45 | return $s; | |
| 46 | } | |
| 47 | sub _jstr { | |
| 48 | my $s = shift; $s = '' unless defined $s; | |
| 49 | $s =~ s/\\/\\\\/g; $s =~ s/"/\\"/g; $s =~ s/[\x00-\x1f]//g; | |
| 50 | return $s; | |
| 51 | } | |
| 52 | ||
| 53 | my $tab = lc($form->{tab} || 'overview'); | |
| 54 | $tab =~ s/[^a-z]//g; | |
| 55 | $tab = 'overview' unless $tab =~ /^(overview|sources|devices|geo|pages)$/; | |
| 56 | ||
| 57 | my $days = $form->{days}; | |
| 58 | $days =~ s/[^0-9]//g if defined $days; | |
| 59 | $days = 30 unless $days && $days =~ /^\d+$/; | |
| 60 | $days = 730 if $days > 730; | |
| 61 | $days = 1 if $days < 1; | |
| 62 | ||
| 63 | sub _parse_dt { | |
| 64 | my $s = shift; | |
| 65 | return undef unless defined($s) && length($s); | |
| 66 | $s =~ s/[^0-9 :T\-]//g; | |
| 67 | if ($s =~ /^(\d{4}-\d{2}-\d{2})[T ](\d{2}:\d{2})(?::\d{2})?$/) { return "$1 $2:00"; } | |
| 68 | if ($s =~ /^(\d{4}-\d{2}-\d{2})$/) { return "$1 00:00:00"; } | |
| 69 | return undef; | |
| 70 | } | |
| 71 | my $abs_from = _parse_dt($form->{from}); | |
| 72 | my $abs_to = _parse_dt($form->{to}); | |
| 73 | my $range; | |
| 74 | my $is_absolute = 0; | |
| 75 | if ($abs_from && $abs_to) { $range = { from => $abs_from, to => $abs_to }; $is_absolute = 1; } | |
| 76 | else { $range = $days; } | |
| 77 | $stats->set_range($range); | |
| 78 | ||
| 79 | my $dbh = $db->db_connect(); | |
| 80 | ||
| 81 | my $schema_ready = 0; | |
| 82 | { | |
| 83 | my $r = $db->db_readwrite($dbh, qq~ | |
| 84 | SELECT COUNT(*) AS n FROM information_schema.tables | |
| 85 | WHERE table_schema='$DB' AND table_name IN ('tracking_sessions','tracking_events') | |
| 86 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 87 | $schema_ready = ($r && $r->{n} && $r->{n} >= 2) ? 1 : 0; | |
| 88 | } | |
| 89 | ||
| 90 | my $kpis = $schema_ready ? $stats->kpis($db, $dbh, $DB) : {}; | |
| 91 | ||
| 92 | sub _rows_with_pct { | |
| 93 | my ($aref, $label_key, $count_key) = @_; | |
| 94 | my $total = 0; $total += ($_->{$count_key} || 0) for @$aref; | |
| 95 | my @out; | |
| 96 | foreach my $r (@$aref) { | |
| 97 | my $n = $r->{$count_key} || 0; | |
| 98 | my $pct = $total ? sprintf('%.1f', ($n / $total) * 100) : '0.0'; | |
| 99 | push @out, { label => _h($r->{$label_key} || 'Unknown'), count => $n, pct => $pct }; | |
| 100 | } | |
| 101 | return \@out; | |
| 102 | } | |
| 103 | ||
| 104 | my $tvars = { | |
| 105 | schema_ready => $schema_ready ? 1 : 0, | |
| 106 | schema_missing => $schema_ready ? 0 : 1, | |
| 107 | tab_overview => ($tab eq 'overview') ? 1 : 0, | |
| 108 | tab_sources => ($tab eq 'sources') ? 1 : 0, | |
| 109 | tab_devices => ($tab eq 'devices') ? 1 : 0, | |
| 110 | tab_geo => ($tab eq 'geo') ? 1 : 0, | |
| 111 | tab_pages => ($tab eq 'pages') ? 1 : 0, | |
| 112 | days => $days, | |
| 113 | is_absolute => $is_absolute, | |
| 114 | is_rolling => $is_absolute ? 0 : 1, | |
| 115 | range_from => $is_absolute ? _h($abs_from) : '', | |
| 116 | range_to => $is_absolute ? _h($abs_to) : '', | |
| 117 | range_from_input => $is_absolute ? do { (my $x = $abs_from) =~ s/ /T/; substr($x, 0, 16) } : '', | |
| 118 | range_to_input => $is_absolute ? do { (my $x = $abs_to) =~ s/ /T/; substr($x, 0, 16) } : '', | |
| 119 | range_label => $is_absolute ? "$abs_from -- $abs_to" : "Last $days days", | |
| 120 | preset_24h => (!$is_absolute && $days == 1) ? 1 : 0, | |
| 121 | preset_7d => (!$is_absolute && $days == 7) ? 1 : 0, | |
| 122 | preset_30d => (!$is_absolute && $days == 30) ? 1 : 0, | |
| 123 | preset_90d => (!$is_absolute && $days == 90) ? 1 : 0, | |
| 124 | preset_1y => (!$is_absolute && $days == 365) ? 1 : 0, | |
| 125 | preset_custom => $is_absolute ? 1 : 0, | |
| 126 | kpi_pageviews => $kpis->{pageviews} || 0, | |
| 127 | kpi_visitors => $kpis->{visitors} || 0, | |
| 128 | kpi_sessions => $kpis->{sessions} || 0, | |
| 129 | kpi_avg_seconds => $kpis->{avg_seconds} || 0, | |
| 130 | kpi_bounce_rate => $kpis->{bounce_rate} || 0, | |
| 131 | kpi_pv_per_session => $kpis->{pv_per_session} || 0, | |
| 132 | }; | |
| 133 | ||
| 134 | if ($schema_ready && ($tab eq 'overview' || $tab eq 'geo')) { | |
| 135 | my @globe = $stats->by_country_geo($db, $dbh, $DB, 80); | |
| 136 | my @json_rows; | |
| 137 | foreach my $g (@globe) { | |
| 138 | push @json_rows, sprintf('{"code":"%s","name":"%s","lat":%s,"lng":%s,"count":%d}', | |
| 139 | $g->{code}, _jstr($g->{name}), $g->{lat}+0, $g->{lng}+0, $g->{visitors}+0); | |
| 140 | } | |
| 141 | $tvars->{globe_json} = '[' . join(',', @json_rows) . ']'; | |
| 142 | # Always render the globe, even with zero countries. Empty array still | |
| 143 | # gives users the rotating wireframe so they know what to expect once | |
| 144 | # visitors start flowing. | |
| 145 | $tvars->{has_globe} = 1; | |
| 146 | ||
| 147 | # State-level traffic for globe.js zoom-in dots. | |
| 148 | my $sr = $stats->state_rollup($db, $dbh, $DB); | |
| 149 | my @sparts; | |
| 150 | foreach my $code (sort keys %$sr) { | |
| 151 | my @rp; | |
| 152 | foreach my $region (sort keys %{ $sr->{$code} }) { | |
| 153 | my $rj = $region; $rj =~ s/\\/\\\\/g; $rj =~ s/"/\\"/g; $rj =~ s/[\x00-\x1f]//g; | |
| 154 | push @rp, sprintf('"%s":%d', $rj, $sr->{$code}{$region}+0); | |
| 155 | } | |
| 156 | push @sparts, sprintf('"%s":{%s}', $code, join(',', @rp)); | |
| 157 | } | |
| 158 | $tvars->{state_json} = '{' . join(',', @sparts) . '}'; | |
| 159 | ||
| 160 | # Full state/city tree JSON for the modal's state-level drill. | |
| 161 | # Structure: { US: { states: { 'New York': { visitors, sessions, | |
| 162 | # cities: [{city, visitors, sessions}, ...] } } } } | |
| 163 | my $tree2 = $stats->full_drill_tree($db, $dbh, $DB); | |
| 164 | my @tparts; | |
| 165 | foreach my $cc (@{ $tree2->{countries} }) { | |
| 166 | my @sparts2; | |
| 167 | foreach my $st (@{ $cc->{states} }) { | |
| 168 | my @cparts; | |
| 169 | foreach my $ct (@{ $st->{cities} }) { | |
| 170 | my $cj = $ct->{city}; $cj =~ s/\\/\\\\/g; $cj =~ s/"/\\"/g; $cj =~ s/[\x00-\x1f]//g; | |
| 171 | push @cparts, sprintf('{"city":"%s","visitors":%d,"sessions":%d}', $cj, $ct->{visitors}+0, $ct->{sessions}+0); | |
| 172 | } | |
| 173 | my $rj = $st->{region}; $rj =~ s/\\/\\\\/g; $rj =~ s/"/\\"/g; $rj =~ s/[\x00-\x1f]//g; | |
| 174 | push @sparts2, sprintf('"%s":{"visitors":%d,"sessions":%d,"cities":[%s]}', $rj, $st->{visitors}+0, $st->{sessions}+0, join(',', @cparts)); | |
| 175 | } | |
| 176 | push @tparts, sprintf('"%s":{"visitors":%d,"sessions":%d,"states":{%s}}', $cc->{code}, $cc->{visitors}+0, $cc->{sessions}+0, join(',', @sparts2)); | |
| 177 | } | |
| 178 | $tvars->{tree_json} = '{' . join(',', @tparts) . '}'; | |
| 179 | ||
| 180 | # Per-country dimension breakdowns JSON. | |
| 181 | my $dims = $stats->country_dimensions_rollup($db, $dbh, $DB); | |
| 182 | my @dparts; | |
| 183 | foreach my $code (sort keys %$dims) { | |
| 184 | my @dx; | |
| 185 | foreach my $dim (qw(browsers os devices resolutions viewports languages timezones)) { | |
| 186 | my $rh = $dims->{$code}{$dim} || {}; | |
| 187 | my @vp; | |
| 188 | foreach my $v (sort { $rh->{$b} <=> $rh->{$a} } keys %$rh) { | |
| 189 | my $vj = $v; $vj =~ s/\\/\\\\/g; $vj =~ s/"/\\"/g; $vj =~ s/[\x00-\x1f]//g; | |
| 190 | push @vp, sprintf('"%s":%d', $vj, $rh->{$v}+0); | |
| 191 | } | |
| 192 | push @dx, sprintf('"%s":{%s}', $dim, join(',', @vp)); | |
| 193 | } | |
| 194 | push @dparts, sprintf('"%s":{%s}', $code, join(',', @dx)); | |
| 195 | } | |
| 196 | $tvars->{dims_json} = '{' . join(',', @dparts) . '}'; | |
| 197 | ||
| 198 | # Site-wide overall audience breakdown (flatten dims across countries). | |
| 199 | { | |
| 200 | my %overall; | |
| 201 | foreach my $code (keys %$dims) { | |
| 202 | foreach my $dim (qw(browsers os devices resolutions viewports languages timezones)) { | |
| 203 | my $rh = $dims->{$code}{$dim} || {}; | |
| 204 | foreach my $v (keys %$rh) { $overall{$dim}{$v} += $rh->{$v}; } | |
| 205 | } | |
| 206 | } | |
| 207 | my @oparts; | |
| 208 | foreach my $dim (qw(browsers os devices resolutions viewports languages timezones)) { | |
| 209 | my $rh = $overall{$dim} || {}; | |
| 210 | my @vp; | |
| 211 | foreach my $v (sort { $rh->{$b} <=> $rh->{$a} } keys %$rh) { | |
| 212 | my $vj = $v; $vj =~ s/\\/\\\\/g; $vj =~ s/"/\\"/g; $vj =~ s/[\x00-\x1f]//g; | |
| 213 | push @vp, sprintf('"%s":%d', $vj, $rh->{$v}+0); | |
| 214 | } | |
| 215 | push @oparts, sprintf('"%s":{%s}', $dim, join(',', @vp)); | |
| 216 | } | |
| 217 | $tvars->{overall_dims_json} = '{' . join(',', @oparts) . '}'; | |
| 218 | } | |
| 219 | ||
| 220 | # Per-state dimension breakdowns JSON. | |
| 221 | my $sdims = $stats->state_dimensions_rollup($db, $dbh, $DB); | |
| 222 | my @sdparts; | |
| 223 | foreach my $code (sort keys %$sdims) { | |
| 224 | my @rparts; | |
| 225 | foreach my $region (sort keys %{ $sdims->{$code} }) { | |
| 226 | my @dx; | |
| 227 | foreach my $dim (qw(browsers os devices resolutions viewports languages timezones)) { | |
| 228 | my $rh = $sdims->{$code}{$region}{$dim} || {}; | |
| 229 | my @vp; | |
| 230 | foreach my $v (sort { $rh->{$b} <=> $rh->{$a} } keys %$rh) { | |
| 231 | my $vj = $v; $vj =~ s/\\/\\\\/g; $vj =~ s/"/\\"/g; $vj =~ s/[\x00-\x1f]//g; | |
| 232 | push @vp, sprintf('"%s":%d', $vj, $rh->{$v}+0); | |
| 233 | } | |
| 234 | push @dx, sprintf('"%s":{%s}', $dim, join(',', @vp)); | |
| 235 | } | |
| 236 | my $rj = $region; $rj =~ s/\\/\\\\/g; $rj =~ s/"/\\"/g; $rj =~ s/[\x00-\x1f]//g; | |
| 237 | push @rparts, sprintf('"%s":{%s}', $rj, join(',', @dx)); | |
| 238 | } | |
| 239 | push @sdparts, sprintf('"%s":{%s}', $code, join(',', @rparts)); | |
| 240 | } | |
| 241 | $tvars->{sdims_json} = '{' . join(',', @sdparts) . '}'; | |
| 242 | ||
| 243 | # Total country count for the new Meta-Admin-style "Countries" KPI tile. | |
| 244 | $tvars->{kpi_countries} = scalar(@globe); | |
| 245 | } | |
| 246 | ||
| 247 | if ($schema_ready && $tab eq 'overview') { | |
| 248 | if (!$is_absolute) { | |
| 249 | my $series = $stats->daily_series($db, $dbh, $DB, $days); | |
| 250 | $tvars->{series_labels_json} = '["' . join('","', @{$series->{labels}}) . '"]'; | |
| 251 | $tvars->{series_pv_json} = '[' . join(',', @{$series->{pageviews}}) . ']'; | |
| 252 | $tvars->{series_vs_json} = '[' . join(',', @{$series->{visitors}}) . ']'; | |
| 253 | $tvars->{has_series} = scalar(@{$series->{labels}}) ? 1 : 0; | |
| 254 | } else { $tvars->{has_series} = 0; } | |
| 255 | my @src = $stats->by_source($db, $dbh, $DB, 8); | |
| 256 | my @br = $stats->by_browser($db, $dbh, $DB, 6); | |
| 257 | my @top = $stats->by_page($db, $dbh, $DB, 10); | |
| 258 | $tvars->{src_rows} = _rows_with_pct(\@src, 'source_kind', 'sessions'); | |
| 259 | $tvars->{has_src_rows} = scalar(@src) ? 1 : 0; | |
| 260 | $tvars->{br_rows} = _rows_with_pct(\@br, 'browser', 'sessions'); | |
| 261 | $tvars->{has_br_rows} = scalar(@br) ? 1 : 0; | |
| 262 | my @top_rows; | |
| 263 | foreach my $p (@top) { | |
| 264 | push @top_rows, { path => _h($p->{page_path} || '/'), pageviews => $p->{pageviews} || 0, sessions => $p->{sessions} || 0 }; | |
| 265 | } | |
| 266 | $tvars->{top_pages} = \@top_rows; | |
| 267 | $tvars->{has_top_pages} = scalar(@top_rows) ? 1 : 0; | |
| 268 | ||
| 269 | # ----- Accordion drill on Overview: full country -> state -> city tree ----- | |
| 270 | # Moved here from the Geo tab so the user sees it directly under the globe. | |
| 271 | my $tree = $stats->full_drill_tree($db, $dbh, $DB); | |
| 272 | my $has_countries = ($tree && scalar @{ $tree->{countries} } > 0) ? 1 : 0; | |
| 273 | my $acc_html = q~ | |
| 274 | <style> | |
| 275 | .accord-scope { position: relative; --acc-brand: var(--col-brand, var(--col-brand-2, var(--col-accent, #5aa9ff))); } | |
| 276 | .accord-collapse-all { background: var(--col-bg-2, #0e1822); color: var(--col-text-muted); border: 1px solid var(--col-border); padding: 5px 11px; border-radius: 7px; font-size: 11px; font-weight: 600; cursor: pointer; transition: all .12s ease; font-family: inherit; margin-left: auto; } | |
| 277 | .accord-collapse-all:hover { color: var(--col-text); border-color: var(--acc-brand); } | |
| 278 | .accord-tbl { width:100%; border-collapse: collapse; margin-bottom: 8px; } | |
| 279 | .accord-tbl th { background: rgba(255,255,255,.03); padding: 10px 14px; text-align: left; font-size: 10.5px; text-transform: uppercase; letter-spacing: 1.4px; color: var(--col-text-muted); font-weight: 700; border-bottom: 1px solid var(--col-border); } | |
| 280 | .accord-tbl td { padding: 11px 14px; border-bottom: 1px solid var(--col-border); font-size: 13px; vertical-align: middle; } | |
| 281 | .accord-tbl tr.expandable { cursor: pointer; transition: background .12s ease; } | |
| 282 | .accord-tbl tr.expandable:hover { background: color-mix(in srgb, var(--acc-brand) 6%, rgba(255,255,255,.02)); } | |
| 283 | .accord-tbl tr.expandable.is-open { background: color-mix(in srgb, var(--acc-brand) 10%, transparent); box-shadow: inset 3px 0 0 0 var(--acc-brand); } | |
| 284 | .accord-tbl .chev { display:inline-block; width:14px; color: var(--col-text-muted); transition: transform .15s ease, color .15s ease; margin-right:6px; } | |
| 285 | .accord-tbl tr.expandable.is-open .chev { transform: rotate(90deg); color: var(--acc-brand); } | |
| 286 | .accord-tbl tr.kids > td { padding: 0 !important; background: rgba(0,0,0,.18); border-bottom: 1px solid var(--col-border); } | |
| 287 | .accord-tbl tr.kids[hidden] { display: none; } | |
| 288 | .accord-tbl .nested { width:100%; border-collapse: collapse; } | |
| 289 | .accord-tbl .nested td { padding: 9px 14px 9px 36px; font-size: 12.5px; border-bottom: 1px solid rgba(255,255,255,.05); } | |
| 290 | .accord-tbl .nested .nested td { padding-left: 58px; font-size: 12px; } | |
| 291 | .accord-tbl .num { text-align:right; font-variant-numeric: tabular-nums; } | |
| 292 | .accord-tbl .leaf td { padding-left: 80px; color: var(--col-text-2); } | |
| 293 | .accord-tbl tr.accord-empty td { text-align:center; color: var(--col-text-muted); padding: 26px 14px; font-size: 12.5px; font-style: italic; } | |
| 294 | </style> | |
| 295 | <div class="accord-scope"> | |
| 296 | <div style="display:flex;align-items:center;margin-bottom:8px;gap:8px"><span style="font-size:11px;color:var(--col-text-dim)">click any row with the chevron to expand</span><button type="button" class="accord-collapse-all">Collapse all</button></div> | |
| 297 | <table class="accord-tbl"> | |
| 298 | <thead><tr><th>Country / state / city</th><th class="num">Visitors</th><th class="num">Sessions</th></tr></thead> | |
| 299 | <tbody> | |
| 300 | ~; | |
| 301 | if ($has_countries) { | |
| 302 | my $ci = 0; | |
| 303 | foreach my $cc (@{ $tree->{countries} }) { | |
| 304 | $ci++; | |
| 305 | my $cid = "atcnt-$ci"; | |
| 306 | my $cname = _h($cc->{name}); | |
| 307 | my $has_states = scalar @{ $cc->{states} } > 0 ? 1 : 0; | |
| 308 | my $chev = $has_states ? '<span class="chev">›</span>' : '<span class="chev" style="opacity:0">›</span>'; | |
| 309 | $acc_html .= qq~<tr class="expandable" data-target="$cid"><td><strong>$chev$cname</strong></td><td class="num">$cc->{visitors}</td><td class="num">$cc->{sessions}</td></tr>~; | |
| 310 | if ($has_states) { | |
| 311 | $acc_html .= qq~<tr class="kids" id="$cid" hidden><td colspan="3"><table class="nested"><tbody>~; | |
| 312 | my $si = 0; | |
| 313 | foreach my $st (@{ $cc->{states} }) { | |
| 314 | $si++; | |
| 315 | my $sid = "${cid}-st-$si"; | |
| 316 | my $rname = _h($st->{region}); | |
| 317 | my $has_cities = scalar @{ $st->{cities} } > 0 ? 1 : 0; | |
| 318 | my $schev = $has_cities ? '<span class="chev">›</span>' : '<span class="chev" style="opacity:0">›</span>'; | |
| 319 | $acc_html .= qq~<tr class="expandable" data-target="$sid"><td>$schev$rname</td><td class="num">$st->{visitors}</td><td class="num">$st->{sessions}</td></tr>~; | |
| 320 | if ($has_cities) { | |
| 321 | $acc_html .= qq~<tr class="kids" id="$sid" hidden><td colspan="3"><table class="nested"><tbody>~; | |
| 322 | foreach my $ct (@{ $st->{cities} }) { | |
| 323 | my $cn = _h($ct->{city}); | |
| 324 | $acc_html .= qq~<tr class="leaf"><td>$cn</td><td class="num">$ct->{visitors}</td><td class="num">$ct->{sessions}</td></tr>~; | |
| 325 | } | |
| 326 | $acc_html .= '</tbody></table></td></tr>'; | |
| 327 | } | |
| 328 | } | |
| 329 | $acc_html .= '</tbody></table></td></tr>'; | |
| 330 | } | |
| 331 | } | |
| 332 | } else { | |
| 333 | $acc_html .= q~<tr class="accord-empty"><td colspan="3">No geo data yet — this table fills in as visitors are recorded.</td></tr>~; | |
| 334 | } | |
| 335 | $acc_html .= q~ </tbody> | |
| 336 | </table> | |
| 337 | </div> | |
| 338 | <script> | |
| 339 | (function(){ | |
| 340 | document.addEventListener('click', function(e){ | |
| 341 | var btn = e.target.closest('.accord-collapse-all'); | |
| 342 | if (btn) { | |
| 343 | var scope = btn.closest('.accord-scope') || document; | |
| 344 | scope.querySelectorAll('tr.kids').forEach(function(k){ k.hidden = true; }); | |
| 345 | scope.querySelectorAll('tr.expandable.is-open').forEach(function(t){ t.classList.remove('is-open'); }); | |
| 346 | return; | |
| 347 | } | |
| 348 | var tr = e.target.closest('tr.expandable'); | |
| 349 | if (!tr) return; | |
| 350 | var id = tr.dataset.target; | |
| 351 | if (!id) return; | |
| 352 | var kids = document.getElementById(id); | |
| 353 | if (!kids) return; | |
| 354 | var open = !kids.hidden; | |
| 355 | kids.hidden = open; | |
| 356 | tr.classList.toggle('is-open', !open); | |
| 357 | }); | |
| 358 | })(); | |
| 359 | </script> | |
| 360 | ~; | |
| 361 | $tvars->{accordion_html} = $acc_html; | |
| 362 | $tvars->{has_accordion} = 1; | |
| 363 | } | |
| 364 | elsif ($schema_ready && $tab eq 'sources') { | |
| 365 | my @src = $stats->by_source($db, $dbh, $DB, 12); | |
| 366 | my @refs = $stats->by_referrer_host($db, $dbh, $DB, 25); | |
| 367 | $tvars->{src_rows} = _rows_with_pct(\@src, 'source_kind', 'sessions'); | |
| 368 | $tvars->{has_src_rows} = scalar(@src) ? 1 : 0; | |
| 369 | $tvars->{ref_rows} = _rows_with_pct(\@refs, 'host', 'sessions'); | |
| 370 | $tvars->{has_ref_rows} = scalar(@refs) ? 1 : 0; | |
| 371 | $tvars->{utm_rows} = []; | |
| 372 | $tvars->{has_utm_rows} = 0; | |
| 373 | } | |
| 374 | elsif ($schema_ready && $tab eq 'devices') { | |
| 375 | my @dev = $stats->by_device ($db, $dbh, $DB); | |
| 376 | my @br = $stats->by_browser ($db, $dbh, $DB, 12); | |
| 377 | my @os = $stats->by_os ($db, $dbh, $DB, 12); | |
| 378 | my @res = $stats->by_resolution($db, $dbh, $DB, 12); | |
| 379 | $tvars->{dev_rows} = _rows_with_pct(\@dev, 'device_type', 'sessions'); | |
| 380 | $tvars->{has_dev_rows} = scalar(@dev) ? 1 : 0; | |
| 381 | $tvars->{br_rows} = _rows_with_pct(\@br, 'browser', 'sessions'); | |
| 382 | $tvars->{has_br_rows} = scalar(@br) ? 1 : 0; | |
| 383 | $tvars->{os_rows} = _rows_with_pct(\@os, 'os', 'sessions'); | |
| 384 | $tvars->{has_os_rows} = scalar(@os) ? 1 : 0; | |
| 385 | $tvars->{res_rows} = _rows_with_pct(\@res, 'resolution', 'sessions'); | |
| 386 | $tvars->{has_res_rows} = scalar(@res) ? 1 : 0; | |
| 387 | } | |
| 388 | elsif ($schema_ready && $tab eq 'geo') { | |
| 389 | my @c = $stats->by_country_enriched($db, $dbh, $DB, 30); | |
| 390 | my @rows; | |
| 391 | my $total = 0; $total += ($_->{visitors} || 0) for @c; | |
| 392 | foreach my $r (@c) { | |
| 393 | my $n = $r->{visitors} || 0; | |
| 394 | my $sec = int($r->{avg_duration_seconds} || 0); | |
| 395 | my $dur = $sec >= 60 ? sprintf('%dm %02ds', int($sec/60), $sec % 60) : "${sec}s"; | |
| 396 | push @rows, { | |
| 397 | country => _h($r->{country} || '(unknown)'), | |
| 398 | visitors => $n, | |
| 399 | sessions => $r->{sessions} || 0, | |
| 400 | pct => $total ? sprintf('%.1f', ($n / $total) * 100) : '0.0', | |
| 401 | avg_duration => $dur, | |
| 402 | bounce_rate => sprintf('%.1f%%', 100 * ($r->{bounce_rate} || 0)), | |
| 403 | pv_per_session => sprintf('%.2f', ($r->{pv_per_session} || 0)), | |
| 404 | }; | |
| 405 | } | |
| 406 | $tvars->{country_rows} = \@rows; | |
| 407 | $tvars->{has_country_rows} = scalar(@rows) ? 1 : 0; | |
| 408 | my @tz = $stats->by_timezone($db, $dbh, $DB, 15); | |
| 409 | $tvars->{tz_rows} = _rows_with_pct(\@tz, 'timezone', 'sessions'); | |
| 410 | $tvars->{has_tz_rows} = scalar(@tz) ? 1 : 0; | |
| 411 | ||
| 412 | my $drill = $form->{country} || ''; | |
| 413 | $drill =~ s/[^A-Za-z]//g; | |
| 414 | $drill = uc(substr($drill, 0, 2)); | |
| 415 | $tvars->{has_country_drill} = 0; | |
| 416 | if (length($drill) == 2) { | |
| 417 | my $d = $stats->country_drill($db, $dbh, $DB, $drill); | |
| 418 | if ($d && $d->{name}) { | |
| 419 | $tvars->{has_country_drill} = 1; | |
| 420 | $tvars->{drill_country_name} = _h($d->{name}); | |
| 421 | $tvars->{drill_total_visitors} = $d->{visitors}; | |
| 422 | $tvars->{drill_total_sessions} = $d->{sessions}; | |
| 423 | my @dr; | |
| 424 | foreach my $r (@{ $d->{regions} || [] }) { | |
| 425 | push @dr, { region => _h($r->{region} || '(unknown)'), visitors => $r->{visitors} || 0, sessions => $r->{sessions} || 0 }; | |
| 426 | } | |
| 427 | $tvars->{drill_regions} = \@dr; | |
| 428 | $tvars->{has_drill_regions} = scalar(@dr) ? 1 : 0; | |
| 429 | my @dc; | |
| 430 | foreach my $c2 (@{ $d->{cities} || [] }) { | |
| 431 | push @dc, { city => _h($c2->{city} || '(unknown)'), region => _h($c2->{region} || ''), visitors => $c2->{visitors} || 0, sessions => $c2->{sessions} || 0 }; | |
| 432 | } | |
| 433 | $tvars->{drill_cities} = \@dc; | |
| 434 | $tvars->{has_drill_cities} = scalar(@dc) ? 1 : 0; | |
| 435 | } | |
| 436 | } | |
| 437 | } | |
| 438 | elsif ($schema_ready && $tab eq 'pages') { | |
| 439 | my @pages = $stats->by_page($db, $dbh, $DB, 50); | |
| 440 | my @landing = $stats->by_landing_page($db, $dbh, $DB, 25); | |
| 441 | my @top_rows; | |
| 442 | foreach my $p (@pages) { | |
| 443 | push @top_rows, { path => _h($p->{page_path} || '/'), pageviews => $p->{pageviews} || 0, sessions => $p->{sessions} || 0 }; | |
| 444 | } | |
| 445 | $tvars->{top_pages} = \@top_rows; | |
| 446 | $tvars->{has_top_pages} = scalar(@top_rows) ? 1 : 0; | |
| 447 | my @land_rows; | |
| 448 | foreach my $l (@landing) { | |
| 449 | push @land_rows, { path => _h($l->{entry_page} || '/'), sessions => $l->{sessions} || 0, bounce => sprintf('%.1f', 100 * ($l->{bounce_rate} || 0)) }; | |
| 450 | } | |
| 451 | $tvars->{landing_rows} = \@land_rows; | |
| 452 | $tvars->{has_landing_rows} = scalar(@land_rows) ? 1 : 0; | |
| 453 | } | |
| 454 | ||
| 455 | $db->db_disconnect($dbh); | |
| 456 | ||
| 457 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-store, no-cache, must-revalidate, max-age=0\nPragma: no-cache\nExpires: 0\n\n"; | |
| 458 | ||
| 459 | my $body = join('', $tfile->template('as_admin_traffic.html', $tvars, $userinfo)); | |
| 460 | ||
| 461 | $wrap->render({ | |
| 462 | userinfo => $userinfo, | |
| 463 | page_key => 'admin_traffic', | |
| 464 | title => 'Traffic Reports', | |
| 465 | body => $body, | |
| 466 | extra_js => ['/assets/javascript/globe.js?v=1783314562', | |
| 467 | '/assets/javascript/datepicker.js?v=3'], | |
| 468 | }); |