Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_improvado_cross_channel_data_model_snowflake_notebook #117

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Improvado Cross-Channel data model Snowflake Notebooks

## Overview
This Salesforce notebook demonstrates a cross-channel data model that consolidates key marketing metrics across primary data sources, campaigns, ad sets, ads, and creatives. The notebook includes a simple visualization to help illustrate the data relationships and insights.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"metadata": {
"kernelspec": {
"display_name": "Streamlit Notebook",
"name": "streamlit"
}
},
"nbformat_minor": 5,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"id": "1ae26c7c-c3cc-4df5-9018-7b53f32242e0",
"metadata": {
"name": "cell7",
"collapsed": false
},
"source": "# Improvado Cross-Channel Model overview and visualization "
},
{
"cell_type": "code",
"id": "3775908f-ca36-4846-8f38-5adca39217f2",
"metadata": {
"language": "python",
"name": "cell1",
"collapsed": false
},
"source": "# Import python packages\nimport streamlit as st\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport matplotlib.dates as mdates\n\n# We can also use Snowpark for our analyses!\nfrom snowflake.snowpark.context import get_active_session\nsession = get_active_session()\n",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"id": "8d50cbf4-0c8d-4950-86cb-114990437ac9",
"metadata": {
"language": "sql",
"name": "cell2",
"collapsed": false
},
"source": "-- Write SQL query to make simple marketing performance report\n\nSELECT date,\n datasource,\n campaign_type,\n SUM(clicks) as clicks,\n SUM(impressions) as impressions,\n SUM(spend) as spend,\n SUM(revenue) as revenue,\n CASE\n WHEN SUM(clicks) = 0.0 THEN 0.0\n ELSE SUM(spend)/SUM(clicks)\n END as CPC,\n CASE\n WHEN AVG(impressions) = 0.0 THEN 0.0 \n ELSE AVG(clicks)/AVG(impressions)\n END as CTR,\n CASE\n WHEN SUM(SPEND) = 0.0 THEN 0.0\n ELSE SUM(Revenue)/SUM(SPEND)*100\n END as ROAS\nFROM TESTDB.TESTSCHEMA.CROSS_CHANNEL_MODEL_FOR_SNOWFLAKE_AI\nGROUP BY date,\n datasource,\n campaign_type",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"id": "c695373e-ac74-4b62-a1f1-08206cbd5c81",
"metadata": {
"language": "python",
"name": "cell3",
"codeCollapsed": false,
"collapsed": false
},
"source": "# Create dataframe from SQL result\ncross_channel_df = cell2.to_pandas()\n\n# Ensure the DATE column is in datetime format\ncross_channel_df['DATE'] = pd.to_datetime(cross_channel_df['DATE'])\n\n# Create a new column for the week\ncross_channel_df['WEEK'] = cross_channel_df['DATE'].dt.to_period('W').apply(lambda r: r.start_time)\n\n# Set the style for the plots\nsns.set(style=\"whitegrid\")\n\n# Define the size of each plot (square)\nplot_size = 5",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"id": "837652e1-c186-4a53-bc12-86b22326e56c",
"metadata": {
"language": "python",
"name": "cell4",
"collapsed": false
},
"outputs": [],
"source": "# Create a figure and axis for the plot\nfig, ax = plt.subplots(figsize=(plot_size, plot_size))\n\n# Plot Spend by datasource and week\nsns.lineplot(data=cross_channel_df, x='WEEK', y='SPEND', hue='DATASOURCE', marker='o', ax=ax)\nax.set_title('Spend by Datasource and Week', fontsize=12)\nax.set_ylabel('Spend', fontsize=10)\nax.set_xlabel('Month', fontsize=10)\n\n# Format the x-axis to show month labels\nax.xaxis.set_major_locator(mdates.MonthLocator())\nax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))\nplt.xticks(rotation=45, fontsize=8)\nplt.yticks(fontsize=8)\n\n# Adjust legend font size\nax.legend(fontsize=8)\n\n# Adjust layout to prevent overlap\nplt.tight_layout()\n\n# Show the plot\nplt.show()",
"execution_count": null
},
{
"cell_type": "code",
"id": "1b076288-d652-4911-98e7-bb3a3fae1c60",
"metadata": {
"language": "python",
"name": "cell5",
"collapsed": false
},
"outputs": [],
"source": "# Create a figure and axis for the plot\nfig, ax = plt.subplots(figsize=(plot_size, plot_size))\n\n# Plot Revenue by datasource and week\nsns.lineplot(data=cross_channel_df, x='WEEK', y='REVENUE', hue='DATASOURCE', marker='o', ax=ax)\nax.set_title('Revenue by Datasource and Week', fontsize=12)\nax.set_ylabel('Revenue', fontsize=10)\nax.set_xlabel('Month', fontsize=10)\n\n# Format the x-axis to show month labels\nax.xaxis.set_major_locator(mdates.MonthLocator())\nax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))\nplt.xticks(rotation=45, fontsize=8)\nplt.yticks(fontsize=8)\n\n# Adjust legend font size\nax.legend(fontsize=8)\n\n# Adjust layout to prevent overlap\nplt.tight_layout()\n\n# Show the plot\nplt.show()",
"execution_count": null
},
{
"cell_type": "code",
"id": "142d59e6-65a4-4afc-bdd5-c606403c72bc",
"metadata": {
"language": "python",
"name": "cell6",
"collapsed": false
},
"outputs": [],
"source": "# Create a figure and axis for the plot\nfig, ax = plt.subplots(figsize=(plot_size, plot_size))\n\n# Plot ROAS by datasource and week\nsns.lineplot(data=cross_channel_df, x='WEEK', y='ROAS', hue='DATASOURCE', marker='o', ax=ax)\nax.set_title('ROAS by Datasource and Week', fontsize=12)\nax.set_ylabel('ROAS', fontsize=10)\nax.set_xlabel('Month', fontsize=10)\n\n# Format the x-axis to show month labels\nax.xaxis.set_major_locator(mdates.MonthLocator())\nax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))\nplt.xticks(rotation=45, fontsize=8)\nplt.yticks(fontsize=8)\n\n# Adjust legend font size\nax.legend(fontsize=8)\n\n# Adjust layout to prevent overlap\nplt.tight_layout()\n\n# Show the plot\nplt.show()",
"execution_count": null
}
]
}