如何通过以下搜索代码(设置为Google搜索“关键字网站:MYDOMAIN.com”)合并Google Analytics页内搜索跟踪?
我遇到的问题是按Enter键会导致用户立即跳转到谷歌搜索查询。我已经看到使用hitCallback的一些零碎,但我不确定如何在此设置的页面上实现此功能。
<form action="http://www.google.com/search" name="searchbox";
method="get" id="searchform" class="form-search">;
<input type="hidden" name="hl" value="en" />;
<input type="hidden" name="ie" value="ISO-8859-1" />;
<input type="hidden" name="sitesearch" id="s" value="MYDOMAIN.com" />;
<input maxlength="256" size="40" name="q" class="search" value="" />;
</form>
我知道我无法使用新的Universal Analytics提供的传统页内搜索,但我不明白为什么在离开之前不应该提交给GA的方式谷歌搜索页面的域名。在思考事情时我相信一个人可以重定向到另一个页面,提交GA,然后重定向到谷歌,但我当然希望最小化我的请求并提出一个简单的解决方案。
答案 0 :(得分:0)
您可以从表单中删除action
,并在对其进行超时后使用jQuery设置它以让GATC触发。像这样:
$('#searchform').on('submit', function(e){
e.preventDefault();
window.setTimeout(function(){
if (typeof _gaq != 'undefined') {
_gaq.push(['_trackEvent', 'Search', 'value', 'paramX', 'valueY', false]);
}
$('#searchform').attr('action', 'http://www.google.com/search');
},1000);
});
答案 1 :(得分:0)
<script>
$("#searchform").submit(function (event) {
event.preventDefault(); // cancel default behavior
var searchTerms = $('input[name=q]').val();
searchTerms = searchTerms.replace(" ", "+");
console.debug(searchTerms);
ga('send', {
'hitType': 'pageview',
'page': "http://MYDOMAIN.com/search?hl=en&ie=ISO-8859-1&sitesearch=MYDOMAIN.com&q=" + searchTerms,
'title': 'Google Search'
});
setTimeout(function () {
window.location.href = "http://google.com/search?hl=en&ie=ISO-8859-1&sitesearch=MYDOMAIN.com&q=" + searchTerms;
}, 500);
});
</script>
我发现,尽管访问了外部域,我发现实现伪站内搜索(通过谷歌搜索提供)的最佳方法是在重定向到正确的搜索页面之前导致虚假页面查看。我插入了半秒延迟以确保在重定向之前发送了页面视图。