简体   繁体   中英

Cannot read “Google” from object in JavaScript

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM