本文介绍了无法读取“Google"来自 JavaScript 中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,用于检测来自几个搜索引擎的引用 URL,然后将此值(不是 mc_u20 变量)传递到服务器以在某处使用.除了一个大问题之外,该脚本就像一种享受,它根本不会跟踪 Google 搜索结果.因此,来自 Google 的任何结果都不会注册.这是脚本:

I've written a script that detects the referring URL from a couple of search engines and then passes this value (not the mc_u20 variable) to a server to be used somewhere. The script works like a treat except for one big problem, it simply won't track Google search results. So any result that comes from Google, simply doesn't register. Here is the script:

var mc_searchProviders = {"search_google":"google.co","search_bing":"bing.com","search_msn":"search.msn","search_yahoo":"search.yahoo","search_mywebsearch":"mywebsearch.com","search_aol":"search.aol.co", "search_baidu":"baidu.co","search_yandex":"yandex.com"};
var mc_socialNetworks = {"social_facebook":"facebook.co","social_twitter":"twitter.co","social_google":"plus.google."};
var mc_pageURL = window.location +'';
var mc_refURL = document.referrer +'';

function mc_excludeList() {
if (mc_refURL.search('some URL') != -1) {
    return false; //exclude some URL
}
if (mc_refURL.search('mail.google.') != -1) {
    return false; //exclude Gmail
}
if (mc_refURL.search(mc_paidSearch) != -1) {
    return false; //exclude paidsearch
}
else {
    mc_checkURL();
}
}

mc_excludeList();

function mc_checkURL() {
    var mc_urlLists = [mc_searchProviders, mc_socialNetworks],
i,mc_u20;
for (i = 0; i < mc_urlLists.length; i++) {
    for (mc_u20 in mc_urlLists[i]) {
    if(!mc_urlLists[i].hasOwnProperty(mc_u20))
    continue;
    if (mc_refURL.search(mc_urlLists[i][mc_u20]) != -1) {
        mc_trackerReport(mc_u20);
        return false;
    }
    else if ((mc_refURL == '') && (mc_directTracking === true)){
        mc_u20 = "direct_traffic";
        mc_trackerReport(mc_u20);
        return false;
        }
        }
    }
}

最烦人的是,我已经在我的本地机器上测试了这个(通过使用实际的谷歌搜索 URL 填充 mc_refURL 并且它就像一个魅力.我也认为也许当搜索第一个 mc_searchProviders 对象它不知何故跳过了第一个实例,所以我添加了一个空白的.但这仍然不起作用.更烦人的是,对于其他所有搜索引擎,mc_u20 变量似乎填充了我需要的东西.

The most annoying thing is, I have tested this on my local machine (by populating the mc_refURL with an actual google search URL and it works like a charm. I've also thought that maybe when searching through the first mc_searchProviders object it is somehow skipping the first instance, so I added a blank one. But still this doesn't work. What's even more annoying is that for every other search engine, the mc_u20 variable seems to populate with what I need.

这让我发疯.任何人都可以看到这里有什么问题吗?我可能还会提到我已登录 Google,但我不知道这会如何影响脚本,因为他们的博文(11 月)说他们正在过滤关键字并不会阻止引用 URL 被传递.

This is driving me insane. Can anyone see what's wrong here? I might also mention that I'm signed into Google but I don't see how this would affect the script as their blogpost (in November) said they were filtering keywords not stopping the referring URL from being passed.

推荐答案

是的,所以我知道发生了什么.脚本的第一部分排除了您自己的网址(请参阅 'some URL' 的位置.假设这被设置为 www.example.com.在 Google 中,如果我搜索例如 example 和 Google 返回 www.example.com 作为第一个搜索结果,在引用 URL 中它将包含 www.example.com. 因此,为什么脚本会崩溃,也许将来有人会发现这很有用.

Right so I figured out what was going on. The first part of the script excludes your own URL (see where it says 'some URL'. Say this was set to www.example.com. In Google if I searched for say example and Google returned www.example.com as the first search result, in the referring URL it would contain www.example.com. Hence why the script was breaking, maybe someone will find this useful in future.

这篇关于无法读取“Google"来自 JavaScript 中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 04:40