-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from powersupplyhq/create_google_display_visua…
…lizations Create google display visualizations
- Loading branch information
Showing
14 changed files
with
340 additions
and
57 deletions.
There are no files selected for viewing
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
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
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
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) | ||
|
||
} |
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
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) | ||
|
||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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) | ||
} |
Oops, something went wrong.