Last active
January 4, 2016 20:09
-
-
Save dmtintner/8671986 to your computer and use it in GitHub Desktop.
SimilarWeb API - CRM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SW = { | |
baseUrl : 'http://api.similarweb.com/site/', | |
nonCompanyEmails : ['gmail.com', 'yahoo.com', 'hotmail.com', 'aol.com', 'googlemail.com'], | |
notEnoughDataText : '--', | |
subdomainText : 'Subdomain', | |
unknownSiteText : 'n/a', | |
fetchOptions : { | |
"muteHttpExceptions" : true, // needed so we can get status code of response | |
}, | |
parseSite : function(email){ | |
var site = email.split('@')[1]; | |
// check if it is a non-regular url | |
var urlSections = site.split('.'); | |
if ( urlSections.length > 2 && urlSections[1] !== 'com' && urlSections[1] !== 'co' ){ | |
return this.subdomainText; | |
} | |
// check if it's a non company email (like gmail) | |
if (this.nonCompanyEmails.indexOf(site) > -1){ | |
return this.unknownSiteText; | |
} else { | |
return site; | |
} | |
}, | |
getCategory : function(site, userKey){ | |
var apiurl = this.baseUrl + site + "/v2/CategoryRank?Format=JSON&UserKey=" + userKey; | |
var response = UrlFetchApp.fetch(apiurl, this.fetchOptions); | |
if (response.getResponseCode() !== 200){ | |
return false; | |
} else { | |
var data = JSON.parse(response); | |
return data.Category.replace(/_/g, " "); // convert underscores to spaces | |
} | |
}, | |
getGlobalRank : function(site, userKey) { | |
var apiurl = this.baseUrl + site + "/v1/traffic?Format=JSON&UserKey=" + userKey; | |
Utilities.sleep(1000); // to prevent 'service invoked too many times' error | |
var data = JSON.parse( UrlFetchApp.fetch(apiurl) ); | |
if (data.GlobalRank === 0) return 'Redirects to another site'; | |
return data.GlobalRank; | |
}, | |
getEstimatedTraffic : function(site, userKey){ | |
var apiurl = SW.baseUrl + site + "/v1/EstimatedTraffic?Format=JSON&UserKey=" + userKey; | |
Utilities.sleep(2000); // to prevent 'service invoked too many times' error | |
var data = JSON.parse( UrlFetchApp.fetch(apiurl) ); | |
if (data.EstimatedVisitors === 0) return 'Redirects to another site'; | |
return data.EstimatedVisitors; | |
}, | |
getCache : function(){ | |
return CacheService.getPrivateCache(); | |
} | |
}; | |
function init(email, userKey) { | |
if (!email) return; | |
if (!userKey) return 'Enter a valid API key in the settings sheet'; | |
var site = SW.parseSite(email); | |
// if no site, stop and don't make api calls | |
if (!site) return; | |
// if we have email, but company is unknown or it is a subdomain | |
if (site == SW.unknownSiteText) return [SW.unknownSiteText, SW.notEnoughDataText, SW.notEnoughDataText, SW.notEnoughDataText]; | |
if (site == SW.subdomainText) return [SW.subdomainText, SW.notEnoughDataText, SW.notEnoughDataText, SW.notEnoughDataText]; | |
// if we have this stored in the cache, don't do another api request | |
var cache = SW.getCache(), | |
cacheKey = site, | |
cacheVal = JSON.parse( cache.get(cacheKey) ); // returns string, need to convert to array | |
if (cacheVal != null) return cacheVal; | |
// if there is a site and we know it try making first api call | |
var category = SW.getCategory(site, userKey); | |
// if no data found, don't make other api calls | |
if (!category) return [site, SW.notEnoughDataText, SW.notEnoughDataText, SW.notEnoughDataText]; | |
// if category returned data, go ahead and make other api calls | |
var globalRank = SW.getGlobalRank(site, userKey), | |
estimatedTraffic = SW.getEstimatedTraffic(site, userKey); | |
var data = [site, category, globalRank, estimatedTraffic]; | |
// store our cached result (as JSON) and return data array | |
cache.put(cacheKey, JSON.stringify(data), 500); // cache expires after 500 seconds | |
return data; | |
} | |
// For auto sorting | |
function onEdit(event){ | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var columnToSortBy = 4; | |
var range = sheet.getRange("MyContacts"); // named range = A4:E54 | |
range.sort( [ {column : columnToSortBy, ascending: true} ] ); | |
} | |
function checkApiKey(key) { | |
if (!key) { | |
return 'Enter an API key'; | |
} else { | |
return 'Your key'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment