How to Detect User Traffic from Google in WordPress

Home » Snippets » How to Detect User Traffic from Google in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever wondered how websites are able to detect if a user is coming from a Google search? As a web developer, being able to identify and gather information about your website’s incoming traffic can provide valuable insights into user behavior and help optimize your site accordingly. In this article, we’ll explore the various methods you can employ to detect users coming from Google and how you can leverage this information to enhance your website’s performance and user experience.

					function wpturbo_detect_user_from_google() {
    $referrer = wp_get_referer();
    $google_url = 'https://www.google.';
    
    if (strpos($referrer, $google_url) !== false) {
        // User came from Google
        // Your code here
    } else {
        // User did not come from Google
        // Your code here
    }
}
add_action( 'wp', 'wpturbo_detect_user_from_google' );
				

The provided code is a function called wpturbo_detect_user_from_google(), which is used to detect if a user has come to your WordPress website from a Google search.

First, the function uses the wp_get_referer() function to get the referring URL from the user’s browser. This refers to the URL of the page that the user was on before they came to your website.

The wp_get_referer() function returns the full URL, including the protocol (e.g., "http://" or "https://"). In our case, we want to check if the user came from any Google search, regardless of the specific domain name, so we only need to check if the URL contains "https://www.google.".

The function then defines a variable called $google_url and sets it to 'https://www.google.'. This variable represents the base URL of Google search results.

Next, the function uses the strpos() function to check if the $referrer contains the $google_url. The strpos() function searches for the occurrence of a substring within a string and returns the position of the first occurrence, or false if the substring is not found.

If the strpos() function returns a value other than false, it means that the user came from a Google search. In this case, you can add your code within the if statement. This code will be executed if the user came from Google.

On the other hand, if the strpos() function returns false, it means that the user did not come from a Google search. In this case, you can add your code within the else statement. This code will be executed if the user did not come from Google.

Finally, the function is hooked into the wp action using the add_action() function. This ensures that the wpturbo_detect_user_from_google() function is executed on every page load.

By using this code snippet, you can effectively detect if a user has come to your WordPress website from a Google search and then execute appropriate code based on that information.

Register an account to save your snippets or go Pro to get more features.