Load pacakges for data cleaning, relating, and visualizing
library(tidyverse)
library(completejourney)
library(ggplot2)
library(treemap)
library(sunburstR)
Load data frames for later analysis.
transactions_df <- get_transactions()
promotions_df <- get_promotions()
campaigns_df <- campaigns
campaign_descriptions_df <- campaign_descriptions
coupons_df <- coupons
coupon_redemptions_df <- coupon_redemptions
demographics_df <- demographics
products_df <- products
Find demographics of campaign 27 coupon redeemers
#demographics of customers who redeemed a campaign 27 coupon
campaign_27_redemptions <- coupon_redemptions_df %>%
filter(campaign_id == 27) %>%
inner_join(demographics_df, by = "household_id") %>%
group_by(household_size,income,.add = TRUE) %>%
summarize(count = n())
Generate treemap plot to visualize campaign 27 redeemer demographics by income range and household size
Find product categories commonly purchased with a Campaign 27 coupon.
#find products mostly commonly purchased from a campaign 27 coupon
campaign_27_products_categories <- coupon_redemptions_df %>%
filter(campaign_id == 27) %>%
inner_join(coupons_df,by = "coupon_upc") %>%
inner_join(products_df,by = "product_id") %>%
filter(campaign_id.y == 27) %>%
select(coupon_upc,product_category) %>%
group_by(product_category) %>%
summarize(count = n()) %>%
arrange(desc(count))
Generate sunburst plot to visualize product categories purchased with a Campaign 27 coupon.
campaign_27_products_sunburst <- campaign_27_products_categories %>%
sunburst(
width="100%",
height=400,
legend = TRUE,
labs(title = "Campaign 27: Redeemed Coupons Product Categories")
)
Redeemed Campaign 27 Coupons Product Category Breakdown
campaign_27_products_sunburst
#To view product categories, hover over a slice of the plot above.
As we can see above, one of the top two product categories was the ICE CREAM/MILK/SHERBTS category.We will now aim to analyze how frequently the one particular household targeted by Campaign 27 traditionally purchase these products. We randomly selected household 936 to analyze their ice cream purchasing behavior.
Code to determine whether each purchase of household 936 included ice cream in their basket or not.
household_936_transactions_sample <- transactions_df %>%
filter(household_id == 936) %>%
inner_join(products_df, by = "product_id") %>%
mutate(is_ice_cream_purchase = ifelse(product_category == "ICE CREAM/MILK/SHERBTS",1,0)) %>%
mutate(date_transaction = date(transaction_timestamp)) %>%
group_by(basket_id,date_transaction,.add = TRUE) %>%
summarize(basket_contains_ice_cream = any(is_ice_cream_purchase == 1))
We now need code to find the start and end date of Campaign 27.
campaign_27_details <- campaign_descriptions_df %>%
filter(campaign_id == 27)
campaign_27_start_date <- campaign_27_details$start_date
campaign_27_end_date <- campaign_27_details$end_date
#list for use in generating purchase analysis time markers for Campaign 27
campaign_date_list <- c(campaign_27_start_date,campaign_27_end_date)
The code below generates a plot showing various baskets of household 936 throughout the year. The blue markers indicate the start and end dates for Campaign 27
household_936_icecream_purchase_tracking_plot <- household_936_transactions_sample %>%
ggplot( aes(x = date_transaction, fill = as.factor(basket_contains_ice_cream))) +
geom_bar() +
labs(x = "Date", y = "Number of Baskets", fill = "Contains Ice Cream") +
scale_fill_manual(values = c("grey", "green"), labels = c("No", "Yes")) +
theme_minimal() +
geom_vline(xintercept = as.numeric(as.Date(campaign_date_list)), linetype = "dashed", color = "blue", linewidth = 1) +
labs(
title = "Household 936 ICE CREAM/MILK/SHERBTS Product Purchasing Behavior",
subtitle = "-No purchase in Campaign 27 period-"
)
household_936_icecream_purchase_tracking_plot