gpt4 book ai didi

javascript - 将 Google Analytics 点击(事件)跟踪代码添加到 Javascript window.open

转载 作者:行者123 更新时间:2023-12-02 18:44:36 25 4
gpt4 key购买 nike

我脑子里有以下 Javascript:

// Google Analytics code required for EventTracking <script type="text/javascript"> function trackOutboundLink(link, category, action) { try { _gaq.push(['_trackEvent', category , action]); } catch(err){} setTimeout(function() {document.location.href = link.href; }, 100); } 

我体内有以下内容:

// required to activate social bookmarks
$(".btn_fb").click(function() {
window.open("");
return false;
});
$(".btn_tw").click(function() {
window.open("http://twitter.com/home?status=Webpage Title"+document.URL+"", "Twitter", "width=660,height=400,scrollbars=no;resizable=no");
return false;
});
$(".btn_li").click(function() {
window.open("http://www.linkedin.com/shareArticle?mini=true&amp;url="+document.URL+"&amp;title=Webpage Title;summary=Webpage summary", "LinkedIn", "width=660,height=400,scrollbars=no;resizable=no");
return false;
}); $('.btn_go').click(function() {
window.open("http://plus.google.com/share?url="+document.URL, 'GooglePlus', 'width=660,height=500,scrollbars=no,resizable=no');
return false;
});
$(".btn_ma").attr("href", "mailto:?subject=Webpage Subject&body=Webpage summary" + window.location); </script>

//GA Code
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-NNNNNN-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

我的 HTML 中包含以下内容:

      <div class="btn_fb"></div>
<div class="btn_tw"></div>
<div class="btn_li"></div>
<a class="btn_ma"></a>
<div class="btn_go"></div>

链接功能正常,但由于它们不是标签(.btn_ma 除外),我无法按照文档添加 Google Analytics 事件跟踪:https://support.google.com/analytics/answer/1136920?hl=en-GB

关于这里的最佳方法有什么建议吗?

最佳答案

您可以将事件跟踪添加到 jQuery 单击事件。

// required to activate social bookmarks
$(".btn_fb").click(function() {
window.open("");
try {
_gaq.push(['_trackEvent', "{category}", "{action}"]);
} catch(err){}
return false;
});

$(".btn_tw").click(function() {
window.open("http://twitter.com/home?status=Webpage Title"+document.URL+"", "Twitter", "width=660,height=400,scrollbars=no;resizable=no");
try {
_gaq.push(['_trackEvent', "{category}", "{action}"]);
} catch(err){}
return false;
});

$(".btn_li").click(function() {
window.open("http://www.linkedin.com/shareArticle?mini=true&amp;url="+document.URL+"&amp;title=Webpage Title;summary=Webpage summary", "LinkedIn", "width=660,height=400,scrollbars=no;resizable=no");
try {
_gaq.push(['_trackEvent', "{category}", "{action}"]);
} catch(err){}
return false;
});

$('.btn_go').click(function() {
window.open("http://plus.google.com/share?url="+document.URL, 'GooglePlus', 'width=660,height=500,scrollbars=no,resizable=no');
try {
_gaq.push(['_trackEvent', "{category}", "{action}"]);
} catch(err){}
return false;
});

$(".btn_ma").click(function() {
window.location = "mailto:?subject=Webpage Subject&body=Webpage summary" + window.location
try {
_gaq.push(['_trackEvent', "{category}", "{action}"]);
} catch(err){}
return false;
});

您会注意到我已将点击事件跟踪添加到每个点击事件中,因此当用户单击 div 时,会执行操作,然后跟踪分析。我还编辑了您的邮件点击事件,以便也可以对其进行跟踪。另一个想法是使用像 Share This 这样的服务或AddThis内置分析功能,也可以链接到 Google Analytics

关于javascript - 将 Google Analytics 点击(事件)跟踪代码添加到 Javascript window.open,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16533546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com