Skip to content

Commit 51c2a8a

Browse files
committed
Improve social link platform detection
Replace brittle href.includes checks with a URL-based detector. Adds hostMatches and detectPlatformFromHref helpers to parse link hostnames (lowercased), handle subdomains, and detect GitHub, Instagram and Discord (discord.gg/discord.com). Uses try/catch and returns 'Unknown' fallback, then uses the detected platform when sending the GA event.
1 parent ba48756 commit 51c2a8a

1 file changed

Lines changed: 29 additions & 11 deletions

File tree

  • attached_assets/low-latency-gaming-guide-main

attached_assets/low-latency-gaming-guide-main/index.html

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,21 +1188,39 @@ <h4>Fonti</h4>
11881188

11891189
// Track external links and social media clicks
11901190
document.addEventListener('DOMContentLoaded', function() {
1191+
function hostMatches(hostname, expectedHost) {
1192+
return hostname === expectedHost || hostname.endsWith('.' + expectedHost);
1193+
}
1194+
1195+
function detectPlatformFromHref(href) {
1196+
try {
1197+
var url = new URL(href, window.location.origin);
1198+
var hostname = url.hostname.toLowerCase();
1199+
1200+
if (hostMatches(hostname, 'github.com')) {
1201+
return 'GitHub';
1202+
}
1203+
1204+
if (hostMatches(hostname, 'instagram.com')) {
1205+
return 'Instagram';
1206+
}
1207+
1208+
if (hostMatches(hostname, 'discord.gg') || hostMatches(hostname, 'discord.com')) {
1209+
return 'Discord';
1210+
}
1211+
} catch (error) {
1212+
return 'Unknown';
1213+
}
1214+
1215+
return 'Unknown';
1216+
}
1217+
11911218
// Track social links (Instagram e Discord)
11921219
document.querySelectorAll('.social-links a').forEach(function(link) {
11931220
link.addEventListener('click', function(e) {
1194-
var platform = 'Unknown';
1221+
var platform = detectPlatformFromHref(this.href);
11951222
var action = 'click';
1196-
1197-
// Identifica la piattaforma dal link
1198-
if (this.href.includes('github.com')) {
1199-
platform = 'GitHub';
1200-
} else if (this.href.includes('instagram.com')) {
1201-
platform = 'Instagram';
1202-
} else if (this.href.includes('discord.gg') || this.href.includes('discord.com')) {
1203-
platform = 'Discord';
1204-
}
1205-
1223+
12061224
// Invia evento a Google Analytics
12071225
if (typeof gtag !== 'undefined') {
12081226
gtag('event', 'social_click', {

0 commit comments

Comments
 (0)