forked from imabutahersiddik/CodeStore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics_system.php
More file actions
69 lines (61 loc) · 2.2 KB
/
analytics_system.php
File metadata and controls
69 lines (61 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
class AnalyticsSystem {
private $pdo;
private $currentTime = '2025-06-14 13:33:08';
private $currentUser = 'imabutahersiddik';
public function __construct($pdo) {
$this->pdo = $pdo;
}
public function recordPageView($page, $params = []) {
$stmt = $this->pdo->prepare("
INSERT INTO page_views
(page, params, user_id, ip_address, user_agent, created_at)
VALUES (?, ?, ?, ?, ?, ?)
");
return $stmt->execute([
$page,
json_encode($params),
isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null,
$_SERVER['REMOTE_ADDR'],
$_SERVER['HTTP_USER_AGENT'],
$this->currentTime
]);
}
public function getAppAnalytics($appId, $period = 'last_30_days') {
$dateRange = match($period) {
'today' => "DATE(created_at) = CURDATE()",
'last_7_days' => "created_at >= DATE_SUB(?, INTERVAL 7 DAY)",
'last_30_days' => "created_at >= DATE_SUB(?, INTERVAL 30 DAY)",
'all_time' => "1=1"
};
$sql = "
SELECT
DATE(created_at) as date,
COUNT(*) as views,
COUNT(DISTINCT user_id) as unique_visitors,
COUNT(DISTINCT CASE WHEN event_type = 'download' THEN id END) as downloads
FROM app_events
WHERE app_id = ? AND $dateRange
GROUP BY DATE(created_at)
ORDER BY date DESC
";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([$appId, $this->currentTime]);
return $stmt->fetchAll();
}
public function getUserBehavior($userId) {
$sql = "
SELECT
user_actions.action_type,
COUNT(*) as count,
AVG(TIMESTAMPDIFF(SECOND, session_start, session_end)) as avg_duration
FROM user_sessions
JOIN user_actions ON user_sessions.id = user_actions.session_id
WHERE user_sessions.user_id = ?
GROUP BY user_actions.action_type
";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([$userId]);
return $stmt->fetchAll();
}
}