Advertisement
vapvarun

Send UTM parameter to webhook

Aug 15th, 2023 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. // Function to check UTM parameters and trigger webhook
  2. function vap_utm_webhook_check() {
  3.  
  4. $allowed_query_params = array('utm_source','utm_campaign','utm_gruppo','utm_annuncio','gclid', 'fbclid', 'ttclid');
  5.  
  6. $matching_params = array();
  7.  
  8. foreach ($allowed_query_params as $param) {
  9. if (isset($_GET[$param])) {
  10. $matching_params[$param] = $_GET[$param];
  11. }
  12. }
  13.  
  14. if (!empty($matching_params)) {
  15. // Replace 'your_webhook_url' with the actual URL of your webhook endpoint
  16. $webhook_url = 'https://hook.eu1.make.com/bxtfysudqbkusmzoaedrpt871e7yput3';
  17. // Retrieve browser information
  18. $user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
  19.  
  20. $matching_params['user_agent'] = $user_agent;
  21.  
  22. // Retrieve client IP address
  23. $client_ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
  24. $matching_params['client_ip'] = $client_ip;
  25.  
  26. // Add the current source URL to the payload
  27. $matching_params['source_url'] = esc_url(wp_get_referer() ?: home_url());
  28.  
  29. $matching_params['current_url'] = esc_url(home_url( $_SERVER['REQUEST_URI'] ));
  30.  
  31. // Prepare webhook payload
  32. $payload = $matching_params;
  33.  
  34. // Send the POST request to the webhook
  35. wp_safe_remote_post($webhook_url, array(
  36. 'body' => json_encode($payload),
  37. 'headers' => array('Content-Type' => 'application/json'),
  38. ));
  39. }
  40. }
  41.  
  42. // Hook to run the webhook check
  43. add_action('init', 'vap_utm_webhook_check');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement