How To Track Geo IP Stats When Cloudflare Is Enabled

Cloudflare masks the IP address of the user and moves it to another HTTP header.

To make it compatible with ThirstyAffiliate’s stats collection module you will need to add the following snippet of code to your functions.php file or a drop-in plugin.

function ta_cloudflare_ip_compatibility( $ip ) {
    if ( ! empty( $_SERVER['HTTP_CF_CONNECTING_IP'] ) )
            $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
    return $ip;
}
add_filter( 'ta_get_user_ip_address' , 'ta_cloudflare_ip_compatibility' , 10 , 1 );

This will be added as a feature to ThirstyAffiliates in the future.

 

Add the following snippet of code to your functions.php file or a drop-in plugin for the Geolocations module when redirecting:

function get_proper_ip_from_cf_for_ta_geo($address) {
	if(isset($_SERVER['HTTP_CF_CONNECTING_IP']) && !empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
		$address = $_SERVER['HTTP_CF_CONNECTING_IP'];
	}
	return $address;
}
add_filter('tap_geolocation_client_ip', 'get_proper_ip_from_cf_for_ta_geo');

 

in ThirstyAffiliates (Free Version)ThirstyAffiliates Pro