This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## functions.R | |
library(rga) | |
library(dygraphs) | |
library(zoo) | |
library(tidyr) | |
library(lubridate) | |
library(d3heatmap) | |
library(dplyr) | |
library(stringr) | |
library(DT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## A script to download and archive Google search analytics | |
## | |
## Demo of searchConsoleR R package. | |
## | |
## Version 1 - 10th August 2015 | |
## | |
## Mark Edmondson (http://markedmondson.me) | |
## load the required libraries | |
## (Download them with install.packages("googleAuthR") and install.packages("searchConsoleR" if necessary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## assuming you have a folder full of .csv's to merge | |
## csv's must all have identical column names. | |
folder <- "./path/to/csv/files" | |
filenames <- list.files(folder) | |
all_files <- Reduce(rbind, lapply(filenames, read.csv)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(googleAuthR) | |
## change the native googleAuthR scopes to the one needed. | |
options("googleAuthR.scopes.selected" = | |
c("https://www.googleapis.com/auth/urlshortener")) | |
#' Shortens a url using goo.gl | |
#' | |
#' @param url URl to shorten with goo.gl | |
#' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## in global.R | |
library(googleAuthR) | |
options("googleAuthR.scopes.selected" = c("https://www.googleapis.com/auth/urlshortener")) | |
shorten_url <- function(url){ | |
body = list( | |
longUrl = url | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("googleAuthR") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(googleAnalyticsR_public) | |
gar_auth(new_user=T) | |
## your profile view Id | |
id <- "XXXXXX" | |
## 61607 results | |
## 30049 unique Ids | |
## 185 Sku's |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## want: 30049 x 187 | |
## userId, product1_view, product2_view, ...., productN_view, productBought | |
pv <- reshape2::recast(product_views, | |
dimension1 ~ productSku + variable, | |
fun.aggregate=sum) | |
library(dplyr) | |
## if a user buys more than once, the row will be duplicated | |
pt <- product_trans %>% select(productSku, dimension1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(randomForest) | |
## warning - can take a long time (30mins) | |
rf <- randomForest(x = predictors, y = response) | |
## once model done, we run it using test data and compare results to reality | |
predictor_test <- test[,which(!names(test) %in% c("dimension1","boughtSku"))] | |
response_test <- as.factor(test[,"boughtSku"]) | |
## check result on test set | |
prediction <- predict(rf, predictor_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## function to get plot data format | |
getCompareTable <- function (test_data, prediction) { | |
require(dplyr) | |
## plot real vs model bought Sku | |
actual_freq <- table(model_data$boughtSku) | |
predicted_freq <- table(prediction) | |
actual_freq <- actual_freq[order(actual_freq)] | |
predicted_freq <- predicted_freq[order(predicted_freq)] |