简体   繁体   English

将Google转换代码添加到WordPress联系表格7

[英]Adding Google Conversion code to WordPress Contact Form 7

I found a tutorial for adding Google Conversion code to WordPress Contact Form 7 plugin which can be found here . 我找到了将Google转换代码添加到WordPress Contact Form 7插件的教程,可以在此处找到。

Currently I've 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 doesn't send correct results. 到我的页脚,但无法发送正确的结果。 Could someone help me out with what I should add to my Contact Form Plugin, through the control panel of WordPress, to make my Analytics show correct results. 有人可以通过WordPress的控制面板帮助我解决应添加到我的联系表单插件中的内容,以使我的Google Analytics(分析)显示正确的结果。 I would not prefer redirecting to another page. 我不希望重定向到另一个页面。

You are on right track. 您步入正轨。 As this script runs you get your results recorded at server. 在运行此脚本时,您会将结果记录在服务器上。

  1. The easiest and most common way to achieve this is to place the script on a separate page(generally a thank-you.php) and redirect user to that page so after completion of his activity we can run this script and record this activity. 实现此目的的最简单,最常见的方法是将脚本放置在单独的页面上(通常为thank-you.php),然后将用户重定向到该页面,因此在其活动完成后,我们可以运行此脚本并记录该活动。 OR 要么
  2. The other way ( a tricky one though ) is to make this script run on the same page using ajax/javascript after user's activity. 另一种方法虽然很棘手 )是在用户活动之后使用ajax / javascript在同一页面上运行此脚本。

If you want to set it without any redirection you may find this helpful. 如果您希望在不进行任何重定向的情况下进行设置,则可能会有所帮助。

Google Conversion Tracking Without Redirection 无需重定向的Google转化跟踪

The analytic code should be like this format: 分析代码应类似于以下格式:

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

(where the last 2 parameters are optional) (最后两个参数是可选的)

So, we just need to make the Contact Form 7's Additional settings code like this: 因此,我们只需要编写Contact Form 7的“其他设置”代码,如下所示:

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

If you want to learn more: Event tracking in WordPress Contact Form 7 (Universal Analytics) and Google Event Tracking - Web Tracking (analytics.js) 如果您想了解更多信息,请执行以下操作: WordPress联系表格7(Universal Analytics)中的 事件跟踪Google Event Tracking-Web Tracking(analytics.js)

I did this in three simple steps: 我通过三个简单步骤完成了此操作:

  1. In the CF7 Plugin (additional settings): 在CF7插件(附加设置)中:

on_sent_ok: "run_conversion_code();" on_sent_ok:“ run_conversion_code();”

  1. In header.php (or in a js file): 在header.php中(或在js文件中):

      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 的functions.php

    function run_conversion_code() 函数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_run_conversion_code','run_conversion_code');

    add_action( 'wp_ajax_nopriv_run_conversion_code', 'run_conversion_code'); add_action('wp_ajax_nopriv_run_conversion_code','run_conversion_code');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM