---
title: "Basic work with googleadsR"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Basic work with googleadsR}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

load(file = "my_data_googleads.rda")
```

## 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://windsor.ai/) API and `R` package `googleadsR` to gain insights into marketing campaign performance in Google Ads. 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. In this case, however, the package is focused on getting data from Google Ads campaigns. 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 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(googleadsR)
my_data_googleads <- 
  fetch_googleads(
  api_key = "your api key")
)
```

Lets take a look at the data we just downloaded to get a better idea about the structure and type of information included. 

```{r, echo = FALSE}
library(googleadsR)
```

```{r}
str(my_data_googleads)
```

## Analyzing our 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 (`campaign` column) and sum up the click count per campaign.

```{r}
library(dplyr)
library(ggplot2)

top_10 <- 
  my_data_googleads %>% 
  filter(clicks > 0) %>% 
  group_by(campaign) %>% 
  summarise(n_clicks = sum(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 = campaign)) +
  geom_col()
```

We can apply the same type of data manipulation and plotting to check the `spend` values.

```{r, fig.width= 7}
my_data_googleads %>%
  filter(clicks > 0) %>%
  group_by(campaign) %>%
  summarise(sum_spend = sum(as.numeric(spend))) %>%
  ungroup %>%
  arrange(desc(sum_spend)) %>%
  slice_head(n = 10) %>%
  ggplot(aes(x = sum_spend, y = 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_googleads %>%
  filter(clicks > 0) %>%
  group_by(campaign) %>%
  summarise(n_clicks = sum(clicks), sum_spend = sum(as.numeric(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 = campaign, fill = aggreg)) +
  geom_col() +
  facet_wrap("aggreg", ncol = 2) + 
  theme(legend.position="bottom")
```

