Adding Google Conversion Code to WordPress Contact Form 7

I found a tutorial for adding Google Conversion code in WordPress Contact Form 7, which can be found here .

I have currently added

<script type="text/javascript"> $(".wpcf7-form").submit(function(){ var google_conversion_id = "12345678910"; var google_conversion_label = "xxxxxxxxxxx"; var image = new Image(1,1); image.src = "http://www.googleadservices.com/pagead/conversion/"+google_conversion_id+"/?label="+google_conversion_label+"&script=0"; }); </script> 

to my footer, but it does not send the correct results. Could someone help me with what I need to add to my plugin contact form via the WordPress dashboard so that my Google Analytics results will show the correct results. I would not want to redirect to another page.

0
source share
3 answers

You are on the right track. Since this script works, you get your results written to the server.

  • The easiest and most common way to achieve this is to place the script on a separate page (usually thank-you.php) and redirect the user to this page, so after completing his activity, we can run this script and record this action. OR
  • Another way (albeit difficult) is to make this script run on with ajax / javascript after user activity.

I have successfully implemented the second on one of my client sites Hiteshi Technologies

If you want to install it without any redirection, you may find this useful.

Track Google conversions without redirecting

Hooray!!

+2
source

The analytic code should look like this:

 ga('send', 'event', 'category', 'action', 'label', value); // value is a number. 

(where the last 2 parameters are optional)

So, we just need to create the Contact Form 7 advanced configuration code as follows:

 on_sent_ok: "ga('send', 'event', 'Landing Page', 'Submit');" //here 'Landing Page' or 'Submit' are just for sample; 

If you want to know more: Event Tracking in WordPress Contact Form 7 (Universal Analytics) and Google Event Tracking - Web Tracking (analytics.js)

0
source

I did this in three simple steps:

  • In the CF7 plugin (advanced settings):

on_sent_ok: "run_conversion_code ();"

  1. In header.php (or in js file):

      function run_conversion_code() { $ = jQuery; var a = "/wp-admin/admin-ajax.php"; $.post(a, {action: 'run_conversion_code'}).done(function(data){ $('body').append(data); }); console.log('conversion code running'); } </script> 
  2. functions.php

    function run_conversion_code ()

     { ?> <script type="text/javascript"> /* <![CDATA[ */ var google_conversion_id = 962XXX263; var google_conversion_language = "en"; var google_conversion_format = "3"; var google_conversion_color = "ffffff"; var google_conversion_label = "TsaNCM6dq1wQ99HzygM"; var google_remarketing_only = false; /* ]]> */ </script> <script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"> </script> <noscript> <div style="display:inline;"> <img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/962390263/?label=TsaNCM6dq1wQ99HzygM&amp;guid=ON&amp;script=0"/> </div> </noscript> <?php die(); } 

    add_action ('wp_ajax_run_conversion_code', 'run_conversion_code');

    add_action ('wp_ajax_nopriv_run_conversion_code', 'run_conversion_code');

0
source

Source: https://habr.com/ru/post/973505/


All Articles