Comprehensive user flow analytics system for tracking user journeys, conversion funnels, and navigation patterns on your website.
User flow analytics provides comprehensive insights into how users navigate through your website, helping identify user journeys, drop-off points, and optimization opportunities.
// Initialize user flow tracking
UserFlowAnalytics.init({
projectId: 'your-project-id',
tracking: {
pageViews: true,
clicks: true,
formSubmissions: true,
customEvents: true,
sessionTracking: {
trackDuration: true,
trackPath: true,
trackReferrer: true
}
},
funnels: {
enabled: true,
autoDetect: true,
customFunnels: []
}
});
// Track user journey steps
UserFlowAnalytics.trackStep('homepage', {
step: 1,
funnel: 'user_onboarding',
timestamp: Date.now(),
source: 'direct',
utmSource: getUTMParameter('utm_source'),
utmMedium: getUTMParameter('utm_medium'),
utmCampaign: getUTMParameter('utm_campaign')
});
UserFlowAnalytics.trackStep('product_page', {
step: 2,
funnel: 'user_onboarding',
productId: 'prod-123',
timestamp: Date.now(),
previousPage: 'homepage',
timeOnPreviousPage: 45000
});
UserFlowAnalytics.trackStep('checkout', {
step: 3,
funnel: 'user_onboarding',
timestamp: Date.now(),
previousPage: 'product_page',
timeOnPreviousPage: 120000
});
// Define standard conversion funnels
UserFlowAnalytics.defineFunnel('user_onboarding', [
{
step: 1,
name: 'Homepage',
url: '/homepage',
required: true
},
{
step: 2,
name: 'Product Page',
url: '/product',
required: true
},
{
step: 3,
name: 'Add to Cart',
event: 'add_to_cart',
required: true
},
{
step: 4,
name: 'Checkout',
url: '/checkout',
required: true
},
{
step: 5,
name: 'Purchase',
event: 'purchase',
required: true
}
]);
UserFlowAnalytics.defineFunnel('lead_generation', [
{
step: 1,
name: 'Landing Page',
url: '/landing',
required: true
},
{
step: 2,
name: 'Form View',
url: '/contact',
required: true
},
{
step: 3,
name: 'Form Start',
event: 'form_start',
required: true
},
{
step: 4,
name: 'Form Submit',
event: 'form_submit',
required: true
}
]);
// Track user paths through the site
class PathTracker {
constructor() {
this.currentPath = [];
this.pathStartTime = Date.now();
this.lastPageVisit = null;
}
trackPageVisit(pageUrl, pageData = {}) {
const visitData = {
url: pageUrl,
timestamp: Date.now(),
timeOnPreviousPage: this.lastPageVisit ?
Date.now() - this.lastPageVisit.timestamp : 0,
referrer: document.referrer,
userAgent: navigator.userAgent,
sessionId: this.getSessionId(),
userId: this.getUserId(),
...pageData
};
this.currentPath.push(visitData);
this.lastPageVisit = visitData;
// Send path data to analytics
UserFlowAnalytics.trackPath(this.currentPath);
}
getPathMetrics() {
return {
pathLength: this.currentPath.length,
totalDuration: Date.now() - this.pathStartTime,
averageTimePerPage: this.calculateAverageTimePerPage(),
bounceRate: this.calculateBounceRate(),
exitPage: this.currentPath[this.currentPath.length - 1]?.url
};
}
}
// Behavioral segmentation implementation
class BehavioralSegmenter {
constructor() {
this.segments = {
'power_users': {
criteria: {
pageViews: { min: 50, weight: 0.3 },
sessionDuration: { min: 300000, weight: 0.4 },
conversionRate: { min: 0.1, weight: 0.3 }
}
},
'casual_visitors': {
criteria: {
pageViews: { max: 10, weight: 0.4 },
sessionDuration: { max: 60000, weight: 0.3 },
returnVisits: { max: 3, weight: 0.3 }
}
},
'potential_converters': {
criteria: {
productViews: { min: 3, weight: 0.4 },
cartAdditions: { min: 1, weight: 0.4 },
timeOnProduct: { min: 30000, weight: 0.2 }
}
}
};
}
segmentUser(userBehavior) {
const scores = {};
for (const [segmentName, segmentConfig] of Object.entries(this.segments)) {
let score = 0;
let totalWeight = 0;
for (const [criterion, config] of Object.entries(segmentConfig.criteria)) {
const userValue = userBehavior[criterion] || 0;
const weight = config.weight;
if (config.min && userValue >= config.min) {
score += weight;
} else if (config.max && userValue <= config.max) {
score += weight;
}
totalWeight += weight;
}
scores[segmentName] = totalWeight > 0 ? score / totalWeight : 0;
}
return Object.entries(scores)
.sort(([,a], [,b]) => b - a)
.map(([segment, score]) => ({ segment, score }));
}
}
# Get user flow analytics
GET /api/v1/user-flows/{project_id}
Parameters:
- start_date: Start date for analytics
- end_date: End date for analytics
- segment: User segment (optional)
- funnel: Funnel name (optional)
Response:
{
"flows": [
{
"path": ["homepage", "product", "checkout", "purchase"],
"user_count": 1250,
"conversion_rate": 15.2,
"average_duration": 450000,
"drop_off_points": ["product"]
}
],
"summary": {
"total_users": 5000,
"average_path_length": 3.2,
"overall_conversion_rate": 12.5
}
}
# Get funnel analytics
GET /api/v1/funnels/{project_id}/{funnel_name}
Parameters:
- start_date: Start date for analytics
- end_date: End date for analytics
- segment: User segment (optional)
Response:
{
"funnel_name": "user_onboarding",
"steps": [
{
"step": 1,
"name": "Homepage",
"users": 5000,
"conversion_rate": 100.0,
"drop_off_rate": 0.0
},
{
"step": 2,
"name": "Product Page",
"users": 3500,
"conversion_rate": 70.0,
"drop_off_rate": 30.0
}
],
"overall_conversion_rate": 15.2
}
# Get user segmentation data
GET /api/v1/segments/{project_id}
Parameters:
- start_date: Start date for analytics
- end_date: End date for analytics
Response:
{
"segments": [
{
"segment_name": "power_users",
"user_count": 500,
"percentage": 10.0,
"conversion_rate": 25.5,
"revenue_contribution": 45.2
},
{
"segment_name": "casual_visitors",
"user_count": 3500,
"percentage": 70.0,
"conversion_rate": 5.2,
"revenue_contribution": 25.8
}
],
"total_users": 5000
}