下面有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>
答案 0 :(得分:4)
是的,你可以这样做,即使谷歌无法验证它(因为代码的改动)。
然而,您的代码中存在大量重复,因为您必须为每个按钮复制一次所有这些巨大的块(在您的情况下总共为12次)。
我建议使用包装函数,将google_conversion_id
和google_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>