Date run: 2016-10-16
This dashboard presents summary statistics for 12 K&I websites which account for the majority of web hits for K&I products. The sites are:
(Note: PHOF, Healthier lives and Tobacco profiles are built on the Fingertips platform)
The dashboard shows:
In the summary statistics tab
In the heat maps tab
---
title: "K&I webstats"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
source_code: embed
---
```{r setup, include=FALSE, cache=TRUE}
library(flexdashboard); require(lubridate)
```
About this site
===================================================================
`r paste("Date run:", lubridate::today())`
This dashboard presents summary statistics for 12 K&I websites which account for the majority of web hits for K&I products.
The sites are:
- [Fingertips](http://fingertips.phe.phe.org.uk)
- [PHOF](http://www.phoutcomes.info)
- [NOO](http://www.noo.org.uk)
- [CHimat](http://www.chimat.org.uk)
- [Local health](http://www.localhealth.org.uk)
- [Mental health intelligence network ](http://www.yhpho.org.uk/default.aspx?RID=198138)
- [YHPHO](http://www.yhpho.org.uk)
- [Healthier lives](http://healthierlives.phe.org.uk)
- [End of life intelligence network](http://www.endoflifecare-intelligence.org.uk/home)
- [IHAL](https://www.improvinghealthandlives.org.uk)
- [SHAPE](https://shape.phe.org.uk)
- [Tobacco profiles](http://www.tobaccoprofiles.info)
(Note: PHOF, Healthier lives and Tobacco profiles are built on the Fingertips platform)
The dashboard shows:
*In the summary statistics tab *
1. Hits for yesterday, 30 days ago, 365 days ago, and % change from this day last year for each site
2. Hits for the last month and the last year for each site
3. Global hits for all sites combined
*In the heat maps tab*
1. Daily hits since April 2013 represented as a 'calendar' heatmap
Summary statistics
===================================================================
```{r}
library(dplyr) # data manipulation
library(data.table) # fast file download and combination
library(lubridate) # date manipulation
library(tidyr) # reshaping
require(knitr) # table and html
library(RGA) # access to Google analystics API
library(gridExtra) # chart layout
library(ggplot2) # charting
library(viridis)
library(ggfortify) # charting extras
library(ggTimeSeries) # charting extras
```
```{r}
## authorise access to google API
ga_token <- authorize(client.id = "934830359575-tbflfk33ce99kgatrte3ecpt4gj5kdhf.apps.googleusercontent.com", client.secret = "l5BP84XI1I5d0qrC3f21s9Gp", cache = TRUE)
## extract profile ids, website urls and site names
lookup <- list_profiles()
lookup1 <- as.data.frame(lookup[c(3,23,42,45,46,63, 112,122,137,133,149,150),c(8,5,1)])
## extract data and create data table
ids <- as.character(lookup1[,"id"])
df <- data.frame()
for(i in 1:length(ids)) {
id<-ids[i]
first <- firstdate(id)
ga <- get_ga(id, start.date = first, end.date = "today",
metrics = "ga:users,
ga:sessions,
ga:avgTimeonPage,
ga:pageviews",
dimension = "ga:date" )
ga <- cbind(id, ga)
df <- rbind(df, ga)
}
df <- as.data.table(df) ## convert to data.table format
df$id <- as.character(df$id) ## convert id to character
lookup1 <- as.data.table(lookup1) ## convert lookup to data.table format for linkage
## set keys
setkey(df, id)
setkey(lookup1, id)
## add names and urls to data frame
df1 <- df[lookup1]
## convert and shorten date
df1$date <-ymd(substring(df1$date, 1, 10))
df1 <- df1 %>% unite("webname", name:websiteUrl, sep = "_")
df1 <- df1 %>% mutate(dwell.time = pageviews * avgTimeonPage / 86400)
```
```{r}
## Extract specific date values
df2 <- df1 %>% group_by(webname) %>%
filter(date == lubridate::today() - 1 | date == lubridate::today() - 30|date == lubridate::today() - 365 ) %>%
select(webname, date, pageviews) %>%
spread(date, pageviews)
names(df2)[2:4] <- c("Last year", "30 days ago", "Yesterday" )
df3 <- df2 %>%
mutate(`Annual % change`= paste(round(100 *(Yesterday - `Last year`)/`Last year`,2), "%"))
## and sum over time
df4 <- df1 %>% group_by(webname) %>%
filter(date >=lubridate::today() - 30) %>%
summarise(monthtotal = sum(pageviews))
df5 <- df1 %>% group_by(webname) %>%
filter(date >=lubridate::today() - 365) %>%
summarise(yeartotal = sum(pageviews))
## and for total hits
totpv <- df1 %>% group_by(date) %>% summarise(pageviews = sum(pageviews))
# totpv1 <- totpv %>%
# filter(date == lubridate::today() - 1 | date == lubridate::today() - 30|date == lubridate::today() - 364 ) %>%
# select(date, pageviews) %>%
# spread(date, pageviews)
#
# names(totpv1)[1:3] <- c("this day last year", "30 days ago", "yesterday" )
#
# totpv2 <- totpv1 %>%
# mutate(`Annual % change `= paste(round(100 *(yesterday - `this day last year`)/`this day last year`,2), "%"))
#
# totpv3 <- totpv %>%
# filter(date >=lubridate::today() - 30) %>%
# summarise(monthtotal = sum(pageviews))
#
#
# totpv4 <- totpv %>%
# filter(date >=lubridate::today() - 365) %>%
# summarise(yeartotal = sum(pageviews))
#
# totsum <- cbind(totpv2,totpv3, totpv4 )
dfsum <- merge(df3, df4, by = 'webname')
dfsum <- merge(dfsum, df5, by = 'webname')
dfsum <- dfsum %>% arrange(desc(yeartotal))
# dfsum <- rbind(dfsum, totsum)
rownames(dfsum) <- c("NOO", "Fingertips", "CHimat", "Localhealth", "PHOF", "IHAL", "YHPHO", "Mental health", "End of Life", "Healthier Lives", "SHAPE", "Tobacco")
dfsum <- dfsum %>% select(-webname)
```
Row
-------------------------------------------------------------------
### Total pageviews yesterday
```{r}
yesterday <- colSums(dfsum[, -4])[[3]]
valueBox(yesterday, "Yesterday hits", icon = "fa-thumbs-up")
```
### Total pageviews in last month
```{r}
lastmonth <- colSums(dfsum[, -4])[[4]]
valueBox(lastmonth, "Last month hits ", icon = "fa-thumbs-up")
```
### Total pageviews in last year
```{r}
lastyear <- colSums(dfsum[, -4])[[5]]
prevyear <- totpv %>%
filter(date <=lubridate::today() - 365 & date > lubridate::today() - 730) %>%
summarise(prevyeartotal = sum(pageviews))
valueBox(lastyear, "Last year hits", icon = "fa-line-chart",
color = ifelse(lastyear < prevyear, "warning", "primary"))
```
### Summary table
```{r}
DT::datatable(dfsum)
df1 <- select(df1, date, users, sessions, pageviews, avgTimeonPage, dwell.time, webname) ## select relevant fields
```
Heatmaps
====================================================================
Row {.tabset}
-----------------------------------------------------------------------
### Total
```{r}
totpv <- df1 %>% group_by(date) %>% summarise(Pageviews = sum(pageviews))
gtot <- ggplot_calendar_heatmap(filter(totpv,date >= "2013-04-01"), "date", "Pageviews")
gtot <-gtot + scale_fill_viridis(option = "D") + facet_wrap(~Year, ncol = 1)
gtot + ggtitle("Total pageviews of K&I web products")
```
### Chimat
```{r}
gch <- ggplot_calendar_heatmap(filter(df1,date >= "2013-04-01" & webname == "www.chimat.org.uk_http://www.chimat.org.uk"), "date", "pageviews")
gch <-gch + scale_fill_viridis(option = "D") + facet_wrap(~Year, ncol = 1)
gch + ggtitle("Total pageviews: Chimat")
```
### PHOF
```{r}
gch <- ggplot_calendar_heatmap(filter(df1,date >= "2013-04-01" & webname == "PHOF_http://www.phoutcomes.info"), "date", "pageviews")
gch <-gch + scale_fill_viridis(option = "D") + facet_wrap(~Year, ncol = 1)
gch + ggtitle("Total pageviews: PHOF")
```
### Fingertips
```{r}
gft <- ggplot_calendar_heatmap(filter(df1,date >= "2013-04-01" & webname == "Fingertips site data_http://phe.org.uk"), "date", "pageviews")
gft <-gft + scale_fill_viridis(option = "D") + facet_wrap(~Year, ncol = 1)
gft <- gft + ggtitle("Total pageviews: Fingertips")
gft
```
### NOO
```{r}
gnoo <- ggplot_calendar_heatmap(filter(df1,date >= "2013-04-01" & webname == "www.noo.org.uk/_http://www.noo.org.uk/"), "date", "pageviews")
gnoo <-gnoo + scale_fill_viridis(option = "D") + facet_wrap(~Year, ncol = 1)
gnoo + ggtitle("Total pageviews: NOO")
if(!require(plotly))install.packages("plotly")
library(plotly)
```
### Local health
```{r}
gnoo <- ggplot_calendar_heatmap(filter(df1,date >= "2013-04-01" & webname == "www.localhealth.org.uk_http://www.localhealth.org.uk"), "date", "pageviews")
gnoo <-gnoo + scale_fill_viridis(option = "D") + facet_wrap(~Year, ncol = 1)
gnoo + ggtitle("Total pageviews: Local health")
if(!require(plotly))install.packages("plotly")
library(plotly)
```