Skip to content

Commit

Permalink
Merge pull request #21 from powersupplyhq/create_google_display_visua…
Browse files Browse the repository at this point in the history
…lizations

Create google display visualizations
  • Loading branch information
csalvato committed Jun 7, 2016
2 parents 65eed20 + 3d3d6c5 commit 5d1588d
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 57 deletions.
2 changes: 2 additions & 0 deletions AdWordsUtilities/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(clean_raw_campaign_data)
export(clean_raw_keyword_data)
export(click_through_rate_over_time)
export(contribution_per_click_report)
export(desktop_gdn_performance_over_time)
export(desktop_performance_over_time)
export(impression_share_over_time)
export(keyword_performance_data)
Expand All @@ -17,6 +18,7 @@ export(keywords_campaign_performance_over_time)
export(keywords_performance_over_time)
export(keywords_report)
export(keywords_with_earnings)
export(mobile_gdn_performance_over_time)
export(mobile_performance_over_time)
export(overall_performance_over_time)
export(raw_campaign_performance_data)
Expand Down
6 changes: 3 additions & 3 deletions AdWordsUtilities/R/contribution_per_click_report.r
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ contribution_per_click_report <- function(keywords_elog,
}

data <- keywords_elog %>%
group_by(keyword,campaign_name) %>%
group_by(campaign_name,keyword,match_type) %>%
AdWordsUtilities::summarize_elog() %>%
filter(cost > 0 & earnings > 0) %>%
group_by(keyword,campaign_name) %>%
filter(cost > 0 & earnings > 0) %>%
group_by(campaign_name,keyword,match_type) %>%
summarize(total_cost = sum(cost),
total_contribution = sum(contribution),
total_clicks = sum(clicks),
Expand Down
42 changes: 42 additions & 0 deletions AdWordsUtilities/R/desktop_gdn_performance_over_time.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' Create plot of Google Display Network mobile performance over time
#'
#' Takes in a AdWords event log and processes performance on desktop devices over time.
#'
#' @param gdn_elog A data frame containing keywords and transaction event log data.
#' @param campaign_filter Optional. (e.g. "Paleo Performers") If provided, generates a data frame and plot for campaigns where the string is included in the campaign name. Default to NULL.
#' @param plot logical. If TRUE, creates a plot of the data. If false, creates no plot, Defaults to \code{TRUE}.
#' @return The data frame used to create a plot of mobile performance over time.
#' @export
#' @examples
#' overall_performance_over_time(elog_data_frame)

desktop_gdn_performance_over_time <- function( gdn_elog,
campaign_filter=NULL,
plot = TRUE){
require(plyr)
require(dplyr)
require(SalvatoUtilities)
if( !is.null(campaign_filter) ) {
gdn_elog <- gdn_elog %>% filter(grepl(campaign_filter,campaign_name))
}
devices_over_time <- gdn_elog %>%
group_by(device,week) %>%
summarize(cost = sum(cost, na.rm = TRUE),
contribution = sum(money_in_the_bank_paid_to_us,na.rm=TRUE) *.25) %>%
mutate(cum_contribution = cumsum(contribution),
cum_cost = cumsum(cost),
cum_ROI = cum_contribution - cum_cost) %>%
gather(type,value,cum_cost,cum_contribution,cum_ROI)

if( plot ) {
require(ggplot2)
plot(ggplot(devices_over_time %>%
filter(device == "dt"),
aes(week,value,group=type,col=type,fill=type)) +
geom_line() +
ggtitle("GDN Trends on Desktop"))
}

return(devices_over_time)

}
42 changes: 42 additions & 0 deletions AdWordsUtilities/R/mobile_gdn_performance_over_time.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' Create plot of Google Display Network mobile performance over time
#'
#' Takes in a AdWords event log and processes performance on mobile devices over time.
#'
#' @param gdn_elog A data frame containing keywords and transaction event log data.
#' @param campaign_filter Optional. (e.g. "Paleo Performers") If provided, generates a data frame and plot for campaigns where the string is included in the campaign name. Default to NULL.
#' @param plot logical. If TRUE, creates a plot of the data. If false, creates no plot, Defaults to \code{TRUE}.
#' @return The data frame used to create a plot of mobile performance over time.
#' @export
#' @examples
#' overall_performance_over_time(elog_data_frame)

mobile_gdn_performance_over_time <- function( gdn_elog,
campaign_filter=NULL,
plot = TRUE){
require(plyr)
require(dplyr)
require(SalvatoUtilities)
if( !is.null(campaign_filter) ) {
gdn_elog <- gdn_elog %>% filter(grepl(campaign_filter,campaign_name))
}
devices_over_time <- gdn_elog %>%
group_by(device,week) %>%
summarize(cost = sum(cost, na.rm = TRUE),
contribution = sum(money_in_the_bank_paid_to_us,na.rm=TRUE) *.25) %>%
mutate(cum_contribution = cumsum(contribution),
cum_cost = cumsum(cost),
cum_ROI = cum_contribution - cum_cost) %>%
gather(type,value,cum_cost,cum_contribution,cum_ROI)

if( plot ) {
require(ggplot2)
plot(ggplot(devices_over_time %>%
filter(device == "mb"),
aes(week,value,group=type,col=type,fill=type)) +
geom_line() +
ggtitle("GDN Trends on Mobile"))
}

return(devices_over_time)

}
9 changes: 5 additions & 4 deletions AdWordsUtilities/R/summarize_elog.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ summarize_elog <- function(elog_data_frame,
){
require(plyr)
require(dplyr)
return (summarize(elog_data_frame, cost = sum(cost, na.rm = TRUE),
return (summarize(elog_data_frame,
cost = sum(cost, na.rm = TRUE),
average_position = weighted.mean(average_position,impressions, na.rm=TRUE),
average_quality_score=mean(quality_score, na.rm=TRUE),
average_quality_score=ifelse(is.null(elog_data_frame$quality_score), NA, mean(quality_score, na.rm=TRUE)) ,
estimated_available_impressions = sum(impressions/est_search_impression_share, na.rm=TRUE),
impressions = sum(impressions, na.rm = TRUE),
# imp share is not wholly accurate because of the way the numbers are reported, but close enough.
Expand All @@ -35,8 +36,8 @@ summarize_elog <- function(elog_data_frame,
earnings_per_click = earnings/clicks,
contribution_per_click= contribution/clicks,
cpa = ifelse(num_acquisitions==0, cost, cost/num_acquisitions),
referred_users=sum(new_referred_users, na.rm=TRUE),
referred_earnings=sum(referred_users_transaction_amount,na.rm=TRUE),
referred_users=ifelse(is.null(elog_data_frame$new_referred_users), NA, sum(new_referred_users, na.rm=TRUE)),
referred_earnings=ifelse(is.null(elog_data_frame$referred_users_transaction_amount), NA, sum(referred_users_transaction_amount,na.rm=TRUE)),
estimated_ltv = num_acquisitions*average_num_orders_in_lifetime*average_value_per_order*margin,
estimated_lifetime_ROAS=(estimated_ltv-cost)/cost,
total_earnings = earnings + referred_earnings,
Expand Down
26 changes: 26 additions & 0 deletions AdWordsUtilities/man/desktop_gdn_performance_over_time.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions AdWordsUtilities/man/mobile_gdn_performance_over_time.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions BingUtilities/R/contribution_per_click_report.r
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ contribution_per_click_report <- function(keywords_elog,
}

data <- keywords_elog %>%
group_by(keyword,campaign_name) %>%
group_by(keyword,match_type,campaign_name) %>%
BingUtilities::summarize_elog() %>%
filter(cost > 0 & earnings > 0) %>%
group_by(keyword,campaign_name) %>%
filter(cost > 0 & earnings > 0) %>%
group_by(keyword,match_type,campaign_name) %>%
summarize(total_cost = sum(cost),
total_contribution = sum(contribution),
total_clicks = sum(clicks),
Expand Down
1 change: 1 addition & 0 deletions PowerSupplyUtilities/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(clean_transactions_data_for_adwords)
export(clean_transactions_data_for_bing)
export(create_adwords_event_log)
export(create_bing_event_log)
export(create_google_display_event_log)
export(customers_per_month_report)
export(get_first_transaction_dates_for_users)
export(get_referrals_data)
Expand Down
95 changes: 95 additions & 0 deletions PowerSupplyUtilities/R/create_google_display_event_log.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#' Create Google Display Event Log (elog) for AdWords
#'
#'
#' @param from Start date in either format <"yyyy-mm-dd"> or <yyyymmdd>. Inclusive.
#' @param to End date in either format <"yyyy-mm-dd"> or <yyyymmdd>. Inclusive.
#' @return A data frame with all events (AdWords clicks, Transactions and Referrals).
#' @export
#' @examples
#' create_google_display_event_log(from=20150101, to=20151231)

create_google_display_event_log <- function(from=Sys.Date(),
to=Sys.Date()){
require(AdWordsUtilities)
require(PowerSupplyUtilities)
require(SalvatoUtilities)

ppc_events <- all_ppc_raw_completed_order_events( from = from, to = to )
mixpanel_adwords_conversions <- ppc_events[["adwords"]]
mixpanel_adwords_conversions <- clean_completed_order_events(mixpanel_adwords_conversions)

##### Retrieve AdWords Spend/Click Data
adwords_campaigns_data <- AdWordsUtilities::campaign_performance_data(from=as.Date(from), to=as.Date(to))

# Retrieve revenue data
db_transactions <- get_transactions_data(from=from, to=to)
#db_influencer_metrics <- get_referrals_data(from=from, to=to)
db_first_transactions <- get_first_transaction_dates_for_users(from=from, to=to, users=db_transactions$app_user_id)

# Join Mixpanel Conversion Data with transaction data
unique_users <- distinct(mixpanel_adwords_conversions, app_user_id)
db_transactions <- db_transactions %>% inner_join(unique_users, by="app_user_id")

#Filter out people where their first order was not in the specified start date and end date
db_transactions <- db_transactions %>% filter(is.element(app_user_id, db_first_transactions$id))

db_transactions <- clean_transactions_data_for_adwords(db_transactions)

# Add campaign names to db_transactions log
db_transactions <- campaign_lookup_table(from=from, to=to) %>%
right_join(db_transactions, by=c(campaign_id = "campaign_id"))


###################################### CREATE ELOGS ################################################
# Create keywords elog
keywords_elog <- rbind.fill(db_transactions, adwords_campaigns_data)
keywords_elog$week <- as.week(keywords_elog$date)
keywords_elog <- keywords_elog %>% arrange(week)

# Create join table for user_id and the keyword and campaign_name of first purchase
user_first_acquisition_metrics <- keywords_elog %>%
filter(!is.na(user_id)) %>% #Remove NA user_ids (which means they are not monetary transactions)
group_by(user_id) %>%
summarize(keyword = first(keyword),
campaign_name=first(campaign_name),
campaign_id=first(campaign_id),
device=first(device),
match_type=first(match_type))

#Add influencer metrics to the event log
db_influencer_metrics <- get_referrals_data(from=from, to=to)
influencer_metrics_with_user_data <- db_influencer_metrics %>%
rename(week=week_start,
user_id=influencer_id) %>%
mutate(week = as.Date(week, format = '%Y-%m-%d')) %>%
inner_join(user_first_acquisition_metrics, by=c(user_id="user_id")) %>%
filter(week >= from, week <= to)
keywords_elog <- rbind.fill(keywords_elog, influencer_metrics_with_user_data)

keywords_elog <- keywords_elog %>% filter(grepl("Rmktg",campaign_name))

keywords_elog <- keywords_elog %>%
select( -transaction_date,
-event,
-time,
-distinct_id,
-X.created,
-X.email,
-X.first_name,
-X.last_name,
-X.lib_version,
-X.name,
-currency,
-discount,
-mp_country_code,
-mp_lib,
-products,
-tax,
-total,
-lastOrderedFrom,
-utm_content,
-mp_keyword,
-traits)

return(keywords_elog)
}
Loading

0 comments on commit 5d1588d

Please sign in to comment.