Marketing Analysis Recommendations (04) - Govarthan-Boopalan/Customer_Behaviour_Analysis GitHub Wiki

GitHub Wiki: Learning Notes for 04_Marketing_Analysis_recomendation.py

This document provides a detailed breakdown of the 04_Marketing_Analysis_recomendation.py script. The script connects to a MySQL database, extracts marketing insights, generates actionable recommendations, and saves them to a text file.

Table of Contents

  1. Script Overview
  2. Flowchart
  3. Code Explanation
  4. Usage
  5. Improvement Suggestions

Script Overview

The script automates the generation of marketing recommendations by analyzing underperforming products and top-performing marketing channels. It uses MySQL for data retrieval and pandas for data manipulation.

Key Features:

  • MySQL Integration: Connects to a MySQL database to fetch product and marketing data.
  • Dynamic Query Execution: Runs predefined SQL queries to extract insights.
  • Recommendation Engine: Generates human-readable recommendations based on query results.
  • Output Generation: Saves recommendations to a text file.

Flowchart

Start
  │
  ├── Connect to MySQL Database
  │
  ├── Define SQL Queries (Underperforming Products & Best Channels)
  │
  ├── Execute Queries and Fetch Results
  │   ├── Convert Results to DataFrame
  │   └── Generate Recommendations via Row Iteration
  │
  ├── Save Recommendations to Text File
  │
  └── End

Code Explanation

Imports

import mysql.connector  # For MySQL database interaction
import pandas as pd     # For data manipulation
  • mysql.connector: Establishes a connection to the MySQL database and executes SQL queries.
  • pandas: Converts SQL query results into a DataFrame for easier data processing.

MySQL Connection

conn = mysql.connector.connect(
    host="localhost",
    user="root",
    password="456123",
    database="ShopEasy"
)
cursor = conn.cursor()
  • mysql.connector.connect(): Creates a connection to the MySQL database using specified credentials.
  • cursor: A MySQL cursor object to execute SQL queries and fetch results.

SQL Queries

queries = {
    "underperforming_products": """SELECT ProductName, AvgRating, PurchaseCount 
                                   FROM (SELECT p.ProductName, AVG(cr.Rating) AS AvgRating, 
                                                 COUNT(cj.JourneyID) AS PurchaseCount
                                         FROM products p
                                         LEFT JOIN customer_reviews cr ON p.ProductID = cr.ProductID
                                         LEFT JOIN customer_journey cj ON p.ProductID = cj.ProductID AND cj.Action = 'purchase'
                                         GROUP BY p.ProductName
                                         ORDER BY AvgRating ASC, PurchaseCount ASC
                                         LIMIT 5) subquery;""",

    "best_marketing_channels": """SELECT ContentType, AVG(Views) AS AvgViews, AVG(Clicks) AS AvgClicks, COUNT(cj.JourneyID) AS Purchases
                                  FROM engagement_data e
                                  LEFT JOIN customer_journey cj ON e.ProductID = cj.ProductID AND cj.Action = 'purchase'
                                  GROUP BY e.ContentType
                                  ORDER BY Purchases DESC;"""
}
  • Structure: A dictionary stores two SQL queries as strings.
  • Query 1: Fetches the top 5 underperforming products based on low ratings and purchase counts.
    • Uses LEFT JOIN to include products even if they lack reviews or purchases.
    • LIMIT 5 restricts results to the worst-performing products.
  • Query 2: Identifies the best-performing marketing channels by purchases.
    • Groups data by ContentType (e.g., email, social media) and sorts by total purchases.

Recommendation Generation

recommendations = []

for key, query in queries.items():
    cursor.execute(query)
    result = cursor.fetchall()
    df = pd.DataFrame(result, columns=[desc[0] for desc in cursor.description])
    
    if key == "underperforming_products":
        for _, row in df.iterrows():
            recommendations.append(
                f"Consider improving or marketing {row['ProductName']} (Rating: {row['AvgRating']}, Sales: {row['PurchaseCount']})."
            )
    
    elif key == "best_marketing_channels":
        for _, row in df.iterrows():
            recommendations.append(
                f"Boost marketing in {row['ContentType']} campaigns (Avg Views: {row['AvgViews']}, Avg Clicks: {row['AvgClicks']}, Purchases: {row['Purchases']})."
            )
  • cursor.execute(query): Executes the SQL query.
  • cursor.fetchall(): Retrieves all rows from the query result.
  • pd.DataFrame(): Converts the result into a DataFrame with column names from the cursor description.
  • df.iterrows(): Iterates over DataFrame rows to generate recommendations.
    • Uses Python f-strings to dynamically insert data into recommendation messages.

Saving Recommendations

with open("../results/Marketing_recommendations.txt", "w") as file:
    for rec in recommendations:
        file.write(rec + "\n")
  • with open(): Opens the file in write mode ("w").
  • file.write(): Writes each recommendation as a new line in the text file.

Usage

  1. Prerequisites:

    • Install required packages:
      pip install mysql-connector-python pandas
      
    • Ensure the MySQL server is running with the ShopEasy database and correct tables.
  2. Run the Script:

    python 04_Marketing_Analysis_recomendation.py
    
  3. Output:

    • A text file named Marketing_recommendations.txt is generated in the ../results directory.

Improvement Suggestions

  1. Error Handling: Add try...except blocks to handle database connection failures or file write errors.
  2. Parameterization: Use environment variables for database credentials (e.g., host, password).
  3. Dynamic Filters: Allow users to adjust query limits (e.g., LIMIT 5) via command-line arguments.
  4. Connection Cleanup: Explicitly close the cursor and database connection after use:
    cursor.close()
    conn.close()
    

Conclusion

This script provides a streamlined way to extract marketing insights from a MySQL database and generate actionable recommendations. By combining SQL queries with pandas for data manipulation, it automates the analysis of product performance and marketing channel effectiveness. The modular structure allows for easy expansion to include additional queries or recommendation logic.