#' @title Get Daily AT Internet Data - Relative Period
#' @description This function allows you to get daily data from 'AT Internet' using relative period. Relative periods allow you to use periods that are automatically updated based on the last minute of available data. It is no longer necessary to change the date, week, etc : you simply need to specify which month, week, etc. you want to refer to for this last minute of available data.
#' METRICS are visits, visitors, conversions visits, conversions rate, visits with sales, time spent per pages, bounce rate
#'
#'
#' @param ATuser Your 'AT Internet' user login
#' @param ATpassword Your 'AT Internet' password login
#' @param space Analysis space
#' @param period_duration Analysis period
#'
#' @param dim_source Optional - DIMENSION Trafic Source : Direct+traffic, Search+engines, "Referrer+sites", "Social+Campaigns"
#' @param dim_device Optional - DIMENSION Device Type
#' @param dim_level2 Optional - DIMENSION Sub-set of your site
#'
#' @param segment Optional - SEGMENT applied to the result set
#'
#' @param pagination By defaut the pagination is set to 1. This allows you to get 10,000 rows of data. If you need more, you must make multiple calls to the API using the pagination feature.
#'
#' @return day_relative extract the data you need from 'AT Internet' database, according to parameters you entered and then provides a dataset.
#'
#' @import httr
#' @import data.table
#'
#' @examples
#'\dontrun{
#'ATuser <- "user_email_adress"
#'ATpassword <- "my_password"
#'space <- 113736
#'period_duration <- 90
#'dim_source <- "Search+engines"
#'dim_device <- "Desktop"
#'dim_level2 <- "MyLevel2"
#'segment <- 204529097
#'pagination <- 1
#'
#' dataset <- day_relative(ATuser, ATpassword, space, period_duration, dim_source, dim_device, dim_level2, segment, pagination)
#'}
#'
#' @export
day_relative <- function(ATuser, ATpassword, space, period_duration, dim_source = NULL, dim_device = NULL, dim_level2 = NULL, segment = NULL, pagination=1){
ATpassword <- as.character(ATpassword)
httr::set_config( httr::config( ssl_verifypeer = 0L ) )
result <- data.frame(matrix(nrow=0, ncol=11))
#DAILY
cnames <- c("d_time_date","d_l2","d_source","d_device_type",
"m_visits","m_visitors","m_conversions1_visits","m_conversions1_rate",
"m_v_sales","m_time_spent_per_pages", "m_bounce_rate"
)
colnames(result) <- cnames
for (i in 1:period_duration) {
time <- i
api_request <- paste0("https://apirest.atinternet-solutions.com/data/v2/json/getData?",
#DAILY
"&columns={d_time_date,d_l2,d_source,d_device_type,m_visits,m_visitors,m_conversions1_visits,m_conversions1_rate,m_v_sales,m_time_spent_per_pages,m_bounce_rate}",
"&sort={d_time_date}",
"&space={s:'", space,"'}",
"&period={R:{D:{start:'-",time,"',end:'-",time,"'}}}",
#OPTIONAL
"&filter={",
#Dim_Source
ifelse(!is.null(dim_source),
paste0("d_source:{$eq:'",dim_source,"'}"),""),
#First Comma
ifelse(
((!is.null(dim_source) & !is.null(dim_device)) | (!is.null(dim_source) & !is.null(dim_level2))),
paste0(","),
""),
#Dim_Device
ifelse(!is.null(dim_device),
paste0("d_device_type:{$eq:'",dim_device,"'}"),""),
#Second Comma
ifelse(
((!is.null(dim_device) & !is.null(dim_level2)) | (!is.null(dim_source) & !is.null(dim_device) & !is.null(dim_level2))),
paste0(","),
""),
#Dim_level2
ifelse(!is.null(dim_level2),
paste0("d_l2:{$lk:'",dim_level2,"'}"),""),
"}",
#"&segment=",segment,
ifelse(!is.null(segment),
paste0("&segment=",segment),""),
"&max-results=10000&page-num=",pagination
)
result = tryCatch({
api_request <- URLencode(api_request, repeated = TRUE)#pour encoder en UTF 8
req <- httr::GET(api_request, httr::authenticate(user = ATuser, password = ATpassword))
if (req$status_code != 200) {
print("***** HTTP QUERY FAILED with code ")
print (req$status_code)
print (httr::content(req))
stop("HTTP query failed")
}
result_doing <- httr::content(req)
list <- result_doing$DataFeed[[1]]$Rows
result_doing <- data.table::rbindlist(list, fill=TRUE)
df <- rbind(result, result_doing)
})
}
df$space <- space
df$segment <- segment
df <- df[order(df$d_time_date),]
return(df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.