Skip to content

Instantly share code, notes, and snippets.

@ArthurCa
Last active March 29, 2024 20:01
Show Gist options
  • Save ArthurCa/8dd9dd4c07c74d19861477da77adef84 to your computer and use it in GitHub Desktop.
Save ArthurCa/8dd9dd4c07c74d19861477da77adef84 to your computer and use it in GitHub Desktop.
Find it on the article: https://arthur.camberlein.com/blogs/articles/leverage-similar-web-api-r || Adding several URL as variables in the R script (api-similarweb.R) from Vincent Terrasi (@voltek62) | Source : https://gist.github.com/voltek62/784cf6cb29c76c182ae12b0481645fc2 |
# All rights reserved to Vincent Terrasi (@voltek62) and his script api-similarweb.R -> https://gist.github.com/voltek62/784cf6cb29c76c182ae12b0481645fc2
library(httr)
library(jsonlite)
# Doc : https://www.similarweb.com/corp/developer/
# Create your key here : https://pro.similarweb.com/#/account/api-management
# Free for 3 Months of Web Traffic Data
key <- "YOURKEY"
# Create a vector of your sites
sites <- c("yourwebsite1.com", "yourwebsite2.com", "yourwebsite3.com", "yourwebsite4.com", "yourwebsite5.com")
# Base URL for the API
base_url <- "https://api.similarweb.com/v1/website/"
# Parameters of your request
params <- "/total-traffic-and-engagement/visits?api_key="
start_date <- "2018-03"
end_date <- "2018-05"
main_domain_only <- "false"
granularity <- "monthly"
# Loop through each site, get and print the JSON results for visits
for (site in sites) {
# Construct the API call URL
url <- paste0(base_url, site, params, key, "&start_date=", start_date, "&end_date=", end_date, "&main_domain_only=", main_domain_only, "&granularity=", granularity)
# Perform the GET request and process the result
result <- GET(url)
text_content <- content(result, as = "text", encoding = "UTF-8")
json_content <- fromJSON(text_content)
# Print site for clarity
cat("Visits for site", site, ":\n")
print(json_content$visits)
cat("\n") # Just to add a line break for better readability
}
# All rights reserved to Vincent Terrasi (@voltek62) and his script api-similarweb.R -> https://gist.github.com/voltek62/784cf6cb29c76c182ae12b0481645fc2
library(httr)
library(jsonlite)
# Doc : https://www.similarweb.com/corp/developer/
# Create your key here : https://pro.similarweb.com/#/account/api-management
# Free for 3 Months of Web Traffic Data
# Build your url
key <- "YOURKEY"
your_site1 <- "yourwebsite1.com"
url1 <- paste0("https://api.similarweb.com/v1/website/",your_site1,"/total-traffic-and-engagement/visits?api_key=",key,"&start_date=2018-03&end_date=2018-05&main_domain_only=false&granularity=monthly")
your_site2 <- "yourwebsite2.com"
url2 <- paste0("https://api.similarweb.com/v1/website/",your_site2,"/total-traffic-and-engagement/visits?api_key=",key,"&start_date=2018-03&end_date=2018-05&main_domain_only=false&granularity=monthly")
your_site3 <- "yourwebsite3.com"
url3 <- paste0("https://api.similarweb.com/v1/website/",your_site3,"/total-traffic-and-engagement/visits?api_key=",key,"&start_date=2018-03&end_date=2018-05&main_domain_only=false&granularity=monthly")
your_site4 <- "yourwebsite4.com"
url4 <- paste0("https://api.similarweb.com/v1/website/",your_site4,"/total-traffic-and-engagement/visits?api_key=",key,"&start_date=2018-03&end_date=2018-05&main_domain_only=false&granularity=monthly")
your_site5 <- "yourwebsite5.com"
url5 <- paste0("https://api.similarweb.com/v1/website/",your_site5,"/total-traffic-and-engagement/visits?api_key=",key,"&start_date=2018-03&end_date=2018-05&main_domain_only=false&granularity=monthly")
# Get your result for each website
result1 <- GET(url1)
result2 <- GET(url2)
result3 <- GET(url3)
result4 <- GET(url4)
result5 <- GET(url5)
textcontent1 <- content(result1,as = "text", encoding = "UTF-8")
textcontent2 <- content(result2,as = "text", encoding = "UTF-8")
textcontent3 <- content(result3,as = "text", encoding = "UTF-8")
textcontent4 <- content(result4,as = "text", encoding = "UTF-8")
textcontent5 <- content(result5,as = "text", encoding = "UTF-8")
jsoncontent1 <- fromJSON(textcontent1)
jsoncontent2 <- fromJSON(textcontent2)
jsoncontent3 <- fromJSON(textcontent3)
jsoncontent4 <- fromJSON(textcontent4)
jsoncontent5 <- fromJSON(textcontent5)
# Print your results
print(jsoncontent1$visits)
print(jsoncontent2$visits)
print(jsoncontent3$visits)
print(jsoncontent4$visits)
print(jsoncontent5$visits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment