Last active
June 24, 2025 12:23
-
-
Save sabrysuleiman/b3f6435dd70db7092287d93d1b1ab54f to your computer and use it in GitHub Desktop.
php function to Get User Traffic Source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function getUserTrafficSource() { | |
| $source = 'unknown'; // Default to unknown | |
| $details = []; // To store additional details | |
| // 1. Check for UTM parameters (most reliable for ads) | |
| if (isset($_GET['utm_medium'])) { | |
| if ($_GET['utm_medium'] === 'cpc' || $_GET['utm_medium'] === 'paid') { | |
| $source = 'ad'; | |
| $details['ad_platform_guess'] = isset($_GET['utm_source']) ? $_GET['utm_source'] : 'unknown_platform'; | |
| $details['utm_medium'] = $_GET['utm_medium']; | |
| if (isset($_GET['utm_campaign'])) $details['utm_campaign'] = $_GET['utm_campaign']; | |
| if (isset($_GET['utm_source'])) $details['utm_source'] = $_GET['utm_source']; | |
| if (isset($_GET['utm_term'])) $details['utm_term'] = $_GET['utm_term']; | |
| if (isset($_GET['utm_content'])) $details['utm_content'] = $_GET['utm_content']; | |
| return ['source' => $source, 'details' => $details]; | |
| } | |
| } | |
| // 2. Check for GCLID (Google Ads specific) | |
| if (isset($_GET['gclid'])) { | |
| $source = 'ad'; | |
| $details['ad_platform'] = 'google_ads'; | |
| $details['gclid'] = $_GET['gclid']; | |
| return ['source' => $source, 'details' => $details]; | |
| } | |
| // 3. Check for FBCLID (Facebook Ads specific) | |
| if (isset($_GET['fbclid'])) { | |
| $source = 'ad'; | |
| $details['ad_platform'] = 'facebook_ads'; | |
| $details['fbclid'] = $_GET['fbclid']; | |
| return ['source' => $source, 'details' => $details]; | |
| } | |
| // 4. Analyze HTTP Referer for Organic Search Engines | |
| if (isset($_SERVER['HTTP_REFERER'])) { | |
| $referer = $_SERVER['HTTP_REFERER']; | |
| $details['referer'] = $referer; | |
| // Common search engine domains | |
| $searchEngines = [ | |
| 'google.com', 'google.co.uk', 'google.ca', 'google.de', 'google.fr', 'google.es', // Add more Google TLDs as needed | |
| 'bing.com', | |
| 'yahoo.com', | |
| 'duckduckgo.com', | |
| 'baidu.com', | |
| 'yandex.com', | |
| ]; | |
| foreach ($searchEngines as $engine) { | |
| if (strpos($referer, $engine) !== false) { | |
| $source = 'organic'; | |
| $details['organic_engine'] = explode('.', parse_url($referer, PHP_URL_HOST))[0]; // Extracts 'google', 'bing', etc. | |
| return ['source' => $source, 'details' => $details]; | |
| } | |
| } | |
| // If referer exists but isn't a known search engine, could be a direct link, social media, etc. | |
| // You might want to add more rules here for social media referers if needed. | |
| if (strpos($referer, 'facebook.com') !== false || strpos($referer, 't.co') !== false || strpos($referer, 'linkedin.com') !== false) { | |
| $source = 'social_media'; | |
| $details['social_platform'] = parse_url($referer, PHP_URL_HOST); | |
| return ['source' => $source, 'details' => $details]; | |
| } | |
| // If referer is not an ad, organic or social, it could be a referral from another website | |
| $source = 'referral'; | |
| } | |
| // 5. If no specific indicators, it's likely a direct visit | |
| // This is the fallback if no other conditions are met. | |
| if ($source === 'unknown' && !isset($_SERVER['HTTP_REFERER'])) { | |
| $source = 'direct'; | |
| } | |
| return ['source' => $source, 'details' => $details]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment