Backend Development 7 min read

Building a Simple Python Tool for Detecting Event Tracking (埋点) in Web Applications

This article demonstrates how to create a Python-based tool that crawls click‑event data from an e‑commerce site, processes and visualizes the information to verify event‑tracking code, covering preparation, data collection, analysis, and visualization steps with complete source code.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Building a Simple Python Tool for Detecting Event Tracking (埋点) in Web Applications

As the internet industry evolves, event tracking (埋点) becomes essential for data analysis and user behavior monitoring, but ensuring its reliability is challenging. This guide shows how to build a simple Python tool to detect and validate such tracking points.

1. Preparation

Install the required packages:

requests   # HTTP requests
pandas     # Data analysis
matplotlib # Data visualization

Install them via:

pip3 install requests pandas matplotlib

Example tracking code used in the demo:

window.sensorsdata_click = function(eventName) {
    console.log('埋点:' + eventName);
}

2. Crawl Data

Construct a URL that returns the latest click data, using a LAST_ID parameter to paginate:

https://www.example.com?last_id=LAST_ID

Python crawler implementation:

import requests
import pandas as pd

data_url = 'https://www.example.com?last_id='
last_id = 0

def get_data():
    global last_id, data_url
    url = data_url + str(last_id)
    response = requests.get(url)
    data = response.json()['data']
    last_id = data[-1]['id']
    return data

The script uses requests for HTTP calls and updates last_id globally to fetch new records each run.

3. Analyze Data

Define constants and data structures, then process the fetched records to count clicks per button:

import matplotlib.pyplot as plt

event_names = ['button1', 'button2', 'button3']
click_count = {name: 0 for name in event_names}

def process_data(data):
    global click_count
    for row in data:
        eventName = row['eventName']
        if eventName in event_names:
            click_count[eventName] += 1
        else:
            print(f"{eventName}没有被定义")

This function aggregates click counts and reports undefined events.

4. Visualize Data

Generate a bar chart showing clicks per button:

def visualize():
    plt.bar(range(len(event_names)), list(click_count.values()), align='center')
    plt.xticks(range(len(event_names)), list(event_names))
    plt.title("Clicks per Button")
    plt.show()

Finally, combine all parts into a runnable script:

import requests
import pandas as pd
import matplotlib.pyplot as plt

data_url = 'https://www.example.com?last_id='
last_id = 0

event_names = ['button1', 'button2', 'button3']
click_count = {name: 0 for name in event_names}

def get_data():
    global last_id, data_url
    url = data_url + str(last_id)
    response = requests.get(url)
    data = response.json()['data']
    last_id = data[-1]['id']
    return data

def process_data(data):
    global click_count
    for row in data:
        eventName = row['eventName']
        if eventName in event_names:
            click_count[eventName] += 1
        else:
            print(f"{eventName}没有被定义")

def visualize():
    plt.bar(range(len(event_names)), list(click_count.values()), align='center')
    plt.xticks(range(len(event_names)), list(event_names))
    plt.title("Clicks per Button")
    plt.show()

if __name__ == '__main__':
    while True:
        data = get_data()
        process_data(data)
        visualize()

Run the script with:

python3 main.py

The tool continuously monitors event‑tracking data, helping developers verify and optimize their tracking implementations for more accurate analytics.

Conclusion

The article presented a complete Python solution for detecting and visualizing click‑event tracking on a simulated e‑commerce site, illustrating how to fetch, process, and display data to ensure tracking code works correctly.

Pythonbackend developmentevent trackingData VisualizationWeb Scraping
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.