![](/img/trans.png)
[英]Google analytics event tracking for form on wordpress site
[英]Google Analytics event tracking code breaks wordpress site when added to functions.php
我通过functions.php添加了一个全局按钮,该按钮使用以下代码显示在我的产品页面上:
function add_content_after_addtocart_button_func() {
echo '
<div class="wsbl_line"><a href="http://line.me/R/msg/text/website.com"
title="share using Line" rel="nofollow"
class="wp_social_bookmarking_light_a"><img src="sample-image.png"
width="135" height="30" class="wp_social_bookmarking_light_img"></a></div>';
}
在将以下Google Analytics(分析)事件跟踪代码添加到上述代码后,该网站就会中断。
function add_content_after_addtocart_button_func() {
echo '
<div class="wsbl_line"><a href="http://line.me/R/msg/text/website.com"
title="share using Line" rel="nofollow"
class="wp_social_bookmarking_light_a" onClick="ga('send', 'event', 'social', 'line-button-click', 'line-button');"><img src="sample-image.png"
width="135" height="30" class="wp_social_bookmarking_light_img"></a></div>';
}
通过将跟踪代码直接放在页面中,我已经成功测试了跟踪代码
<a href="website.com" onClick="ga('send', 'event', 'social', 'line-button-
click', 'line-button');">anchor text</a>
我究竟做错了什么?
如果使用单引号打开代码并将其用于要回显的代码中,则需要对其进行转义,如下所示:
function add_content_after_addtocart_button_func() {
echo '
<div class="wsbl_line"><a href="http://line.me/R/msg/text/website.com"
title="share using Line" rel="nofollow"
class="wp_social_bookmarking_light_a" onClick="ga(\'send\', \'event\',
\'social\', \'line-button-click\', \'line-button\');"><img src="sample-
image.png"
width="135" height="30" class="wp_social_bookmarking_light_img"></a></div>';
}
即使您使用“双引号”打开回声,它也会中断,因为您正在回声的代码中有两个双倍数。我建议您使用带有php语法突出显示功能的文本编辑器来查看中断的地方。
尝试一下我给你的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.