---
title: "tutorial"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{tutorial}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

load("my_googleanalytics_data.RData")
```

```{r setup}
library(fetchGoogleAnalyticsR)

library(dplyr)
library(ggplot2)
```


## 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 `fetchGoogleAnalyticsR` to gain insights into marketing campaign performance. 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 marketing data from Google Analytics. 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 Analytics 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}

my_googleanalytics_data <-
  fetch_googleanalytics(api_key = "your api key",
           date_from = Sys.Date()-100,
           date_to = Sys.Date(),
           fields = c("campaign", "clicks",
                      "spend", "impressions", "date")) 
```

This code will collect data for the last 100 days. Lets take a look at the data we just downloaded to get a better idea about the structure and type of information included. 

```{r}
str(my_googleanalytics_data)
```

## Analyzing Google Analytics marketing data 

Now we can analyze data from Google Analytics data. For instance, let's compare the two campaigns we have to see which one performed better the last 100 days. 

```{r}
ggplot(my_googleanalytics_data, aes(y = clicks, fill = campaign)) + geom_boxplot()
```

It looks like APAC campaign is performing better than  UK&CO in number of clicks. Now let's see if this difference is statistically significant by using generalized linear models, as our variable response is number of clicks, which have a Poisson distribution.

```{r}
lmod <- glm(clicks ~ campaign, data = my_googleanalytics_data, family = "poisson")
summary(lmod)
```

We can see that differences among campaigns are statistically significant and that the campaign UK&CO have a mean that is 0.79 lower than the APAC campaign.
