├── readme.md ├── script.js └── sidebar /readme.md: -------------------------------------------------------------------------------- 1 | ## Read the Blog post first : 2 | Semrush API Google sheets 3 | 4 | ## Quick start - no installation needed : 5 | Make a copy of the master spreadsheet with the script and you're ready to go! 6 | 7 | ## Functions 8 | 9 |
10 |
DOMAIN_OVERVIEW_SEMRUSH(domain, db, date, excludeHeaders, cache)
11 |

Returns Semrush domain history for a specified domain from January 2012 onwards

12 |
13 |
DOMAIN_ORGANIC_KEYWORDS_SEMRUSH(domain, filterBy, matchType, query, limit, db, date, excludeHeaders, cache)
14 |

Returns Semrush Organic keywords for a specified domain

15 |
16 |
HISTORICAL_RANKING_KEYWORD_SEMRUSH(domain, query, limit, db, date, excludeHeaders, cache)
17 |

Returns Historical rankings for domain/keyword combination

18 |
19 |
URL_ORGANIC_KEYWORDS_SEMRUSH(url, limit, db, excludeHeaders, cache)
20 |

Returns Ranking Semrush Organic keywords per URL

21 |
22 |
KEYWORD_DIFFICULTY_SEMRUSH(query, db, excludeHeaders, cache)
23 |

Returns Semrush Keyword Difficulty for a keyword

24 |
25 |
SERPS_SEMRUSH(query, limit, db, excludeHeaders, cache)
26 |

Returns Semrush organic search results for a specific keyword

27 |
28 |
RELATED_QUERIES_SEMRUSH(query, limit, db, excludeHeaders, cache)
29 |

Returns Related search queries for a keyword from Semrush

30 |
31 |
KEYWORD_VOLUME_SEMRUSH(query, db, excludeHeaders, cache)
32 |

Returns Keyword Volume from semrush

33 |
34 |
PHRASE_QUESTIONS_SEMRUSH(query, limit, db, filterBySearchVolume, searchVolumeFilter, searchVolume, excludeHeaders, cache)
35 |

Returns Questions asked for a keyword from Semrush

36 |
37 |
38 | 39 | 40 | 41 | ## DOMAIN\_OVERVIEW\_SEMRUSH(domain, db, date, excludeHeaders, cache) ⇒ 42 | Returns Semrush domain history for a specified domain from January 2012 onwards 43 | 44 | **Kind**: global function 45 | **Returns**: Returns organic keywords count, organic traffic, organic cost, adwords data 46 | **Customfunction**: 47 | 48 | | Param | Type | Description | 49 | | --- | --- | --- | 50 | | domain | "example.com" | REQUIRED The root domain, example: "nytimes.com", DO NOT include protocol (http/https) | 51 | | db | "us" | OPTIONAL The country database you want to search from. Default is US | 52 | | date | 201601 | OPTIONAL Leave this blank for current data. YYYYMM format for historical reports, note: always reports on the 15th of the month. | 53 | | excludeHeaders | true | OPTIONAL true to EXCLUDE column headers or false to include. Default is false. | 54 | | cache | "TRUE" | OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) | 55 | 56 | 57 | 58 | ## DOMAIN\_ORGANIC\_KEYWORDS\_SEMRUSH(domain, filterBy, matchType, query, limit, db, date, excludeHeaders, cache) ⇒ 59 | Returns Semrush Organic keywords for a specified domain 60 | 61 | **Kind**: global function 62 | **Returns**: Access organic keywords for a domain from semrush.com database. 63 | **Customfunction**: 64 | 65 | | Param | Type | Description | 66 | | --- | --- | --- | 67 | | domain | "example.com" | REQUIRED The root domain, example: "nytimes.com", DO NOT include protocol (http/https) | 68 | | filterBy | true | OPTIONAL Use true to include the query in the filter or false to remove the query in the filter. Default is true | 69 | | matchType | true | OPTIONAL Use true for partial match, use false for exact match. Default is true, partial match | 70 | | query | "apartments" | OPTIONAL The keyword you want to filter by. Relies on previous 2 parameters. Example: "brown shoes". | 71 | | limit | 10 | OPTIONAL Number from 1 to 10000 | 72 | | db | "us" | OPTIONAL The country database you want to search from. Default is US | 73 | | date | 201601 | OPTIONAL Leave this blank for current data. YYYYMM format for historical reports, note: always reports on the 15th of the month. | 74 | | excludeHeaders | true | OPTIONAL true to EXCLUDE column headers or false to include. Default is false. | 75 | | cache | "TRUE" | OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) | 76 | 77 | 78 | 79 | ## HISTORICAL\_RANKING\_KEYWORD\_SEMRUSH(domain, query, limit, db, date, excludeHeaders, cache) ⇒ 80 | Returns Historical rankings for domain/keyword combination 81 | 82 | **Kind**: global function 83 | **Returns**: Access organic keywords for a domain from semrush.com database 84 | **Customfunction**: 85 | 86 | | Param | Type | Description | 87 | | --- | --- | --- | 88 | | domain | "example.com" | REQUIRED The root domain, example: "nytimes.com", DO NOT include protocol (http/https) | 89 | | query | "apartments" | OPTIONAL The keyword you want to filter by. Relies on previous 2 parameters. Example: "brown shoes". | 90 | | limit | 10 | OPTIONAL Number from 1 to 10000 | 91 | | db | "us" | OPTIONAL The country database you want to search from. Default is US | 92 | | date | 201601 | OPTIONAL Leave this blank for current data. YYYYMM format for historical reports, note: always reports on the 15th of the month. | 93 | | excludeHeaders | true | OPTIONAL true to EXCLUDE column headers or false to include. Default is false. | 94 | | cache | "TRUE" | OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) | 95 | 96 | 97 | 98 | ## URL\_ORGANIC\_KEYWORDS\_SEMRUSH(url, limit, db, excludeHeaders, cache) ⇒ 99 | Returns Ranking Semrush Organic keywords per URL 100 | 101 | **Kind**: global function 102 | **Returns**: Access organic keywords for a url from semrush.com database 103 | **Customfunction**: 104 | 105 | | Param | Type | Description | 106 | | --- | --- | --- | 107 | | url | "http://example.com" | REQUIRED The exact URL you want data for, MUST include protocol (http/https) | 108 | | limit | "10" | OPTIONAL Number from 1 to 10,000, for number of results | 109 | | db | "US" | OPTIONAL The database, example "US" for American database. Default is US | 110 | | excludeHeaders | true | OPTIONAL true to EXCLUDE column headers or false to include. Default is false. | 111 | | cache | "TRUE" | OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) | 112 | 113 | 114 | 115 | ## KEYWORD\_DIFFICULTY\_SEMRUSH(query, db, excludeHeaders, cache) ⇒ 116 | Returns Semrush Keyword Difficulty for a keyword 117 | 118 | **Kind**: global function 119 | **Returns**: Access keyword difficulty for keyword from semrush 120 | **Customfunction**: 121 | 122 | | Param | Type | Description | 123 | | --- | --- | --- | 124 | | query | "apartments" | REQUIRED The keyword you want information for. Example: "brown shoes". | 125 | | db | "us" | OPTIONAL The country database you want to search from. Default is US | 126 | | excludeHeaders | true | OPTIONAL true to EXCLUDE column headers or false to include. Default is false. | 127 | | cache | "TRUE" | OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) | 128 | 129 | 130 | 131 | ## SERPS\_SEMRUSH(query, limit, db, excludeHeaders, cache) ⇒ 132 | Returns Semrush organic search results for a specific keyword 133 | 134 | **Kind**: global function 135 | **Returns**: Access organic search results for a keyword from semrush.com database 136 | **Customfunction**: 137 | 138 | | Param | Type | Description | 139 | | --- | --- | --- | 140 | | query | "apartments" | REQUIRED The keyword you want information for. Example: "brown shoes". | 141 | | limit | 10 | OPTIONAL Number from 10 to 20, for number of results. Default is 10 | 142 | | db | "us" | OPTIONAL The country database you want to search from. Default is US | 143 | | excludeHeaders | true | OPTIONAL true to EXCLUDE column headers or false to include. Default is false. | 144 | | cache | "TRUE" | OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) | 145 | 146 | 147 | 148 | ## RELATED\_QUERIES\_SEMRUSH(query, limit, db, excludeHeaders, cache) ⇒ 149 | Returns Related search queries for a keyword from Semrush 150 | 151 | **Kind**: global function 152 | **Returns**: Returns related queries for a specific keyword from semrush.com 153 | **Customfunction**: 154 | 155 | | Param | Type | Description | 156 | | --- | --- | --- | 157 | | query | "apartments" | REQUIRED The keyword you want information for. Example: "brown shoes". | 158 | | limit | 10 | OPTIONAL The number of results. Default is 1 | 159 | | db | "us" | OPTIONAL The country database you want to search from. Default is US | 160 | | excludeHeaders | true | OPTIONAL true to EXCLUDE column headers or false to include. Default is false. | 161 | | cache | "TRUE" | OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) | 162 | 163 | 164 | 165 | ## KEYWORD\_VOLUME\_SEMRUSH(query, db, excludeHeaders, cache) ⇒ 166 | Returns Keyword Volume from semrush 167 | 168 | **Kind**: global function 169 | **Returns**: Returns search volume, cpc, etc.. 170 | **Customfunction**: 171 | 172 | | Param | Type | Description | 173 | | --- | --- | --- | 174 | | query | "apartments" | REQUIRED The keyword you want information for. Example: "brown shoes". | 175 | | db | "us" | OPTIONAL The country database you want to search from, default is "us" | 176 | | excludeHeaders | true | OPTIONAL true to EXCLUDE column headers or false to include. Default is false. | 177 | | cache | "TRUE" | OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) | 178 | 179 | 180 | 181 | ## PHRASE\_QUESTIONS\_SEMRUSH(query, limit, db, filterBySearchVolume, searchVolumeFilter, searchVolume, excludeHeaders, cache) ⇒ 182 | Returns Questions asked for a keyword from Semrush 183 | 184 | **Kind**: global function 185 | **Returns**: Returns Questions asked for a specific keyword from semrush.com 186 | **Customfunction**: 187 | 188 | | Param | Type | Description | 189 | | --- | --- | --- | 190 | | query | "apartments" | REQUIRED The keyword you want information for. Example: "brown shoes". | 191 | | limit | 10 | OPTIONAL The number of results. Default is 1 | 192 | | db | "us" | OPTIONAL The country database you want to search from. Default is US | 193 | | filterBySearchVolume | true | OPTIONAL If you want to filter by search volume, use TRUE, default is FALSE | 194 | | searchVolumeFilter | ">" | OPTIONAL Filters the search volume parameter (next). Use ">" for Greater than, or "<" for Less than. Default is ">" greater than | 195 | | searchVolume | 1000 | OPTIONAL A search volume number (per month) you want to filter results by. Both previous parameters need to be set for this to work. | 196 | | excludeHeaders | true | OPTIONAL true to EXCLUDE column headers or false to include. Default is false. | 197 | | cache | "TRUE" | OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) | 198 | -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | /* --------------------------------------------------------------------------- 2 | * Semrush API functions (Google sheets) from Analytics API: https://www.semrush.com/api-analytics/ 3 | * 4 | * @desc Access Organic API calls from the Semrush.com API using Google sheets formulas 5 | * @author Dave Sottimano @dsottimano Twitter 6 | * @license MIT (http://www.opensource.org/licenses/mit-license.php) 7 | * @version 1.0 8 | * -------------------------------------------------------------------------*/ 9 | 10 | //---------------------------------------------------------------------------- 11 | 12 | function onOpen() { 13 | SpreadsheetApp.getUi() 14 | .createMenu('Helper Functions') 15 | .addItem('Check Semrush API balance', 'checkSemrushAccount') 16 | .addItem('Add your API key', 'addApiKey') 17 | .addItem('Flush the cache', 'flushAllCacheAndProperties') 18 | .addItem('Show formula reference', 'formulaReferenceSidebar') 19 | .addToUi(); 20 | 21 | if (!SemrushGlobal.data.API_KEY) SpreadsheetApp.getActiveSpreadsheet().toast('In the Helper Functions menu (menu bar), click on Add your API key to get started!', 'The Library is ready to rock :', 5); 22 | 23 | } 24 | 25 | function formulaReferenceSidebar () { 26 | var htmlOutput = HtmlService.createHtmlOutputFromFile('sidebar') 27 | .setTitle('Semrush API Library (Unofficial)'); 28 | 29 | SpreadsheetApp.getUi().showSidebar(htmlOutput); 30 | } 31 | 32 | 33 | /* ---------------------------------------------------------------------------* 34 | SemrushGlobal Object START 35 | * -------------------------------------------------------------------------*/ 36 | 37 | var SemrushGlobal = { 38 | 39 | data : { 40 | API_KEY : PropertiesService.getUserProperties().getProperty("semrushkey"), 41 | DEFAULT_DB : "us", 42 | API_KEY_PROPERTY_NAME: "semrushkey" 43 | }, 44 | queries : { 45 | domainOrganic : "http://api.semrush.com/?type=domain_organic&key=", 46 | urlOrganic : "http://api.semrush.com/?type=url_organic&key=", 47 | keywordDifficulty: "http://api.semrush.com/?type=phrase_kdi&key=", 48 | phraseOrganic : "http://api.semrush.com/?type=phrase_organic&key=", 49 | keywordVolume : "http://api.semrush.com/?type=phrase_this&key=", 50 | relatedQueries : "http://api.semrush.com/?type=phrase_related&key=", 51 | phraseQuestions: "https://api.semrush.com/?type=phrase_questions&key=", 52 | domainOverview : "http://api.semrush.com/?type=domain_rank&key=", 53 | countUnits : "http://www.semrush.com/users/countapiunits.html?key=" 54 | }, 55 | methods : { 56 | giveApiRest : function giveApiRest () { 57 | Utilities.sleep(200) 58 | }, 59 | checkAccount : function checkAccount () { 60 | 61 | try { 62 | if (!SemrushGlobal.data.API_KEY) return [false,"No valid API key found. Please run the Add Semrush Key function from the Spreadsheet menu under 'Helper functions'"] 63 | var result = UrlFetchApp.fetch(SemrushGlobal.queries.countUnits + SemrushGlobal.data.API_KEY,{"muteHttpExceptions":true}) 64 | 65 | if (typeof result.getContentText() === "string") { 66 | if(parseInt(result.getContentText()) > -1) { 67 | return [true] 68 | } 69 | var parsedResult = JSON.parse(result.getContentText()) 70 | if (parsedResult == 0) throw "The key:" + PropertiesService.getDocumentProperties().getProperty("semrushkey") + " has no credits left" 71 | if (parsedResult.data.error) throw parsedResult.data.error 72 | return [true] 73 | } 74 | 75 | } catch (e) { 76 | return [false,e] 77 | } 78 | }, 79 | parseApiResponse : function parseApiResponse(result,valueBoolean) { 80 | var data = [], valueBoolean, filtered 81 | valueBoolean ? valueBoolean = 1 : valueBoolean = 0; 82 | var newLines = result.split("\n"); 83 | 84 | try { 85 | for(i=valueBoolean;i -1) { 229 | return Browser.msgBox("You have : " + result + " API credits left") 230 | } 231 | var parsedResult = JSON.parse(result) 232 | if (parsedResult.data.error) throw parsedResult.data.error 233 | 234 | } catch (e) { 235 | return Browser.msgBox("Oops, an Error has occurred","Error: " + e, Browser.Buttons.OK) 236 | } 237 | } 238 | 239 | /* ---------------------------------------------------------------------------* 240 | Helper functions END 241 | * -------------------------------------------------------------------------*/ 242 | 243 | 244 | 245 | /* ---------------------------------------------------------------------------* 246 | MAIN functions START 247 | * -------------------------------------------------------------------------*/ 248 | 249 | 250 | /** 251 | * Returns Semrush domain history for a specified domain from January 2012 onwards 252 | * 253 | * @param {"example.com"} domain REQUIRED The root domain, example: "nytimes.com", DO NOT include protocol (http/https) 254 | * @param {"us"} db OPTIONAL The country database you want to search from. Default is US 255 | * @param {201601} date OPTIONAL Leave this blank for current data. YYYYMM format for historical reports, note: always reports on the 15th of the month. 256 | * @param {true} excludeHeaders OPTIONAL true to EXCLUDE column headers or false to include. Default is false. 257 | * @param {true} cache OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) 258 | * @return Returns organic keywords count, organic traffic, organic cost, adwords data 259 | * @customfunction 260 | */ 261 | 262 | function DOMAIN_OVERVIEW_SEMRUSH(domain,db,date,excludeHeaders,cache) { 263 | try { 264 | if (!domain || domain.indexOf("http") > -1) return "Error: Enter a valid domain, do not include protocol"; 265 | if (domain.map) throw "Sorry, the domain parameter cannot be an array of values. It needs to be a single string" 266 | var displayDate = "&display_date=", db = db || SemrushGlobal.data.DEFAULT_DB; 267 | typeof cache === 'boolean' && cache === false ? cache = false: cache = true 268 | 269 | var accountCheck = SemrushGlobal.methods.checkAccount() 270 | if (!accountCheck[0]) return accountCheck[1] 271 | 272 | date ? displayDate+= date + "15" : displayDate = ""; 273 | SemrushGlobal.methods.giveApiRest(); 274 | 275 | if (cache) { 276 | var cacheStringName = ROOT_.encode(arguments.callee.name,arguments); 277 | var cachedResult = ROOT_.checkCache(cacheStringName); 278 | if (cachedResult) return cachedResult; 279 | } 280 | 281 | var result = UrlFetchApp.fetch(SemrushGlobal.queries.domainOverview+SemrushGlobal.data.API_KEY+"&export_columns=Or,Ot,Oc,Ad,At,Ac&domain="+domain+"&database="+db+displayDate).getContentText() 282 | if (result.indexOf("ERROR") > -1) throw result.trim() 283 | if (cache) ROOT_.addToCache(cacheStringName, SemrushGlobal.methods.parseApiResponse(result,excludeHeaders)); 284 | return SemrushGlobal.methods.parseApiResponse(result,excludeHeaders); 285 | } catch (e) { 286 | return e; 287 | } 288 | 289 | 290 | } 291 | 292 | /** 293 | * Returns Semrush Organic keywords for a specified domain 294 | * 295 | * @param {"example.com"} domain REQUIRED The root domain, example: "nytimes.com", DO NOT include protocol (http/https) 296 | * @param {true} filterBy OPTIONAL Use true to include the query in the filter or false to remove the query in the filter. Default is true 297 | * @param {true} matchType OPTIONAL Use true for partial match, use false for exact match. Default is true, partial match 298 | * @param {"apartments"} query OPTIONAL The keyword you want to filter by. Relies on previous 2 parameters. Example: "brown shoes". 299 | * @param {10} limit OPTIONAL Number from 1 to 10000 300 | * @param {"us"} db OPTIONAL The country database you want to search from. Default is US 301 | * @param {201601} date OPTIONAL Leave this blank for current data. YYYYMM format for historical reports, note: always reports on the 15th of the month. 302 | * @param {true} excludeHeaders OPTIONAL true to EXCLUDE column headers or false to include. Default is false. 303 | * @param {true} cache OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) 304 | * @return Access organic keywords for a domain from semrush.com database. 305 | * @customfunction 306 | */ 307 | 308 | 309 | function DOMAIN_ORGANIC_KEYWORDS_SEMRUSH(domain,filterBy,matchType,query,limit,db,date,excludeHeaders,cache) { 310 | try { 311 | if (!domain || domain.indexOf("http") > -1) return "Error: Enter a valid domain, do not include protocol" 312 | if (domain.map) throw "Sorry, the domain parameter cannot be an array of values. It needs to be a single string" 313 | var displayDate = "&display_date=", filterOperator,filterBy,excludeHeaders,query = query || "", limit = limit || 1, db = db || SemrushGlobal.data.DEFAULT_DB, filterBy = filterBy && true, matchType = matchType && true 314 | typeof cache === 'boolean' && cache === false ? cache = false: cache = true 315 | 316 | var accountCheck = SemrushGlobal.methods.checkAccount() 317 | if (!accountCheck[0]) return accountCheck[1] 318 | 319 | if (cache) { 320 | var cacheStringName = ROOT_.encode(arguments.callee.name,arguments); 321 | var cachedResult = ROOT_.checkCache(cacheStringName) ; 322 | if (cachedResult) return cachedResult; 323 | } 324 | 325 | date ? displayDate+= date + "15" : displayDate = ""; 326 | filterBy ? filterBy = "%2B" : filterBy = "-"; 327 | matchType ? matchType = "Co" : matchType = "Eq" 328 | if(query) query = "%7C" + query 329 | 330 | SemrushGlobal.methods.giveApiRest(); 331 | 332 | var result = UrlFetchApp.fetch(SemrushGlobal.queries.domainOrganic+SemrushGlobal.data.API_KEY+"&display_limit="+limit+"&export_columns=Ph,Po,Pp,Pd,Nq,Cp,Ur,Tr,Tc,Co,Nr,Td&domain="+domain+"&display_sort=tr_desc&database="+db+"&display_filter="+filterBy+"%7CPh%7C"+matchType+query+displayDate).getContentText() 333 | if (result.indexOf("ERROR") > -1) throw result.trim() 334 | if (cache) ROOT_.addToCache(cacheStringName, SemrushGlobal.methods.parseApiResponse(result,excludeHeaders)); 335 | return SemrushGlobal.methods.parseApiResponse(result,excludeHeaders); 336 | } catch (e) { 337 | return e; 338 | } 339 | 340 | 341 | } 342 | 343 | 344 | /** 345 | * Returns Historical rankings for domain/keyword combination 346 | * 347 | * @param {"example.com"} domain REQUIRED The root domain, example: "nytimes.com", DO NOT include protocol (http/https) 348 | * @param {"apartments"} query OPTIONAL The keyword you want to filter by. Relies on previous 2 parameters. Example: "brown shoes". 349 | * @param {10} limit OPTIONAL Number from 1 to 10000 350 | * @param {"us"} db OPTIONAL The country database you want to search from. Default is US 351 | * @param {201601} date OPTIONAL Leave this blank for current data. YYYYMM format for historical reports, note: always reports on the 15th of the month. 352 | * @param {true} excludeHeaders OPTIONAL true to EXCLUDE column headers or false to include. Default is false. 353 | * @param {true} cache OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) 354 | * @return Access organic keywords for a domain from semrush.com database 355 | * @customfunction 356 | */ 357 | 358 | function HISTORICAL_RANKING_KEYWORD_SEMRUSH(domain,query,limit,db,date,excludeHeaders,cache) { 359 | 360 | try { 361 | if (!domain || domain.indexOf("http") > -1) return "Error: Enter a valid domain, do not include protocol" 362 | var displayDate = "&display_date=",filterBy = "%2B", matchType = "Eq", excludeHeaders,query = query || "", limit = limit || 1, db = db || SemrushGlobal.data.DEFAULT_DB 363 | typeof cache === 'boolean' && cache === false ? cache = false: cache = true 364 | var accountCheck = SemrushGlobal.methods.checkAccount() 365 | if (!accountCheck[0]) return accountCheck[1] 366 | 367 | if (cache) { 368 | var cacheStringName = ROOT_.encode(arguments.callee.name,arguments) 369 | var cachedResult = ROOT_.checkCache(cacheStringName) 370 | if (cachedResult) return cachedResult 371 | } 372 | date ? displayDate+= date + "15" : displayDate = ""; 373 | 374 | SemrushGlobal.methods.giveApiRest(); 375 | 376 | var result = UrlFetchApp.fetch(SemrushGlobal.queries.domainOrganic+SemrushGlobal.data.API_KEY+"&display_limit="+limit+"&export_columns=Po&domain="+domain+"&display_sort=tr_desc&database="+db+"&display_filter="+filterBy+"%7CPh%7C"+matchType+"%7C"+query+displayDate).getContentText() 377 | if (result.indexOf("ERROR") > -1) throw result.trim() 378 | if (cache) ROOT_.addToCache(cacheStringName, SemrushGlobal.methods.parseApiResponse(result,excludeHeaders)); 379 | return SemrushGlobal.methods.parseApiResponse(result,excludeHeaders); 380 | } catch (e) { 381 | return e 382 | } 383 | 384 | 385 | } 386 | 387 | 388 | 389 | 390 | /** 391 | * Returns Ranking Semrush Organic keywords per URL 392 | * @param {"http://example.com"} url REQUIRED The exact URL you want data for, MUST include protocol (http/https) 393 | * @param {"10"} limit OPTIONAL Number from 1 to 10,000, for number of results 394 | * @param {"US"} db OPTIONAL The database, example "US" for American database. Default is US 395 | * @param {true} excludeHeaders OPTIONAL true to EXCLUDE column headers or false to include. Default is false. 396 | * @param {true} cache OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) 397 | * @return Access organic keywords for a url from semrush.com database 398 | * @customfunction 399 | */ 400 | 401 | 402 | function URL_ORGANIC_KEYWORDS_SEMRUSH(url,limit,db,excludeHeaders,cache) { 403 | try { 404 | var db = db || SemrushGlobal.data.DEFAULT_DB, limit = limit || 10; 405 | if (!url || url.indexOf("http") == -1) return "Error: Enter a valid URL, ensure you include the protocol"; 406 | if (url.map) throw "Sorry, the URL parameter cannot be an array of values. It needs to be a single string" 407 | typeof cache === 'boolean' && cache === false ? cache = false: cache = true 408 | //semrush won't report on a homepage unless it has a trailing slash; 409 | if (url.match(/\//g).length < 3) url += "/"; 410 | var accountCheck = SemrushGlobal.methods.checkAccount() 411 | if (!accountCheck[0]) return accountCheck[1] 412 | 413 | if (cache) { 414 | var cacheStringName = ROOT_.encode(arguments.callee.name,arguments); 415 | var cachedResult = ROOT_.checkCache(cacheStringName); 416 | if (cachedResult) return cachedResult; 417 | } 418 | SemrushGlobal.methods.giveApiRest(); 419 | 420 | 421 | var result = UrlFetchApp.fetch(SemrushGlobal.queries.urlOrganic + SemrushGlobal.data.API_KEY+ "&display_limit="+limit+"&export_columns=Ph,Po,Nq,Cp,Co,Tr,Tc,Nr,Td&url="+url+"&database="+db).getContentText(); 422 | if (result.indexOf("ERROR") > -1) throw result.trim() 423 | if (cache) ROOT_.addToCache(cacheStringName, SemrushGlobal.methods.parseApiResponse(result,excludeHeaders)); 424 | return SemrushGlobal.methods.parseApiResponse(result,excludeHeaders); 425 | } catch(e) { 426 | return e; 427 | } 428 | } 429 | 430 | 431 | /** 432 | * Returns Semrush Keyword Difficulty for a keyword 433 | * 434 | * @param {"apartments"} query REQUIRED The keyword you want information for. Example: "brown shoes". 435 | * @param {"us"} db OPTIONAL The country database you want to search from. Default is US 436 | * @param {true} excludeHeaders OPTIONAL true to EXCLUDE column headers or false to include. Default is false. 437 | * @param {true} cache OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) 438 | * @return Access keyword difficulty for keyword from semrush 439 | * @customfunction 440 | */ 441 | 442 | function KEYWORD_DIFFICULTY_SEMRUSH(query,db,excludeHeaders,cache) { 443 | try { 444 | if (!query) return "Error: Missing query"; 445 | if (query.map) throw "Sorry, the query parameter cannot be an array of values. It needs to be a single string" 446 | typeof cache === 'boolean' && cache === false ? cache = false: cache = true 447 | var db = db || SemrushGlobal.data.DEFAULT_DB; 448 | 449 | var accountCheck = SemrushGlobal.methods.checkAccount() 450 | if (!accountCheck[0]) return accountCheck[1] 451 | 452 | if (cache) { 453 | var cacheStringName = ROOT_.encode(arguments.callee.name,arguments); 454 | var cachedResult = ROOT_.checkCache(cacheStringName); 455 | if (cachedResult) return cachedResult; 456 | } 457 | SemrushGlobal.methods.giveApiRest(); 458 | var result = UrlFetchApp.fetch(SemrushGlobal.queries.keywordDifficulty + SemrushGlobal.data.API_KEY+ "&export_columns=Ph,Kd&phrase="+query+"&database=" + db).getContentText(); 459 | if (result.indexOf("ERROR") > -1) throw result.trim() 460 | if (cache) ROOT_.addToCache(cacheStringName, SemrushGlobal.methods.parseApiResponse(result,excludeHeaders)); 461 | return SemrushGlobal.methods.parseApiResponse(result,excludeHeaders); 462 | } catch(e) { 463 | return e 464 | } 465 | } 466 | 467 | /** 468 | * Returns Semrush organic search results for a specific keyword 469 | * 470 | * @param {"apartments"} query REQUIRED The keyword you want information for. Example: "brown shoes". 471 | * @param {10} limit OPTIONAL Number from 10 to 20, for number of results. Default is 10 472 | * @param {"us"} db OPTIONAL The country database you want to search from. Default is US 473 | * @param {true} excludeHeaders OPTIONAL true to EXCLUDE column headers or false to include. Default is false. 474 | * @param {true} cache OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) 475 | * @return Access organic search results for a keyword from semrush.com database 476 | * @customfunction 477 | */ 478 | 479 | function SERPS_SEMRUSH(query,limit,db,excludeHeaders,cache) { 480 | 481 | try { 482 | if (!query) return "Error: Missing query"; 483 | if (query.map) throw "Sorry, the query parameter cannot be an array of values. It needs to be a single string" 484 | var db = db || SemrushGlobal.data.DEFAULT_DB, limit = limit || 10; 485 | typeof cache === 'boolean' && cache === false ? cache = false: cache = true 486 | var accountCheck = SemrushGlobal.methods.checkAccount() 487 | if (!accountCheck[0]) return accountCheck[1] 488 | 489 | 490 | if (cache) { 491 | var cacheStringName = ROOT_.encode(arguments.callee.name,arguments); 492 | var cachedResult = ROOT_.checkCache(cacheStringName); 493 | if (cachedResult) return cachedResult; 494 | } 495 | 496 | SemrushGlobal.methods.giveApiRest(); 497 | var result = UrlFetchApp.fetch(SemrushGlobal.queries.phraseOrganic + SemrushGlobal.data.API_KEY+"&phrase="+query+"&export_columns=Dn,Ur,Fl&database="+db+"&display_limit="+limit).getContentText(); 498 | if (result.indexOf("ERROR") > -1) throw result.trim() 499 | if (cache) ROOT_.addToCache(cacheStringName, SemrushGlobal.methods.parseApiResponse(result,excludeHeaders)); 500 | return SemrushGlobal.methods.parseApiResponse(result); 501 | } catch (e) { 502 | return e 503 | } 504 | } 505 | 506 | /** 507 | * Returns Related search queries for a keyword from Semrush 508 | * 509 | * @param {"apartments"} query REQUIRED The keyword you want information for. Example: "brown shoes". 510 | * @param {10} limit OPTIONAL The number of results. Default is 1 511 | * @param {"us"} db OPTIONAL The country database you want to search from. Default is US 512 | * @param {true} excludeHeaders OPTIONAL true to EXCLUDE column headers or false to include. Default is false. 513 | * @param {true} cache OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) 514 | * @return Returns related queries for a specific keyword from semrush.com 515 | * @customfunction 516 | */ 517 | 518 | 519 | function RELATED_QUERIES_SEMRUSH(query,limit,db,excludeHeaders,cache) { 520 | try { 521 | if (!query) return "Error: Missing query"; 522 | if (query.map) throw "Sorry, the query parameter cannot be an array of values. It needs to be a single string" 523 | var limit = limit || 1, db = db || SemrushGlobal.data.DEFAULT_DB; 524 | typeof cache === 'boolean' && cache === false ? cache = false: cache = true 525 | var accountCheck = SemrushGlobal.methods.checkAccount() 526 | if (!accountCheck[0]) return accountCheck[1] 527 | 528 | if (cache) { 529 | var cacheStringName = ROOT_.encode(arguments.callee.name,arguments); 530 | var cachedResult = ROOT_.checkCache(cacheStringName); 531 | if (cachedResult) return cachedResult; 532 | } 533 | SemrushGlobal.methods.giveApiRest(); 534 | 535 | var result = UrlFetchApp.fetch(SemrushGlobal.queries.relatedQueries + SemrushGlobal.data.API_KEY+"&display_limit="+limit+"&export_columns=Ph,Nq,Cp,Co,Nr,Td&phrase="+query+"&database="+db+"&display_sort=nq_desc").getContentText(); 536 | if (result.indexOf("ERROR") > -1) throw result.trim() 537 | if (cache) ROOT_.addToCache(cacheStringName, SemrushGlobal.methods.parseApiResponse(result,excludeHeaders)); 538 | return SemrushGlobal.methods.parseApiResponse(result,excludeHeaders); 539 | } catch(e) { 540 | return e; 541 | } 542 | 543 | } 544 | 545 | /** 546 | * Returns Keyword Volume from semrush 547 | * 548 | * @param {"apartments"} query REQUIRED The keyword you want information for. Example: "brown shoes". 549 | * @param {"us"} db OPTIONAL The country database you want to search from, default is "us" 550 | * @param {true} excludeHeaders OPTIONAL true to EXCLUDE column headers or false to include. Default is false. 551 | * @param {true} cache OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) 552 | * @return Returns search volume, cpc, etc.. 553 | * @customfunction 554 | */ 555 | 556 | function KEYWORD_VOLUME_SEMRUSH(query,db,excludeHeaders,cache) { 557 | try { 558 | if (!query) return "Error: Missing query"; 559 | var db = db || SemrushGlobal.data.DEFAULT_DB; 560 | SemrushGlobal.methods.giveApiRest(); 561 | typeof cache === 'boolean' && cache === false ? cache = false: cache = true 562 | var accountCheck = SemrushGlobal.methods.checkAccount() 563 | if (!accountCheck[0]) return accountCheck[1] 564 | 565 | if (query.map) throw "Sorry, the query parameter cannot be an array of values. It needs to be a single string" 566 | 567 | if (cache) { 568 | var cacheStringName = ROOT_.encode(arguments.callee.name,arguments); 569 | var cachedResult = ROOT_.checkCache(cacheStringName); 570 | if (cachedResult) return cachedResult; 571 | } 572 | 573 | var result = UrlFetchApp.fetch(SemrushGlobal.queries.keywordVolume + SemrushGlobal.data.API_KEY+"&export_columns=Ph,Nq,Cp,Co&phrase="+query+"&database="+db).getContentText(); 574 | if (result.indexOf("ERROR") > -1) throw result.trim() 575 | if (cache) ROOT_.addToCache(cacheStringName, SemrushGlobal.methods.parseApiResponse(result,excludeHeaders)); 576 | return SemrushGlobal.methods.parseApiResponse(result,excludeHeaders); 577 | 578 | } catch (e) { 579 | return e; 580 | } 581 | } 582 | 583 | 584 | /** 585 | * Returns Questions asked for a keyword from Semrush 586 | * 587 | * @param {"apartments"} query REQUIRED The keyword you want information for. Example: "brown shoes". 588 | * @param {10} limit OPTIONAL The number of results. Default is 1 589 | * @param {"us"} db OPTIONAL The country database you want to search from. Default is US 590 | * @param {true} filterBySearchVolume OPTIONAL If you want to filter by search volume, use TRUE, default is FALSE 591 | * @param {">"} searchVolumeFilter OPTIONAL Filters the search volume parameter (next). Use ">" for Greater than, or "<" for Less than. Default is ">" greater than 592 | * @param {1000} searchVolume OPTIONAL A search volume number (per month) you want to filter results by. Both previous parameters need to be set for this to work. 593 | * @param {true} excludeHeaders OPTIONAL true to EXCLUDE column headers or false to include. Default is false. 594 | * @param {true} cache OPTIONAL use FALSE if you DO NOT want to cache these results or DO NOT want to return cached results, default is TRUE (cache enabled) 595 | * @return Returns Questions asked for a specific keyword from semrush.com 596 | * @customfunction 597 | */ 598 | 599 | 600 | function PHRASE_QUESTIONS_SEMRUSH(query,limit,db,filterBySearchVolume,searchVolumeFilter,searchVolume, excludeHeaders,cache) { 601 | try { 602 | if (!query) return "Error: Missing query"; 603 | var limit = limit || 1, db = db || SemrushGlobal.data.DEFAULT_DB, searchVolumeFilter = searchVolumeFilter || ">"; 604 | typeof cache === 'boolean' && cache === false ? cache = false: cache = true 605 | SemrushGlobal.methods.giveApiRest(); 606 | var accountCheck = SemrushGlobal.methods.checkAccount() 607 | if (!accountCheck[0]) return accountCheck[1] 608 | 609 | if (cache) { 610 | var cacheStringName = ROOT_.encode(arguments.callee.name,arguments); 611 | var cachedResult = ROOT_.checkCache(cacheStringName); 612 | if (cachedResult) return cachedResult; 613 | } 614 | searchVolumeFilter === ">" ? searchVolumeFilter = "Gt" : searchVolumeFilter = "Lt" 615 | filterBySearchVolume? filterBySearchVolume = "&display_filter=%2B%7CNq%7C"+searchVolumeFilter+"%7C"+searchVolume: filterBySearchVolume = "" 616 | var result = UrlFetchApp.fetch(SemrushGlobal.queries.phraseQuestions + SemrushGlobal.data.API_KEY+"&phrase="+query+"&export_columns=Ph,Nq,Cp,Co,Nr,Td&database="+db+"&display_limit="+limit+"&display_sort=nq_desc"+filterBySearchVolume).getContentText(); 617 | if (result.indexOf("ERROR") > -1) throw result.trim() 618 | if (cache) ROOT_.addToCache(cacheStringName, SemrushGlobal.methods.parseApiResponse(result,excludeHeaders)); 619 | return SemrushGlobal.methods.parseApiResponse(result,excludeHeaders); 620 | } catch(e) { 621 | return e; 622 | } 623 | 624 | } 625 | 626 | 627 | /* ---------------------------------------------------------------------------* 628 | MAIN functions END 629 | * -------------------------------------------------------------------------*/ 630 | -------------------------------------------------------------------------------- /sidebar: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 27 | 28 | 29 | 30 |
31 |
32 |
33 |

Opensourceseo.org

34 |

Semrush Library Reference (see post)

35 |
36 |
37 |
38 |
39 |
40 |
41 | 44 |
45 |
46 | 47 |
48 |
49 |

DOMAIN_ORGANIC_KEYWORDS_SEMRUSH

50 |

Params: (domain, filterBy, matchType, query, limit, db, excludeHeaders, date, cache)

51 |

Returns: lists keywords that bring users to a domain via Google’s top 20 organic search results.

52 | 53 |

DOMAIN_OVERVIEW_SEMRUSH

54 |

Params: (query, excludeHeaders, db, cache)

55 |

Returns: provides live or historical data on a domain’s keyword rankings in both organic and paid search in a chosen regional database.

56 |
57 |
58 |
59 |
60 |
61 |
62 | 65 |
66 |
67 |
68 |
69 | 70 |

KEYWORD_DIFFICULTY_SEMRUSH

71 |

Params: (query, excludeHeaders, db, cache)

72 |

Returns: provides keyword difficulty, an index that helps to estimate how difficult it would be to seize competitors’ positions in organic search within the Google’s top 20 with an indicated search term

73 | 74 |

KEYWORD_VOLUME_SEMRUSH

75 |

Params: (query, excludeHeaders, db, cache)

76 |

Returns: provides a summary of a keyword, including its volume, CPC, competition, and the number of results in all regional databases

77 | 78 |

RELATED_QUERIES_SEMRUSH

79 |

Params: (query, excludeHeaders, limit, db, cache)

80 |

Returns: provides an extended list of related keywords, synonyms, and variations relevant to a queried term in a chosen database.

81 | 82 |

PHRASE_QUESTIONS_SEMRUSH

83 |

Params: (query, limit, db, filterBySearchVolume, searchVolumeFilter, searchVolume, excludeHeaders, cache)

84 |

Returns: provides a list of phrase questions relevant to a queried term in a chosen database

85 | 86 | 87 |
88 |
89 |
90 |
91 |
92 |
93 | 96 |
97 |
98 |
99 |
100 | 101 |

URL_ORGANIC_KEYWORDS_SEMRUSH

102 |

Params: (url, excludeHeaders, limit, db, cache)

103 |

Returns: lists keywords that bring users to a URL via Google’s top 20 organic search results..

104 | 105 |

SERPS_SEMRUSH

106 |

Params: (query,limit,db,excludeHeaders,cache)

107 |

Returns: lists domains that are ranking in Google’s top 20 organic search results with a requested keyword.

108 |
109 |
110 |
111 |
112 |
113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | --------------------------------------------------------------------------------