Tracking the source of users sending information through Contact Form 7 or in short, Tracking Contact Form 7 wordpress
To create a contact form, an order form for a WordPress website, in the previous article, I showed you how to use the Contact Form 7 plugin to create a form. But for those who do marketing and advertising, after creating a form, they will need to know how to track the source of users submitting the form to better analyze user behavior to be able to optimize the advertising campaign. running.
Instructions for setting up user source tracking via Contact Form 7:
Step 1: Insert the following code at the end of the functions.php file in the theme you are using.
| function wpshore_wpcf7_before_send_mail($array) { | |
| global $wpdb; | |
| if(wpautop($array[‘body’]) == $array[‘body’]) // The email is of HTML type | |
| $lineBreak = “<br/>”; | |
| else | |
| $lineBreak = “\n”; | |
| $trackingInfo .= $lineBreak . $lineBreak . ‘– Tracking Info –’ . $lineBreak; | |
| $trackingInfo .= ‘Form Fill URL: ‘ . $_SERVER[‘HTTP_REFERER’] . $lineBreak; | |
| if (isset ($_SESSION[‘OriginalRef’]) ) | |
| $trackingInfo .= ‘User came from page: ‘ . $_SESSION[‘OriginalRef’] . $lineBreak; | |
| if (isset ($_SESSION[‘LandingPage’]) ) | |
| $trackingInfo .= ‘Landing page before form filling: ‘ . $_SESSION[‘LandingPage’] . $lineBreak; | |
| if ( isset ($_SERVER[“REMOTE_ADDR”]) ) | |
| $trackingInfo .= ‘User IP: ‘ . $_SERVER[“REMOTE_ADDR”] . $lineBreak; | |
| if ( isset ($_SERVER[“HTTP_X_FORWARDED_FOR”])) | |
| $trackingInfo .= ‘User\’s Proxy Server IP: ‘ . $_SERVER[“HTTP_X_FORWARDED_FOR”] . $lineBreak . $lineBreak; | |
| if ( isset ($_SERVER[“HTTP_USER_AGENT”]) ) | |
| $trackingInfo .= ‘Browser info: ‘ . $_SERVER[“HTTP_USER_AGENT”] . $lineBreak; | |
| $array[‘body’] = str_replace(‘[tracking-info]’, $trackingInfo, $array[‘body’]); | |
| return $array; | |
| } | |
| add_filter(‘wpcf7_mail_components’, ‘wpshore_wpcf7_before_send_mail’); | |
| // Original Referrer | |
| function wpshore_set_session_values() | |
| { | |
| if (!session_id()) | |
| { | |
| session_start(); | |
| } | |
| if (!isset($_SESSION[‘OriginalRef’])) | |
| { | |
| $_SESSION[‘OriginalRef’] = $_SERVER[‘HTTP_REFERER’]; | |
| } | |
| if (!isset($_SESSION[‘LandingPage’])) | |
| { | |
| $_SESSION[‘LandingPage’] = “http://” . $_SERVER[“SERVER_NAME”] . $_SERVER[“REQUEST_URI”]; | |
| } | |
| } | |
| add_action(‘init’, ‘wpshore_set_session_values’); |
Step 2: Log in to the wordpress management page and open the form to be tracked . Switch to the Mail Configuration tab and add in the Body section the shortcode [tracking-info] as follows:

Epilogue
That’s all there is to it. Hope this article will help in Tracking Contact Form 7 wordpress so that you can track customer behavior, thereby optimizing the effectiveness of your advertising campaign.
If you find it interesting, you can follow the wordpress tips section to know more new knowledge.
Follow fanpage to receive the latest posts: Group
Wish you have useful knowledge about worpdress.
