Arthur Camberlein >> SEO & Data articles >> Leverage the similar web API on R

Leverage the similar web API on R

Written by Arthur Camberlein | Published on & updated on

A few years back I reused some of Vincent Terrasi code on R to look for URL comparison on Similar web.

What my first version of the code looked like

And it looked like this:

# 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)

The reforrmated version is better

Now, I reformated it a bit with a loop and it looks way better (isn't it?)

library(httr)
library(jsonlite)

# Set your API key
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
}

Copying and pasting the code from Gists

All the code is on:

Back to blog

Learn more with the article FAQ

Leverage the similar web API on R - FAQs

Blog post taggued in: R, SEO

Written by and Reviewed by