---
title: "Exploring All of The Data - Google Store"
author: "Brett Kobold"
date: "9/16/2018"
output: html_document
---

Loading in library and function to convert JSON blobs coloums to 
```{r}
library(readr)
library(tidyverse)
library(jsonlite)
library(lubridate)
```
Thanks to [Mark Edmondson](https://www.kaggle.com/marked/using-r-tidyeval-to-expand-json-into-columns) for this function! Very clean adnd helpful
```{r}
json_to_columns <- function(df, column){
  column <- enquo(column)
  
  json_df <- df %>% 
    pull(!!column) %>% 
    paste(collapse = ",") %>% 
    paste("[",.,"]") %>% 
    fromJSON(flatten = TRUE)
  
  df %>% 
    select(-!!column) %>% 
    bind_cols(json_df)
}
```

Loading in data and extracting data to columns from JSON.
```{r}
train <- read_csv("../input/train.csv")
test <- read_csv("../input/test.csv")

train <- train %>% 
  json_to_columns(device) %>% 
  json_to_columns(geoNetwork) %>% 
  json_to_columns(totals) %>% 
  json_to_columns(trafficSource)

test <- test %>% 
  json_to_columns(device) %>% 
  json_to_columns(geoNetwork) %>% 
  json_to_columns(totals) %>% 
  json_to_columns(trafficSource)
```

Looking at metrics from the site by day.
```{r}
#visitors, visits, and pagesviews
train %>% mutate(date = ymd(date)) %>%
  group_by(date) %>%
  summarise(users = n_distinct(fullVisitorId),
            visits = n_distinct(sessionId),
            pageviews = as.numeric(pageviews) %>% sum(na.rm = TRUE),
            hits = as.numeric(hits) %>% sum(na.rm = TRUE),
            revenue = as.numeric(transactionRevenue) %>% log %>% sum(na.rm = TRUE)) %>% 
  gather(dimesions, value, 2:6) %>%
  ggplot() +
  geom_line(aes(date, value, group = dimesions, color = dimesions))
```

Looking at metrics from the site by week.
```{r}
#visitors, visits, and pagesviews
train %>% mutate(date = ymd(date)) %>%
  group_by(date = ceiling_date(date, "week")) %>%
  summarise(users = n_distinct(fullVisitorId),
            visits = n_distinct(sessionId),
            pageviews = as.numeric(pageviews) %>% sum(na.rm = TRUE),
            hits = as.numeric(hits) %>% sum(na.rm = TRUE),
            revenue = as.numeric(transactionRevenue) %>% log %>% sum(na.rm = TRUE)) %>%
  gather(dimesions, value, 2:6) %>%
  ggplot() +
  geom_line(aes(date, value, group = dimesions, color = dimesions))
```
