run:R W Run
DIR
2025-05-14 17:31:16
R W Run
DIR
2026-02-22 14:28:22
R W Run
4.34 KB
2026-04-08 19:25:21
R W Run
562 By
2026-04-08 19:25:22
R W Run
1.43 KB
2026-04-08 19:25:21
R W Run
2.6 KB
2026-04-08 19:25:22
R W Run
446 By
2026-04-08 19:25:21
R W Run
1.5 KB
2026-04-08 19:25:22
R W Run
1.03 KB
2026-04-08 19:25:21
R W Run
5.83 KB
2026-04-08 19:25:22
R W Run
4.83 KB
2026-04-08 19:25:21
R W Run
6.43 KB
2026-04-08 19:25:21
R W Run
2.58 KB
2026-04-08 19:25:22
R W Run
564 By
2026-04-08 19:25:21
R W Run
316 By
2026-04-08 19:25:21
R W Run
27.32 KB
2026-04-08 19:25:21
R W Run
27.81 KB
2026-04-08 19:25:20
R W Run
2.06 KB
2026-04-08 19:25:19
R W Run
484 By
2026-04-08 19:25:22
R W Run
1.75 KB
2026-04-08 19:25:22
R W Run
4.41 KB
2026-04-08 19:25:22
R W Run
2.55 KB
2026-04-08 19:25:22
R W Run
3.33 KB
2026-04-08 19:25:22
R W Run
762 By
2026-04-08 19:25:20
R W Run
5.03 KB
2026-04-08 19:25:20
R W Run
75 By
2026-04-08 19:25:21
R W Run
2.1 KB
2026-04-08 19:25:23
R W Run
1.17 KB
2026-04-08 19:25:22
R W Run
382 By
2026-04-08 19:25:22
R W Run
1.97 KB
2026-04-08 19:25:22
R W Run
1.4 KB
2026-04-08 19:25:20
R W Run
2.11 KB
2026-04-08 19:25:20
R W Run
1.24 KB
2026-04-08 19:25:23
R W Run
2.93 KB
2026-04-08 19:25:21
R W Run
error_log
📄gclid_email.php
1<?php
2use WHMCS\Database\Capsule;
3
4add_hook('AfterModuleCreate', 1, function ($vars) {
5
6 // -------------------------------------------------------------------------
7 // CONFIGURE YOUR FREE TRIAL PRODUCT IDs HERE
8 // -------------------------------------------------------------------------
9 $trialProductIds = [11];
10 // -------------------------------------------------------------------------
11
12 $serviceId = (int)($vars['serviceid'] ?? ($vars['params']['serviceid'] ?? 0));
13 if ($serviceId <= 0) {
14 logActivity("GCLID Hook: Failed to find serviceid in hook vars.");
15 return;
16 }
17
18 $serviceData = Capsule::table('tblhosting')->where('id', $serviceId)->first();
19 if (!$serviceData) {
20 logActivity("GCLID Hook: No record found in tblhosting for Service ID {$serviceId}");
21 return;
22 }
23
24 $clientId = (int)$serviceData->userid;
25 $packageId = (int)$serviceData->packageid;
26 $orderId = (int)$serviceData->orderid;
27
28 $productNameMap = [
29 11 => 'Trial',
30 1 => '1 Month',
31 6 => '1 Month',
32 2 => '3 Months',
33 7 => '3 Months',
34 4 => '6 Months',
35 8 => '6 Months',
36 5 => '12 Months',
37 10 => '12 Months',
38 ];
39 $productName = $productNameMap[$packageId] ?? (string) Capsule::table('tblproducts')->where('id', $packageId)->value('name');
40
41 // 1. DIRECT DB CHECK: See if this Service ID is tied to an Affiliate
42 $isAffiliate = Capsule::table('tblaffiliatesaccounts')
43 ->where('relid', $serviceId)
44 ->exists();
45
46 // 2. FETCH GCLID (Field 34)
47 $gclidValue = (string) Capsule::table('tblcustomfieldsvalues')
48 ->where('fieldid', 34)
49 ->where('relid', $clientId)
50 ->value('value');
51
52 // Fallback for GCLID if DB is empty
53 if ($gclidValue === '') {
54 if (!empty($_SESSION['gclid'])) {
55 $gclidValue = (string) $_SESSION['gclid'];
56 } elseif (!empty($_COOKIE['gclid'])) {
57 $gclidValue = (string) $_COOKIE['gclid'];
58 }
59 }
60
61 // 3. TRIAL / UPGRADE DETECTION
62 $currentProductIsTrial = in_array($packageId, $trialProductIds);
63
64 $isTrialUpgrade = false;
65 if (!$currentProductIsTrial) {
66 // Check if this client has prior services that were ONLY free trials
67 $clientServices = array_map('intval', Capsule::table('tblhosting')
68 ->where('userid', $clientId)
69 ->where('id', '!=', $serviceId)
70 ->pluck('packageid')
71 ->toArray());
72
73 if (!empty($clientServices)) {
74 $hadTrial = count(array_intersect($clientServices, $trialProductIds)) > 0;
75 $hadNonTrial = count(array_diff($clientServices, $trialProductIds)) > 0;
76
77 // Only flag as upgrade if they had a trial and NO other paid products before
78 $isTrialUpgrade = $hadTrial && !$hadNonTrial;
79 }
80 }
81
82 // 4. SUBJECT LOGIC
83 if ($isAffiliate) {
84 $subjectStatus = 'Affiliate';
85 } else {
86 $subjectStatus = ($gclidValue !== '') ? 'GCLID Found' : 'No GCLID';
87 }
88
89 if ($isTrialUpgrade) {
90 $subjectStatus .= ' - Upgrade';
91 }
92
93 //Step 5 Counter
94 $todayStart = date('Y-m-d') . ' 00:00:00';
95 $todayEnd = date('Y-m-d') . ' 23:59:59';
96 $dailyCount = (int) Capsule::table('tblhosting')
97 ->whereBetween('regdate', [$todayStart, $todayEnd])
98 ->count();
99
100 // 5. BODY LOGIC
101 $upgradeNote = $isTrialUpgrade
102 ? "<strong>Trial Conversion:</strong> Client previously had a free trial and has now upgraded.<br><br>"
103 : "";
104
105 $results = localAPI('SendAdminEmail', [
106 'customsubject' => "#{$dailyCount} - {$subjectStatus} - {$productName}",
107 'custommessage' => "<strong>Client ID:</strong> {$clientId}<br>" .
108 "<strong>Product:</strong> {$productName}<br>" .
109 "<strong>Order ID:</strong> {$orderId}<br><br>" .
110 "New order successfully setup for service.<br><br>" .
111 $upgradeNote .
112 ($gclidValue !== '' ? "<strong>Marketing GCLID:</strong><br>{$gclidValue}" : ""),
113 'type' => 'system',
114 ]);
115
116 if (($results['result'] ?? '') === 'success') {
117 logActivity("GCLID Hook: Admin alert sent for Client {$clientId} (Service {$serviceId}, Order {$orderId})" . ($isTrialUpgrade ? " [Trial Upgrade]" : ""));
118 } else {
119 logActivity("GCLID Hook API ERROR (Service {$serviceId}): " . json_encode($results));
120 }
121});
122