-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgaForecastConversionToSlack.R
More file actions
53 lines (40 loc) · 1.48 KB
/
gaForecastConversionToSlack.R
File metadata and controls
53 lines (40 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# For more information about Slackr: https://github.com/hrbrmstr/slackr
# Fredrik Cederlöf, Head of CX & Analytics Collector Bank
# https://www.linkedin.com/in/fredrikcederlof/
library(googleAnalyticsR)
library(forecast)
library(slackr)
library(cronR)
#devtools::install_github("hrbrmstr/slackr")
# check if file exists
# file.exists("~/.slackr")
# remove global variables
# rm(list = ls())
# call setup slack from Renviron file .slackr
slackrSetup()
# google analytics authentication
ga_auth()
my_id <- 11111111
# declare time intervall
OneYearAgo <- Sys.Date() - 365
Yesterday <- Sys.Date() - 1
df <- google_analytics(my_id,
date_range = c(OneYearAgo, Yesterday),
metrics = c("goal9Completions", "users"),
dimensions = c("date"),
anti_sample = TRUE,
max = -1)
# calculate forecast
conversions <- ts(df, start=c(2018,1), end=c(2018,12),frequency=365)
conversionsForecast <- forecast(auto.arima(df$goal6Completions))
# create new data frame based on calculated forecast
df2 = data.frame(conversionsForecast)
df2 <- round(df2, 1)
# calculate weekly mean forecast values and round zero decimals
WeeklyForecast <- sum(head(df2$Point.Forecast,7))
WeeklyForecast <- round(WeeklyForecast, 0)
# prepare message before pusing to Slack
message <- paste("Forecasted conversions for this week is", WeeklyForecast, ":thumbsup:.", sep=" ")
text_slackr(preformatted = FALSE, message)
# send simple message to Slack
#slackr(message)