我已设置 Google Analytics 来跟踪 outbound links .但是,我还安装了一个名为 Amazon Link Localizer 的 WordPress 插件。修改传出 URL,使其最终形式为 prourls.com?url=www.amazon.com 而不是 www.amazon.com。
是否可以修改 Analytics 脚本,以便仍然可以跟踪这些出站链接点击?
最佳答案
第 1 步:添加 trackOutboundLink脚本到您的网站。
在 <head></head> 中添加此代码页面 HTML 的一部分:(完整的脚本位于此页面的底部,但使用此脚本有助于最小化页面大小)
<script>
var trackOutboundLink=function(t,e){var n,o="object"==typeof t?t.href:t,a=document.createElement("a");function c(){document.location=o}e=e||o,a.href=e,/\bprourls\.com?$/i.test(a.host)?e=(n=/[\?&]url=([^&#]+)/.test(a.search)&&RegExp.$1||"")?decodeURIComponent(n):e:/\blinksynergy\.com$/i.test(a.host)&&(e=(n=/[\?&]murl=([^&#]+)/.test(a.search)&&RegExp.$1||"")?decodeURIComponent(n):e),"function"==typeof gtag?gtag("event","click",{event_category:"outbound",event_label:e,transport_type:"beacon",event_callback:c}):ga("send","event","outbound","click",e,{transport:"beacon",hitCallback:c})};window.addEventListener("load",function(){var t,e,n=document.getElementsByTagName("a")||[];function o(t){t.preventDefault();var e=this.getAttribute("data-outbound-link");trackOutboundLink(this.href,"1"===e?"":e)}for(t=0;t<n.length;t++)(e=n[t]).href&&!/^#/.test(e.href)&&location.host!==e.host&&""!==e.getAttribute("data-outbound-link")&&e.addEventListener("click",o,!1)},!1);
</script>
请注意,该脚本是 Google Analytics 提供的脚本的修改/自定义版本:
https://support.google.com/analytics/answer/1136920?hl=en — 对于旧的 analytics.js (或 ga.js)库。
https://support.google.com/analytics/answer/7478520?hl=en — 对于新的 gtag.js 图书馆。
第 2 步:添加 data-outbound-link="1"到出站链接的 HTML。
对于您要跟踪的每个出站链接,将其添加到链接的 HTML 中:
data-outbound-link="1"
(如果您之前在链接的 HTML 中添加了 onclick="trackOutboundLink('http://www.example.com'); return false;",请将其替换为上述代码。)
所以不是:<a href="http://www.example.com" onclick="trackOutboundLink('http://www.example.com'); return false;">Check out example.com</a>
..使用这个:<a href="http://www.example.com" data-outbound-link="1">Check out example.com</a>
完整脚本
以下是自定义 的完整未缩小版本 trackOutboundLink脚本:(仅供引用)
<script>
var trackOutboundLink = function(link, url2track) {
var url2visit = ('object' === typeof link) ? link.href : link,
a = document.createElement('a'),
b;
url2track = url2track || url2visit;
a.href = url2track;
// Dynamic redirect URLs via Prourls.com; this was for Amazon URLs.
if (/\bprourls\.com?$/i.test(a.host)) {
b = (/[\?&]url=([^&#]+)/.test(a.search) && RegExp.$1) || '';
url2track = b ? decodeURIComponent(b) : url2track;
// Dynamic redirect URLs via Link Synergy; this was for Footlocker URLs.
} else if (/\blinksynergy\.com$/i.test(a.host)) {
b = (/[\?&]murl=([^&#]+)/.test(a.search) && RegExp.$1) || '';
url2track = b ? decodeURIComponent(b) : url2track;
}
// All other URLs are not parsed and expected to be valid outbound URLs.
function onTracked() {
//alert('Tracked: ' + url2track);
document.location = url2visit;
}
if ('function' === typeof gtag) {
gtag('event', 'click', {
'event_category': 'outbound',
'event_label': url2track,
'transport_type': 'beacon',
'event_callback': onTracked
});
} else {
ga('send', 'event', 'outbound', 'click', url2track, {
'transport': 'beacon',
'hitCallback': onTracked
});
}
};
window.addEventListener('load', function() {
var links = document.getElementsByTagName('a') || [],
i, a;
function _go(e) {
e.preventDefault();
// Track a custom URL address, if specified.
var b = this.getAttribute('data-outbound-link');
trackOutboundLink(this.href, '1' === b ? '' : b);
}
for (i = 0; i < links.length; i++) {
a = links[i];
if (!a.href || /^#/.test(a.href) || location.host === a.host) {
continue;
}
if ('' !== a.getAttribute('data-outbound-link')) {
a.addEventListener('click', _go, false);
}
}
}, false);
</script>
关于javascript - Google Analytics - 带重定向的出站链接跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49911882/
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我想使用spawn(针对多个并发子进程)在Ruby中执行一个外部进程,并将标准输出或标准错误收集到一个字符串中,其方式类似于使用Python的子进程Popen.communicate()可以完成的操作。我尝试将:out/:err重定向到一个新的StringIO对象,但这会生成一个ArgumentError,并且临时重新定义$stdxxx会混淆子进程的输出。 最佳答案 如果你不喜欢popen,这是我的方法:r,w=IO.pipepid=Process.spawn(command,:out=>w,:err=>[:child,:out])
我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我有一个未排序的链接列表,我将其保存在旁边,我想单击每个链接并确保它转到真实页面而不是404、500等。问题是我不知道该怎么做。是否有一些我可以检查的对象会给我http状态代码或任何东西?mylinks=Browser.ul(:id,'my_ul_id').linksmylinks.eachdo|link|link.click#needtocheckfora200statusorsomethinghere!how?Browser.backend 最佳答案 我的回答与铁皮人的想法类似。require'net/http'require'
我一直在玩一个脚本,它在Chrome中获取选定的文本并在Google中查找它,提供四个最佳选择,然后粘贴相关链接。它以不同的格式粘贴,具体取决于当前在Chrome中打开的页面-DokuWiki打开的DokuWiki格式,普通网站的HTML,我想要我的WordPress所见即所得编辑器的富文本。我尝试使用pbpaste-Preferrtf来查看没有其他样式的富文本链接在粘贴板上的样子,但它仍然输出纯文本。在文本编辑中保存文件并进行试验后,我想出了以下内容text=%q|{\rtf1{\field{\*\fldinst{HYPERLINK"URL"}}{\fldrsltTEXT}}}|te
基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub
我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan
假设您有一个可执行文件foo.rb,其库bar.rb的布局如下:/bin/foo.rb/lib/bar.rb在foo.rb的header中放置以下要求以在bar.rb中引入功能:requireFile.dirname(__FILE__)+"../lib/bar.rb"只要对foo.rb的所有调用都是直接的,这就可以正常工作。如果你把$HOME/project和符号链接(symboliclink)foo.rb放入$HOME/usr/bin,然后__FILE__解析为$HOME/usr/bin/foo.rb,因此无法找到bar.rb关于foo.rb的目录名.我意识到像rubygems这
在我的mac上安装几个东西时遇到这个问题,我认为这个问题来自将我的豹子升级到雪豹。我认为这个问题也与macports有关。/usr/local/lib/libz.1.dylib,filewasbuiltfori386whichisnotthearchitecturebeinglinked(x86_64)有什么想法吗?更新更具体地说,这发生在安装nokogirigem时日志看起来像:xslt_stylesheet.c:127:warning:passingargument1of‘Nokogiri_wrap_xml_document’withdifferentwidthduetoproto