diff --git a/PowerSupplyUtilities/NAMESPACE b/PowerSupplyUtilities/NAMESPACE index 9da813d..1f68fe1 100644 --- a/PowerSupplyUtilities/NAMESPACE +++ b/PowerSupplyUtilities/NAMESPACE @@ -1,3 +1,4 @@ # Generated by roxygen2: do not edit by hand +export(get_referrals_data) export(get_transactions_data) diff --git a/PowerSupplyUtilities/R/get_influencer_data.r b/PowerSupplyUtilities/R/get_referrals_data.r similarity index 95% rename from PowerSupplyUtilities/R/get_influencer_data.r rename to PowerSupplyUtilities/R/get_referrals_data.r index ef3a0ba..1004c8b 100644 --- a/PowerSupplyUtilities/R/get_influencer_data.r +++ b/PowerSupplyUtilities/R/get_referrals_data.r @@ -16,7 +16,7 @@ get_referrals_data <- function(from=Sys.Date(), to=Sys.Date(), database_driver="database_drivers/postgresql-9.4.1208.jre6.jar", - jdbc_config_file="jdbc_data_warehouse_database_config.txt", + jdbc_config_file="jdbc_datawarehouse_string.txt", transactions_query_file="referrals_query.sql"){ if(file.exists(database_driver)) { require(RJDBC) @@ -28,8 +28,10 @@ get_referrals_data <- function(from=Sys.Date(), require(SalvatoUtilities) referrals_query <- string_from_file(transactions_query_file) referrals_data <- dbGetQuery(db, referrals_query) + dbDisconnect(db) return(referrals_data) } else { + dbDisconnect(db) stop("Can't find referrals_query.sql (or the file provided) in your working directory.\n\tDownload the file from the git repo (https://github.com/powersupplyhq/adwords-analysis), put it in your working directory and try again.") } } else { diff --git a/PowerSupplyUtilities/R/get_transactions_data.r b/PowerSupplyUtilities/R/get_transactions_data.r index 4aa1578..20386f6 100644 --- a/PowerSupplyUtilities/R/get_transactions_data.r +++ b/PowerSupplyUtilities/R/get_transactions_data.r @@ -28,8 +28,10 @@ get_transactions_data <- function(from=Sys.Date(), require(SalvatoUtilities) transactions_query <- string_from_file(transactions_query_file) transactions <- dbGetQuery(db, transactions_query) + dbDisconnect(db) return(transactions) } else { + dbDisconnect(db) stop("Can't find transactions_query.sql (or the file provided) in your working directory.\n\tDownload the file from the git repo (https://github.com/powersupplyhq/adwords-analysis), put it in your working directory and try again.") } } else { diff --git a/PowerSupplyUtilities/man/get_referrals_data.Rd b/PowerSupplyUtilities/man/get_referrals_data.Rd new file mode 100644 index 0000000..8fc2e2a --- /dev/null +++ b/PowerSupplyUtilities/man/get_referrals_data.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_referrals_data.r +\name{get_referrals_data} +\alias{get_referrals_data} +\title{Get Referrals Data} +\usage{ +get_referrals_data(from = Sys.Date(), to = Sys.Date(), + database_driver = "database_drivers/postgresql-9.4.1208.jre6.jar", + jdbc_config_file = "jdbc_datawarehouse_string.txt", + transactions_query_file = "referrals_query.sql") +} +\arguments{ +\item{from}{Start date in either format <"yyyy-mm-dd"> or . Inclusive.} + +\item{to}{End date in either format <"yyyy-mm-dd"> or . Inclusive.} + +\item{database_driver}{Path to the database driver for the database being used. Defaults to "database_drivers/postgresql-9.4.1208.jre6.jar".} + +\item{jdbc_config_file}{Path to a text file with the URI to connect to the database, including credentials. Defaults to "jdbc_data_warehouse_database_config.txt"} + +\item{transactions_query_file}{Path to a .sql file that includes the SQL query to be used to retrieve the necessary data from the database. Defaults to "referrals_query.sql"} +} +\value{ +A data frame with transaction data within the date range. +} +\description{ +Returns a data frame with information about the number of referees (people who purchased from a referral) a user has accumulated within +within a date range. Data is pulled from cached data warehouse that is updated nightly. +} +\examples{ +get_transactions_data(from=20150101, to=20151231) +} + diff --git a/PowerSupplyUtilities/man/get_transactions_data.Rd b/PowerSupplyUtilities/man/get_transactions_data.Rd index 271346b..90dad29 100644 --- a/PowerSupplyUtilities/man/get_transactions_data.Rd +++ b/PowerSupplyUtilities/man/get_transactions_data.Rd @@ -13,6 +13,12 @@ get_transactions_data(from = Sys.Date(), to = Sys.Date(), \item{from}{Start date in either format <"yyyy-mm-dd"> or . Inclusive.} \item{to}{End date in either format <"yyyy-mm-dd"> or . Inclusive.} + +\item{database_driver}{Path to the database driver for the database being used. Defaults to "database_drivers/postgresql-9.4.1208.jre6.jar".} + +\item{jdbc_config_file}{Path to a text file with the URI to connect to the database, including credentials. Defaults to "jdbc_transactions_database_config.txt"} + +\item{transactions_query_file}{Path to a .sql file that includes the SQL query to be used to retrieve the necessary data from the database. Defaults to "transactions_query.sql"} } \value{ A data frame with transaction data within the date range. diff --git a/adwords.r b/adwords.r index dd77ff5..2bddc88 100644 --- a/adwords.r +++ b/adwords.r @@ -48,12 +48,8 @@ adwords_keywords_data <- keyword_performance_data(from=as.Date(start_date), to=a adwords_campaigns_data <- campaign_performance_data(from=as.Date(start_date), to=as.Date(end_date)) # Retrieve revenue data -pgsql <- JDBC("org.postgresql.Driver", "database_drivers/postgresql-9.4.1208.jre6.jar", "`") -datawarehouse_db <- dbConnect(pgsql, string_from_file("jdbc_datawarehouse_string.txt")) -influencer_metrics_query <- string_from_file("influencer_metrics_query.sql") -db_influencer_metrics <- dbGetQuery(datawarehouse_db, influencer_metrics_query) - db_transactions <- get_transactions_data(from=start_date, to=end_date) +db_influencer_metrics_new <- get_referrals_data(from=start_date, to=end_date) # Join Mixpanel Conversion Data with Data Warehouse transaction data mixpanel_adwords_conversions <- data.frame(mixpanel_adwords_conversions) @@ -81,9 +77,6 @@ db_first_transactions <- dbGetQuery(heroku_db, GetoptLong::qq(paste("SELECT WHERE first_transaction between '@{start_date}' and '@{end_date}'"))) -dbDisconnect(datawarehouse_db) -dbDisconnect(heroku_db) - #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)) diff --git a/influencer_metrics_query.sql b/referrals_query.sql similarity index 100% rename from influencer_metrics_query.sql rename to referrals_query.sql