Page cover

Real-Time User Behavior Analysis

1.1 Oveview

Real-time user behavior analysis enables CapsureLabs to monitor, interpret, and act on user actions across the platform as they occur. This functionality supports personalized experiences, improves security by detecting unusual activities, and allows data-driven insights for platform optimization.


1.2 Real-Time User Behavior Analysis Methods

Collect user actions across the platform, such as clicks, views, and interactions, using event capture and streaming technology.


1.3 Architecture Overview

Application or service that generates user events.


1.4 Data Collection and Processing: Event Producer and Streaming Processor

from kafka import KafkaProducer
import json

producer = KafkaProducer(
    bootstrap_servers='localhost:9092',
    value_serializer=lambda v: json.dumps(v).encode('utf-8')
)

event = {
    "user_id": "123",
    "action": "click",
    "page": "homepage",
    "timestamp": "2024-10-05T12:00:00Z"
}

producer.send('user_behavior_topic', event)
producer.flush()
from pyflink.datastream import StreamExecutionEnvironment
from pyflink.datastream.window import TimeWindow
from pyflink.datastream.functions import MapFunction

env = StreamExecutionEnvironment.get_execution_environment()

class ExtractBehaviorData(MapFunction):
    def map(self, value):
        return json.loads(value)['user_id'], json.loads(value)['action']

data_stream = env.add_source("kafka_source")
processed_stream = data_stream.map(ExtractBehaviorData)

1.4.2 Real-Time Pattern Recognition Model

from sklearn.ensemble import IsolationForest
import numpy as np

X_train = np.array([[10], [15], [20], [15], [50]]) 
model = IsolationForest(contamination=0.1)
model.fit(X_train)

X_new = np.array([[25]])
prediction = model.predict(X_new)  

1.4.3 Model Integration: Real-Time Prediction API

from flask import Flask, request, jsonify
import joblib
import numpy as np

app = Flask(__name__)

model = joblib.load('user_behavior_anomaly_model.pkl')

@app.route('/predict_behavior', methods=['POST'])
def predict_behavior():
    data = request.json
    features = np.array([data['clicks_per_minute'], data['pages_per_session']]).reshape(1, -1)
    
    prediction = model.predict(features)
    result = "Anomaly" if prediction[0] == -1 else "Normal"
    return jsonify({'behavior_prediction': result})

if __name__ == '__main__':
    app.run(debug=True)

1.4.4 cURL Request

curl -X POST -H "Content-Type: application/json" -d '{"clicks_per_minute": 18, "pages_per_session": 5}' http://localhost:5000/predict_behavior

Last updated