{"id":4300,"date":"2020-05-25T08:44:47","date_gmt":"2020-05-25T05:44:47","guid":{"rendered":"https:\/\/trackingchef.com\/?p=4300"},"modified":"2023-07-02T11:59:23","modified_gmt":"2023-07-02T08:59:23","slug":"bulletproof-contact-form-7-tracking-with-gtm","status":"publish","type":"post","link":"https:\/\/trackingchef.com\/google-tag-manager\/bulletproof-contact-form-7-tracking-with-gtm\/","title":{"rendered":"Bulletproof Contact Form 7 tracking with GTM"},"content":{"rendered":"\n

Contact Form 7<\/a> (aka CF7) is the most popular contact form plugin for WordPress, with over 5 million active installations. <\/p>\n\n\n\n

The most amazing part about this plugin’s popularity is that it is completely stripped of any fanciness. It lacks any support of CSS (which can of course be added separately or via other plugins) and even support for actions beyond sending the form’s data to an email address. It just works.<\/p>\n\n\n\n

It is mostly popular among developers, that take this stripped-down functionality and easily mold it into the forms they want on their site.<\/p>\n\n\n\n

In this post, I’ll discuss several methods to capture form submissions on CF7 using GTM.<\/p>\n\n\n\n

The best course of action is to redirect the users to a Thank You page after a successful form submission. This interaction is simple to capture across all ad and analytics platforms.<\/p>\n\n\n\n

If you\u2019re using a CF7 form without a Thank You page (as most users do), you can use Google Tag Manager (GTM) to track form submissions in several ways.<\/p>\n\n\n\n

Form Submission Trigger<\/h2>\n\n\n\n

This is the standard trigger for tracking form submissions in GTM. You can set it up with the Form Submission trigger. While this is simple to set up, it isn’t always a solid solution as it might miss submissions (if the tag didn’t run in time) or send false positives on failed submissions.<\/p>\n\n\n\n

\"\"
Form Submission Trigger in GTM<\/figcaption><\/figure>\n\n\n\n

Element Visibility Trigger<\/h2>\n\n\n\n

This trigger is more stable than the Form Submission. The basic idea behind this trigger is identifying when a user has viewed a certain element of the page. In this case, we ask GTM to identify users that have seen the Thank Message after a successful submission. <\/p>\n\n\n\n

\n

Pro tip:<\/strong>
This trigger works regardless of the text of the Thank You Message<\/p>\n<\/blockquote>\n\n\n\n

To set up this trigger, simply navigate to the Triggers page in GTM a click ‘New’. Select Trigger Type ‘Element Visibility’ and set the Selection Method to ‘CSS Selector’. Paste in the field ‘.wpcf7 form.sent .wpcf7-response-output<\/em>‘. (shoutout to Uri for correcting this syntax).<\/p>\n\n\n\n

Finally, check the ‘Observe DOM changes’ box, or the trigger won’t be able to identify the new page element.<\/p>\n\n\n\n

\"\"
Element Visibility Trigger in Google Tag Manager<\/figcaption><\/figure>\n\n\n\n
\n

Pro tip:<\/strong>
You can use a more restrictive CSS Selector to limit the trigger to specific forms, for example by adding the form’s ID to the CSS Selector<\/p>\n<\/blockquote>\n\n\n\n

DOM Event<\/h2>\n\n\n\n

This method uses a different approach. It adds a DOM event listener that observes form submissions.<\/p>\n\n\n\n

In layman’s terms, every time a CF7 form is successfully submitted, an “invisible” flag is raised in the page’s HTML code. This event listener will identify that this flag has been raised, and in turn, will trigger your code.<\/p>\n\n\n\n

\n

Pro tip:<\/strong>
This method can be used to track form submissions even without GTM, by triggering the relevant conversion pixel (e.g. Facebook Lead) <\/p>\n<\/blockquote>\n\n\n\n

To use this event listener, create a new Custom HTML tag in GTM and paste in the code below. This tag should be triggered on all pages.<\/p>\n\n\n\n

\n

Pro tip:<\/strong>
If you’re already using the GTM4WP plugin for adding GTM to your WordPress site, you can use its built in feature for CF7 tracking<\/p>\n<\/blockquote>\n\n\n\n

Simple setup<\/h3>\n\n\n\n
<script>\ndocument.addEventListener( 'wpcf7submit', function( event ) {\n        dataLayer.push({\n          'event' : 'form_submit',\n          'form_id' : event.detail.contactFormId\n        });\n    \t}, false );\n<\/script><\/code><\/pre>\n\n\n\n

Advanced setup<\/h3>\n\n\n\n

This script is slightly more complex. It takes the Form ID from CF7 and replaces it with a friendly name. For example, Form ID ‘1234’ will be replaced with ‘contact us’. This will make the reporting in Google Analytics more user-friendly.<\/p>\n\n\n\n

<script>\n  var cf7id = {\n  1234: 'contact us',\n  5678: 'demo'\n};\ndocument.addEventListener( 'wpcf7submit', function( event ) {\n        var CF7formID = cf7id[event.detail.contactFormId];\n        if (!CF7formID) return;  \n        dataLayer.push({\n          'event' : 'form_submit',\n          'form_name' : CF7formID,\n          'form_id' : event.detail.contactFormId,\n        });\n    \t}, false );\n<\/script><\/code><\/pre>\n\n\n\n
\n

Pro tip:<\/strong>
Since this tracking is an inherent part of the site, in many cases I would prefer adding it directly to the site’s header\/footer instead of working via GTM<\/p>\n<\/blockquote>\n\n\n\n

Triggering other platforms<\/h3>\n\n\n\n

If you want to use this trigger for additional platforms beyond Google Analytics, you can simply create a Trigger that captures only form submissions.<\/p>\n\n\n\n

Navigate to the Triggers section and create a new trigger. Select the Trigger Type ‘Custom Event’ and set the Event Name to ‘form_submit’.<\/p>\n\n\n\n

This trigger can now be used to trigger conversion pixels, e.g Facebook or LinkedIn Leads.<\/p>\n\n\n

\n
\"\"
Facebook Lead event with form_submit<\/em> trigger<\/figcaption><\/figure><\/div>\n\n\n

If you want to track the specific form submitted, you can use the form_id <\/em>DataLayer variable.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Similarly, the form_id<\/em> variable can be used to limit the trigger to only fire on specific forms.<\/p>\n\n\n

\n
\"\"
Setting the form_submit<\/em> event to trigger conditionally on with for specific form_id <\/em>value<\/figcaption><\/figure><\/div>","protected":false},"excerpt":{"rendered":"

In this post I’ll discuss several methods to capture form submissions on CF7 using GTM.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[49],"_links":{"self":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4300"}],"collection":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/comments?post=4300"}],"version-history":[{"count":9,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4300\/revisions"}],"predecessor-version":[{"id":5673,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4300\/revisions\/5673"}],"wp:attachment":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/media?parent=4300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/categories?post=4300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/tags?post=4300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}