javascript 如何在提交按钮上使用 ajax 实现 Google Adwords“转换像素”(没有“谢谢”页面)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27149647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement a Google Adwords "conversion pixel" using ajax on a submit button (no "thank you" page)
提问by Rick Sanchez
I want to implement an adwords "conversion pixel" script - this is the script:
我想实现一个 AdWords“转换像素”脚本 - 这是脚本:
<!-- Google Code for General_Lead Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 000;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "hahaha";
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/975247275153/?label=IiuoCNO17lcQgZ2P0QM&guid=ON&script=0"/>
</div>
</noscript>
to a contact form on my website which doesn't lead to a "thank you" page. This is my submit form code. (The validation isn't 100% done :P )
到我网站上的联系表格,该表格不会指向“谢谢”页面。这是我的提交表单代码。(验证不是 100% 完成:P)
// Send button for the "contact form".
$('#sendBtn').click(function(){
//get info
var fullname = $("#fullname").val();
var email = $("#email").val();
var text = $("#text").val();
//send info to php
$.ajax({
beforeSend: function() {
if ( IsEmail(email) == false) {
$('#aboutUnsuccess').show("slow");
$('#contactform').hide("slow");
}
},
url: 'http://www.example.com/contact.php',
type: "POST",
data: ({ "fullname": fullname, "email": email, "text": text }),
success: function (results){
if ( IsEmail(email) == true) {
//hide table
$('#contactform').hide('slow', function() {
$('#contactform').hide( "slow" );
});
//show textboxes
$('#aboutSuccess').show("slow");
}
}
});
});
I tried using .append that I found out it doesn't work after googleing.
我尝试使用 .append,我发现它在谷歌搜索后不起作用。
and tried to follow this post: How to track a Google Adwords conversion onclick?- which didn't work too
并尝试关注这篇文章:How to track an Google Adwords conversion onclick? - 这也不起作用
回答by andlrc
Google supports async conversions:
Google 支持异步转换:
https://developers.google.com/adwords-remarketing-tag/asynchronous/
https://developers.google.com/adwords-remarketing-tag/asynchronous/
Load this script in your html file:
在您的 html 文件中加载此脚本:
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion_async.js" charset="utf-8"></script>
And then call the function google_trackConversion
when your user clicks the button:
然后google_trackConversion
在用户单击按钮时调用该函数:
window.google_trackConversion({
google_conversion_id: 1234,
google_remarketing_only: true,
// ...
});
回答by Irshad Khan
100% working Google Conversion Tracking concept using Ajax on a submit button :
100% 在提交按钮上使用 Ajax 的 Google 转化跟踪概念:
$.ajax({
type: "POST",
url: "enquiry-submit.php",
data: data,
success: function (result) {
$("#msg").fadeIn(400).html(result);
/* Conversion Tracking Start */
var google_conversion_id = YOUR_CONVERSION_ID_HERE;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "YOUR_CONVERSION_LABEL_HERE";
var google_remarketing_only = false;
$.getScript('//www.googleadservices.com/pagead/conversion.js');
var image = new Image(1, 1);
image.src = "//www.googleadservices.com/pagead/conversion/YOUR_CONVERSION_ID_HERE/?label=YOUR_CONVERSION_LABEL_HERE&guid=ON&script=0";
/* Conversion Tracking End */
}
});
It is working on my Google Ads Campaign.
它适用于我的 Google Ads 广告系列。
Note: You must try or test this by clicking on your ad. The effect of conversion will be visible after 15 minutes on your Google AdWords Console
注意:您必须通过点击您的广告来尝试或测试。15 分钟后,您将在 Google AdWords 控制台上看到转化效果
回答by ViES
$.ajax({
dataType: "script",
cache: true,
url: 'https://www.googleadservices.com/pagead/conversion_async.js'
}).done(function () {
isInitialized = true;
});
google_trackConversion({
google_conversion_id: 12345,
google_conversion_language: "en",
google_conversion_format: "3",
google_conversion_color: "ffffff",
google_conversion_label: "label",
google_conversion_value: 0,
google_remarketing_only: false
});
回答by sonofkrish
function for creating iframe and put adwords code in thanks.php file
用于创建 iframe 并将 Adwords 代码放入Thanks.php 文件的函数
function conversionTracker() {
var iframe = document.createElement('iframe');
iframe.style.width = '0px';
iframe.style.height = '0px';
document.body.appendChild(iframe);
iframe.src = 'thanks.php'; //Make sure path is correct! (update with bloginfo var for nebula)
};
call conversionTracker() in ajax success function
....
success: function(response){
conversionTracker();
}
then create a thanks.php file and put adwords code in this file
然后创建一个Thanks.php文件并将adwords代码放在这个文件中
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Thank You</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- Google Code for Frachiseglobal Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 1234;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "dummy";
var google_conversion_value = 1.00;
var google_conversion_currency = "INR";
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/1234/?value=1&currency_code=INR&label=dsfdsf&guid=ON&script=0"/>
</div>
</noscript>
</body>
</html>
Note : here label and conversion_id are fake values, Please put your own.
注意:这里的label和conversion_id是假值,请自行填写。