--- title: "Basic work with WindsoraiR" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Basic work with WindsoraiR} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` ## Goal The goal here is to outline in a couple of paragraphs and few lines of code some simple ways in which we can use the [Windsor.ai](https://www.windsor.ai/) API and `R` package `windsoraiR` to gain insights into marketing campaign performance across channels like Google and Facebook. The nice thing about Windsor.ai is that you can have all of your marketing channels aggregating in a single place and then access all data at once using this package. Of course, once the data is in `R` you can do much more than the examples below, and work on analysis, predictions or dashboards. ## Getting data from Facebook and Google ads into R After we create an account at Windsor.ai and obtain an API key, collecting our data from Windsor to R is as easy as: ```{r, eval = FALSE} library(windsoraiR) my_data <- windsor_fetch( api_key = "your api key", date_preset = "last_7d", fields = c("source", "campaign", "clicks", "medium", "sessions", "spend") ) ``` Lets take a peek at the data we just downloaded to get a better idea about the structure and type of information included. ```{r, echo = FALSE} library(windsoraiR) ``` ```{r} str(my_data) ``` ## Analyzing our Facebook and Google ad campaign data First, lets try to find the campaigns with most clicks. To do this, we'll filter only those rows that have clicks, then group the dataset by campaign (`data.campaign` column) and sum up the click count per campaign. ```{r} library(dplyr) library(ggplot2) top_10 <- my_data %>% filter(data.clicks > 0) %>% group_by(data.campaign) %>% summarise(n_clicks = sum(data.clicks)) %>% ungroup %>% arrange(desc(n_clicks)) %>% slice_head(n = 10) knitr::kable(top_10) ``` Thereafter we can quickly visualize our data using `ggplot2` ```{r, fig.width=7} ggplot(top_10, aes(x = n_clicks, y = data.campaign)) + geom_col() ``` We can gain further insight by grouping by other variables add mapping them to the plot aesthetics. For example, in this case we might want to keep track of the data source (linkedin, google, ...). So we'll modify our `magrittr` chain to add another grouping variable before tallying the clicks. ```{r} top_10 <- my_data %>% filter(data.clicks > 0) %>% group_by(data.source, data.campaign) %>% summarise(n_clicks = sum(data.clicks)) %>% ungroup %>% arrange(desc(n_clicks)) %>% slice_head(n = 10) knitr::kable(top_10) ``` ```{r, fig.width=7} ggplot(top_10, aes(x = n_clicks, y = data.campaign, fill = data.source)) + geom_col() ``` In this case the vast majority of data (all of it for the top 10 ad campaigns) comes from Google, so only this source is labeled on the graph. We can apply the same type of data manipulation and plotting to check the `data.spend` values. ```{r, fig.width= 7} my_data %>% filter(data.clicks > 0) %>% group_by(data.campaign) %>% summarise(sum_spend = sum(as.numeric(data.spend))) %>% ungroup %>% arrange(desc(sum_spend)) %>% slice_head(n = 10) %>% ggplot(aes(x = sum_spend, y = data.campaign)) + geom_col() ``` Finally, for a direct comparison, we can aggregate both the clicks and spending per ad campaign and plot them jointly: ```{r, fig.width=10} library(tidyr) my_data %>% filter(data.clicks > 0) %>% group_by(data.campaign) %>% summarise(n_clicks = sum(data.clicks), sum_spend = sum(as.numeric(data.spend))) %>% arrange(desc(sum_spend)) %>% slice_head(n = 10) %>% pivot_longer(cols = c("n_clicks", "sum_spend"), names_to = "aggreg", values_to = "values") %>% ggplot(aes(x = values, y = data.campaign, fill = aggreg)) + geom_col() + facet_wrap("aggreg", ncol = 2) + theme(legend.position="bottom") ```