import pandas as pd
import numpy as np
# Define channels with their multipliers, group, spending ranges, and tactics for paid channels
channels = {
"Google Ads": {
"multiplier": 2.0,
"group": "Paid",
"spend_range": (5000, 12000),
"tactics": [
"Search - Brand", "Search - Non-Brand", "Search - Competitor",
"Display - Retargeting", "Display - Prospecting", "Display - Contextual",
"Performance Max", "YouTube - In-Stream", "YouTube - Discovery",
"Shopping - Standard", "Shopping - Smart", "App Campaign - Installs",
"App Campaign - Engagement", "Demand Gen", "Local Campaign"
]
},
"TV Ads": {
"multiplier": 1.5,
"group": "Paid",
"spend_range": (5000, 12000),
"tactics": [
"National Broadcast", "Local Broadcast", "Cable - General",
"Cable - Sports Package", "Connected TV (CTV) - Programmatic",
"Streaming Service - Hulu", "Streaming Service - Peacock",
"Streaming Service - Other", "Addressable TV", "Syndication",
"Direct Response TV (DRTV)", "Infomercial"
]
},
"TikTok Ads": {
"multiplier": 1.8,
"group": "Paid",
"spend_range": (5000, 12000),
"tactics": [
"In-Feed Ads - Auction", "In-Feed Ads - Reach & Frequency",
"TopView Ads", "Brand Takeover", "Branded Hashtag Challenge",
"Branded Effects", "Spark Ads - Influencer Content", "Spark Ads - Own Content",
"Video Shopping Ads", "Collection Ads", "Lead Generation Ads",
"Community Interaction Ads"
]
},
"Meta Ads": {
"multiplier": 1.6,
"group": "Paid",
"spend_range": (5000, 12000),
"tactics": [
"Facebook Feed - Prospecting", "Facebook Feed - Retargeting",
"Instagram Feed - Prospecting", "Instagram Feed - Retargeting",
"Instagram Stories - Prospecting", "Instagram Stories - Retargeting",
"Facebook Marketplace", "Audience Network", "Messenger Ads",
"Reels Ads - Prospecting", "Reels Ads - Retargeting",
"Lead Ads", "Conversion Ads", "Traffic Ads", "Advantage+ Shopping"
]
},
"X Ads": { # Formerly Twitter Ads
"multiplier": 1.4,
"group": "Paid",
"spend_range": (5000, 12000),
"tactics": [
"Promoted Ads - Awareness", "Promoted Ads - Consideration", "Promoted Ads - Conversion",
"Follower Ads", "X Amplify Pre-roll", "X Amplify Sponsorships",
"Trend Takeover", "Trend Takeover+", "Promoted Live Video",
"Branded Notifications", "Branded Hashtags", "Website Traffic Campaign",
"App Install Campaign"
]
},
"Instagram Ads": { # Note: Often managed via Meta, but kept separate here as requested
"multiplier": 1.7,
"group": "Paid",
"spend_range": (5000, 12000),
"tactics": [
"Feed - Single Image/Video", "Feed - Carousel", "Stories - Full Screen",
"Stories - Image/Video", "Reels - Vertical Video", "Explore Ads",
"Shop Ads - Product Tags", "Shop Ads - Collection",
"Direct Message Ads", "AR Filter Ads", "Influencer Collab Posts (Boosted)",
"User-Generated Content Ads", "Event Response Ads"
]
},
"YouTube Reels": { # Note: YouTube Shorts is the more common term, but using the request's term
"multiplier": 1.9,
"group": "Paid",
"spend_range": (5000, 12000),
"tactics": [
"Shorts Feed - Awareness", "Shorts Feed - Consideration", "Shorts Feed - Action",
"Video Action Campaign (Shorts Placement)", "App Campaign (Shorts Placement)",
"Performance Max (Shorts Placement)", "Demand Gen (Shorts Placement)",
"Brand Lift Study (Shorts)", "Reach Campaign (Shorts)",
"Product Feed Integration (Shorts)", "Sequential Storytelling (Shorts)"
]
},
"Affiliate Marketing": {
"multiplier": 3.0,
"group": "Paid",
"spend_range": (5000, 12000),
"tactics": [
"Influencer Networks", "Coupon/Deal Sites", "Review/Content Sites",
"Loyalty/Cashback Programs", "Comparison Shopping Engines",
"Email List Affiliates", "Search Affiliates (PPC)", "Social Media Affiliates",
"Affiliate Networks (Commission Junction, Rakuten)", "Direct Partnerships",
"Sub-Affiliate Networks", "Mobile App Affiliates"
]
},
"Email Marketing": {
"multiplier": 4.0,
"group": "Organic",
"spend_range": (2000, 6000),
# No specific tactics needed for Organic in this context
},
"SEO": {
"multiplier": 5.0,
"group": "Organic",
"spend_range": (2000, 6000),
# No specific tactics needed for Organic in this context
},
}
# Create a date range for 200 weeks
start_date = "2022-01-03" # starting on a Monday
weeks = pd.date_range(start=start_date, periods=200, freq="W-MON")
# Set a seed for reproducibility
np.random.seed(42)
# Prepare a list for spending data (one row per channel per week)
spending_rows = []
# For each week, generate spending for each channel.
# Use channel-specific spending ranges and assign tactics for Paid channels.
for date in weeks:
for channel, info in channels.items():
low, high = info["spend_range"]
# With 80% probability, generate a spend; otherwise 0.
if np.random.rand() < 0.8:
spend = np.random.uniform(low, high)
else:
spend = 0.0
# Assign tactic only if the channel is 'Paid' and has tactics defined
channel_tactic = None # Default to None (or could use 'N/A')
if info["group"] == "Paid" and "tactics" in info and info["tactics"]:
channel_tactic = np.random.choice(info["tactics"])
spending_rows.append(
{
"date": date.strftime("%Y-%m-%d"),
"channel": channel,
"channel_group": info["group"],
"channel_level_tactic": channel_tactic, # Add the new column here
"geo": "US",
"spend": round(spend, 2),
}
)
# Create the spending DataFrame
spend_df = pd.DataFrame(spending_rows)
# Reorder columns for better readability (optional)
spend_df = spend_df[['date', 'geo', 'channel_group', 'channel', 'channel_level_tactic', 'spend']]
# --- Revenue Generation (No changes needed here) ---
# Now, generate the revenue data.
# For each week, revenue = baseline + sum(spend * multiplier) + noise
# NOTE: Revenue is still calculated at the CHANNEL level, not tactic level.
baseline = 100000 # baseline revenue per week
revenue_rows = []
# Group spending data by week (date)
# We group the original spend_df by date to get total spend per channel per week
for date, group in spend_df.groupby("date"):
contribution = 0
# Aggregate spend per channel first for the revenue calculation
channel_spends = group.groupby("channel")["spend"].sum()
for channel, total_channel_spend in channel_spends.items():
multiplier = channels[channel]["multiplier"]
contribution += total_channel_spend * multiplier
# Add a baseline and a small noise (±2000)
noise = np.random.uniform(-2000, 2000)
revenue = baseline + contribution + noise
revenue_rows.append({"date": date, "geo": "US", "revenue": round(revenue, 2)})
# Create the revenue DataFrame
revenue_df = pd.DataFrame(revenue_rows)
# Filter dates (if needed, same as before)
revenue_df = revenue_df[revenue_df["date"] < "2025-12-31"] # Adjusted filter date to include more generated data
spend_df = spend_df[spend_df["date"] < "2025-12-31"] # Adjusted filter date
# Save the dataframes to CSV files
spend_df.to_csv("spending_data_with_tactics.csv", index=False)
revenue_df.to_csv("revenue_data.csv", index=False) # Revenue file name remains the same
print("Updated Spending data shape:", spend_df.shape)
print("Revenue data shape:", revenue_df.shape)
print(spend_df.head())
print("\nSample of tactics for a Paid channel:")
print(spend_df[spend_df['channel'] == 'Google Ads']['channel_level_tactic'].value_counts().head())
print("\nSample of tactics for an Organic channel:")
print(spend_df[spend_df['channel'] == 'SEO']['channel_level_tactic'].value_counts(dropna=False)) # use dropna=False to see None values
print("\nFiles generated: spending_data_with_tactics.csv, revenue_data.csv")