Ivy Professional School
Rating
Learning/SQL Data Drill #9

Web Event Analysis

Last updated

Analyze customer web events to understand product brand interest, campaign funnel performance, and user-level engagement behavior.

SQLIntermediateCTEString FunctionsFunnel AnalysisFIRST_VALUE

Your Objective

Analyze customer web events to understand product brand interest, campaign funnel performance, and user-level engagement behavior.

The Setup

Your dataset contains two tables from a web analytics system: 1. A Web_Events table recording each customer interaction — page views, add-to-cart, checkout, purchase, and search events — along with the campaign, session, and a JSON field containing event details 2. A Products table with product_id, product_name, brand, and category

Table Schema

Web_Events
ColumnTypeKey / Notes
event_idINTPK
customer_idVARCHAR(20)
event_dateDATE
event_typeVARCHAR(50)
session_idVARCHAR(50)
utm_campaignVARCHAR(50)
event_jsonTEXT
Products
ColumnTypeKey / Notes
product_idINTPK
product_nameVARCHAR(100)
brandVARCHAR(50)
categoryVARCHAR(50)

Sample Data

Products — 6 rows

product_idproduct_namebrandcategory
101Wireless EarbudsboAtElectronics
102Smart WatchNoiseElectronics
103Running ShoesPumaFootwear
104Denim JacketLevi'sFashion
105BackpackSkybagsAccessories
106Bluetooth SpeakerJBLElectronics

Web_Events — 30 rows

event_idcustomer_idevent_dateevent_typesession_idutm_campaignevent_json
1C0012025-08-02page_viewS001SummerSale{"product_id":101,"page":"product"}
2C0012025-08-02add_to_cartS001SummerSale{"product_id":101,"page":"cart"}
3C0012025-08-02checkoutS001SummerSale{"product_id":101,"page":"checkout"}
4C0012025-08-02purchaseS001SummerSale{"product_id":101,"page":"success"}
5C0022025-08-05page_viewS002SummerSale{"product_id":102,"page":"product"}
6C0022025-08-05add_to_cartS002SummerSale{"product_id":102,"page":"cart"}
7C0022025-08-05checkoutS002SummerSale{"product_id":102,"page":"checkout"}
8C0032025-08-10page_viewS003SummerSale{"product_id":103,"page":"product"}
9C0032025-08-10add_to_cartS003SummerSale{"product_id":103,"page":"cart"}
10C0042025-08-12page_viewS004FestiveOffer{"product_id":104,"page":"product"}
11C0042025-08-12add_to_cartS004FestiveOffer{"product_id":104,"page":"cart"}
12C0042025-08-12checkoutS004FestiveOffer{"product_id":104,"page":"checkout"}
13C0042025-08-12purchaseS004FestiveOffer{"product_id":104,"page":"success"}
14C0052025-08-15page_viewS005FestiveOffer{"product_id":105,"page":"product"}
15C0052025-08-15add_to_cartS005FestiveOffer{"product_id":105,"page":"cart"}
16C0062025-08-18page_viewS006FestiveOffer{"product_id":106,"page":"product"}
17C0062025-08-18checkoutS006FestiveOffer{"product_id":106,"page":"checkout"}
18C0072025-08-20page_viewS007NewLaunch{"product_id":101,"page":"product"}
19C0072025-08-20add_to_cartS007NewLaunch{"product_id":101,"page":"cart"}
20C0072025-08-20purchaseS007NewLaunch{"product_id":101,"page":"success"}
21C0082025-08-22page_viewS008NewLaunch{"product_id":106,"page":"product"}
22C0082025-08-22add_to_cartS008NewLaunch{"product_id":106,"page":"cart"}
23C0082025-08-22checkoutS008NewLaunch{"product_id":106,"page":"checkout"}
24C0092025-08-25searchS009NewLaunch{"keyword":"earbuds"}
25C0012025-08-28page_viewS010SummerSale{"product_id":102,"page":"product"}
26C0012025-08-28add_to_cartS010SummerSale{"product_id":102,"page":"cart"}
27C0022025-08-30purchaseS011SummerSale{"product_id":102,"page":"success"}
28C0032025-09-01page_viewS012FestiveOffer{"product_id":103,"page":"product"}
29C0032025-09-01add_to_cartS012FestiveOffer{"product_id":103,"page":"cart"}
30C0032025-09-01purchaseS012FestiveOffer{"product_id":103,"page":"success"}

Your Task

This drill has three questions: 1. Add to Cart by Brand — Compute total add_to_cart counts per product brand, sorted highest first. Your result table should have: brand, total_add_to_cart. 2. Campaign Funnel — Create a funnel showing page_view → add_to_cart → checkout → purchase conversion rates per campaign (exclude search events). Your result table should have: utm_campaign, event_type, cnt, rate. 3. User Feature Table — Build a per-user feature table using 2025-09-01 as the reference date (30-day window). Your result table should have: customer_id, last_event_date, no_of_sessions, add_to_carts, purchases.

Your output should look like this (partial example):

brandtotal_add_to_cart
Noise2
boAt2
……

Hints

  • →Q1: Use MID() and LOCATE() to extract product_id from event_json. LOCATE(":") finds the colon, MID() pulls the value after it. Filter event_type = "add_to_cart" and exclude blank customer_ids. Join with Products to get brand names.
  • →Q2: Filter out "search" events before grouping — the funnel only tracks page_view, add_to_cart, checkout, and purchase. Use FIRST_VALUE(cnt) OVER (PARTITION BY utm_campaign ORDER BY cnt DESC) to get the top event count per campaign, then divide each stage count by it.
  • →Q3: Write two CTEs. The first calculates 30-day activity per customer using conditional SUM(IF(event_type="add_to_cart",1,0)). The second finds each customer's last event date using FIRST_VALUE(event_date) OVER (PARTITION BY customer_id ORDER BY event_date DESC). LEFT JOIN so customers with no 30-day activity still appear — use IFNULL() to replace NULLs with 0.
Explore Enterprise Solutions