Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Check 0 pageviews by comparing sitemap.XML URLs with Google Analytics visits.
## date range of URLs to test
dates <- c(Sys.Date() - 30, Sys.Date())
##GA View ID
id <- 11111111
## function to get sitemap URLs
get_sitemap <- function(sitemap, field = "loc"){
sm <- as_list(read_xml(sitemap))
out <- try(Reduce(rbind,
vapply(sm, function(x) Reduce(rbind, x[[field]]), character(1))
if(inherits(out, "try-error")){
message("Problem with sitemap:", sitemap)
## make google SEO filter
google_seo <- filter_clause_ga4(
dim_filter("source", "EXACT", "google"),
dim_filter("medium", "EXACT", "organic")
operator = "AND")
## get the pages
pages <- google_analytics_4(id,
date_range = dates,
dimensions = "pagePath",
metrics = c("pageviews","totalEvents"),
dim_filters = google_seo,
max = -1,
anti_sample = TRUE)
## get the sitemap index file
url_si <- ""
sitemap_index <- get_sitemap(url_si)
## get all the sitemaps (maybe you only need the call above if you have no sitemap index)
many_sitemaps <- lapply(sitemap_index, get_sitemap)
## all the urls in all the sitemaps
all_urls <- Reduce(c, many_sitemaps)
## Compare and get the URLs that are in XML but not in Google Analytics
## dplyr transformations
sitemap_urls <- as.tbl(, stringsAsFactors = FALSE))
sitemap_urls <- sitemap_urls %>% mutate(path = paste0("/",urltools::path(all_urls)))
sitemap_not_in_ga <- anti_join(sitemap_urls, pages, by = c(path = "pagePath"))
## write out to CSV
write.csv(sitemap_not_in_ga, file = "./data/sitemap_urls_not_in_ga.csv", row.names = FALSE)
Copy link

withetu commented Jan 30, 2017


I am a beginner in R. While do run your R code for my GA account, here I stuck up

get the sitemap index file

url_si <- ""
sitemap_index <- get_sitemap(url_si)

Error in x[[field]] : subscript out of bounds
Problem with sitemap:

Please help me!

Thank you

Copy link

MarkEdmondson1234 commented Jun 21, 2017

Sorry I get no notifications for this so missed it. Its saying you have no field in the sitemap, is it a correctly configured one? I realise you may never see this, for the same reasons I didn't.

Copy link

stringbenderb5 commented Jul 5, 2017

Where would I place this code in a wordpress site? and will it work in wordpress?

Copy link

MarkEdmondson1234 commented Jun 13, 2018

Just saw this, sorry. It won't work in Wordpress, which is PHP. This is a script to run in R, locally on your laptop, perhaps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment