Cannot read “Google” from object in JavaScript

大憨熊 提交于 2019-12-12 02:29:12

问题


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;
        }
        }
    }
}

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.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/9094401/cannot-read-google-from-object-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!