vapvarun icon

Send UTM parameter to webhook

vapvarun | PRO | 08/18/23 08:20:06 AM UTC (Edited) | 0 ⭐ | 449 👁️ | Never ⏰ | []
text |

1.64 KB

|

None

|

0 👍

/

0 👎

// Function to check UTM parameters and trigger webhook
function vap_utm_webhook_check() {
     $allowed_query_params = array('utm_source','utm_campaign','utm_gruppo','utm_annuncio','gclid', 'fbclid', 'ttclid');
     $matching_params = array();
     foreach ($allowed_query_params as $param) {
        if (isset($_GET[$param])) {
            $matching_params[$param] = $_GET[$param];
        }
    }
     if (!empty($matching_params)) {
        // Replace 'your_webhook_url' with the actual URL of your webhook endpoint
        $webhook_url = 'https://hook.eu1.make.com/bxtfysudqbkusmzoaedrpt871e7yput3';
		// Retrieve browser information
		$user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
    		$matching_params['user_agent'] = $user_agent;
    		// Retrieve client IP address
   		$client_ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
		$matching_params['client_ip'] = $client_ip;
         // Add the current source URL to the payload
        $matching_params['source_url'] = esc_url(wp_get_referer() ?: home_url());
 		$matching_params['current_url'] = esc_url(home_url( $_SERVER['REQUEST_URI'] ));
         // Prepare webhook payload
        $payload = $matching_params;
         // Send the POST request to the webhook
        wp_safe_remote_post($webhook_url, array(
            'body' => json_encode($payload),
            'headers' => array('Content-Type' => 'application/json'),
        ));
    }
}
 // Hook to run the webhook check
add_action('init', 'vap_utm_webhook_check');

Comments