我下面有12个相同的代码段,它们都需要放在同一页面上。我已经对它们进行了修改,因此它们分别是goog_report_conversion_a,goog_report_conversion_b等。但这会导致标记无法验证-这是将多个代码段添加到一页的正确方法吗?

<script type="text/javascript">
/* <![CDATA[ */
goog_snippet_vars_a = function() {
var w = window;
w.google_conversion_id = XXXXXXXXX;
w.google_conversion_label = "XXX1";
w.google_remarketing_only = false;
}
// DO NOT CHANGE THE CODE BELOW.
goog_report_conversion_a = function(url) {
goog_snippet_vars_a();
window.google_conversion_format = "3";
window.google_is_call = true;
var opt = new Object();
opt.onload_callback = function() {
if (typeof(url) != 'undefined') {
  window.location = url;
}
}
var conv_handler = window['google_trackConversion'];
if (typeof(conv_handler) == 'function') {
conv_handler(opt);
}
}
/* ]]> */
<script type="text/javascript"
src="//www.googleadservices.com/pagead/conversion_async.js">
</script>  

//Example click events below

    <a onClick="goog_report_conversion_a()" href="/">Click Event</a>
<a onClick="goog_report_conversion_b()" href="/">Click Event</a>

最佳答案

是的,即使Google无法验证(因为代码中的更改),您也可以通过这种方式进行操作。

但是,您的代码中有很多重复项,因为您必须为每个按钮复制所有这些大块一次(在您的情况下,总共复制12次)。

我建议使用将google_conversion_idgoogle_conversion_label作为参数的包装器函数。

<script type="text/javascript">
/* <![CDATA[ */
goog_conv_custom = function(conversion_id, conversion_label, url) {
var w = window;
w.google_conversion_id = conversion_id;
w.google_conversion_label = conversion_label;
w.google_remarketing_only = false;

goog_report_conversion(url);
}
// DO NOT CHANGE THE CODE BELOW.
goog_report_conversion = function(url) {

window.google_conversion_format = "3";
window.google_is_call = true;
var opt = new Object();
opt.onload_callback = function() {
if (typeof(url) != 'undefined') {
  window.location = url;
}
}
var conv_handler = window['google_trackConversion'];
if (typeof(conv_handler) == 'function') {
conv_handler(opt);
}
}
/* ]]> */
</script>
<script type="text/javascript"
  src="//www.googleadservices.com/pagead/conversion_async.js">
</script>  

//Example click events below

<a onClick="goog_conv_custom('conversion_id_a', 'conversion_label_a')" href="/">Click Event</a>
<a onClick="goog_conv_custom('conversion_id_b', 'conversion_label_b')" href="/">Click Event</a>

08-04 03:32