base_converion_rate_pd = spark.table("base_conversion_rate").toPandas()
pie, ax = plt.subplots(figsize=[20,9])
labels = base_converion_rate_pd['interaction_type']
plt.pie(x=base_converion_rate_pd['count'], autopct="%.1f%%", explode=[0.05]*2, labels=labels, pctdistance=0.5)
plt.title("Base Conversion Rate");
attribution_by_model_type_pd = spark.table("attribution_by_model_type").toPandas()
pt = sns.catplot(x='channel',y='conversions_attributed',hue='attribution_model',data=attribution_by_model_type_pd, kind='bar', aspect=4, legend=True)
pt.fig.set_figwidth(20)
pt.fig.set_figheight(9)
plt.tick_params(labelsize=15)
plt.ylabel("Number of Conversions")
plt.xlabel("Channels")
plt.title("Channel Performance");
%sql
CREATE OR REPLACE TABLE cpa_summary
USING DELTA
AS
SELECT
spending.channel,
spending.dollar_spend,
attribution_count.attribution_model,
attribution_count.conversions_attributed,
round(spending.dollar_spend / attribution_count.conversions_attributed,2) AS CPA_in_Dollars
FROM
(SELECT explode(channel_spend) AS (channel, spend),
round(total_spend_in_dollars * spend, 2) AS dollar_spend
FROM gold_ad_spend) AS spending
JOIN
(SELECT attribution_model, channel, round(attribution_percent * (
SELECT count(*) FROM gold_user_journey WHERE conversion = 1)) AS conversions_attributed
FROM gold_attribution) AS attribution_count
ON spending.channel = attribution_count.channel;
OK
cpa_summary_pd = spark.table("cpa_summary").toPandas()
pt = sns.catplot(x='channel', y='CPA_in_Dollars',hue='attribution_model',data=cpa_summary_pd, kind='bar', aspect=4, ci=None)
plt.title("Cost of Aquisition by Channel")
pt.fig.set_figwidth(20)
pt.fig.set_figheight(9)
plt.tick_params(labelsize=15)
plt.ylabel("CPA in $")
plt.xlabel("Channels")
plt.title("Channel Cost per Aquisition");
%sql
CREATE OR REPLACE TABLE spend_optimization_view
USING DELTA
AS
SELECT
a.channel,
a.pct_spend,
b.attribution_percent,
b.attribution_percent / a.pct_spend as ROAS,
a.dollar_spend,
round(
(b.attribution_percent / a.pct_spend) * a.dollar_spend,
2
) as proposed_dollar_spend
FROM
exploded_gold_ad_spend a
JOIN gold_attribution b on a.channel = b.channel
and attribution_model = 'markov_chain';
CREATE
OR REPLACE TABLE spend_optimization_final
USING DELTA AS
SELECT
channel,
'current_spending' AS spending,
dollar_spend as budget
FROM exploded_gold_ad_spend
UNION
SELECT
channel,
'proposed_spending' AS spending,
proposed_dollar_spend as budget
FROM
spend_optimization_view;
OK
spend_optimization_final_pd = spark.table("spend_optimization_final").toPandas()
pt = sns.catplot(x='channel', y='budget', hue='spending', data=spend_optimization_final_pd, kind='bar', aspect=4, ci=None)
plt.tick_params(labelsize=15)
pt.fig.set_figwidth(20)
pt.fig.set_figheight(9)
plt.title("Spend Optimization per Channel")
plt.ylabel("Budget in $")
plt.xlabel("Channels")
Out[14]:
Text(0.5, 36.14399999999999, 'Channels')
‹ Back to Table of Contents