Files
wevads-platform/scripts/fix-templates-tracking.php
2026-02-26 04:53:11 +01:00

113 lines
3.6 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* FIX TEMPLATES - Add tracking placeholders
* Replaces direct sponsor links with [url] placeholder
* Adds [open] pixel and [unsub] link if missing
*/
$dbHost = 'localhost';
$dbName = 'adx_system';
$dbUser = 'admin';
$dbPass = 'admin123';
try {
$pdo = new PDO("pgsql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("DB Error: " . $e->getMessage() . "\n");
}
echo "=== FIX TEMPLATES TRACKING ===\n\n";
// Get all creatives with content
$creatives = $pdo->query("
SELECT c.id, c.offer_id, c.name, c.value, o.name as offer_name
FROM affiliate.creatives c
LEFT JOIN affiliate.offers o ON o.id = c.offer_id
WHERE c.value IS NOT NULL AND c.value != '' AND LENGTH(c.value) > 0
")->fetchAll(PDO::FETCH_ASSOC);
echo "Found " . count($creatives) . " templates to check\n\n";
$fixed = 0;
$skipped = 0;
foreach ($creatives as $c) {
$html = base64_decode($c['value']);
$modified = false;
$changes = [];
echo "Template #{$c['id']} - {$c['name']}:\n";
// 1. Replace direct sponsor links with [url]
// Pattern: rivoweb.com, e36lbat.com, eflow links
$patterns = [
'/(href=["\'])https?:\/\/[^"\']*rivoweb\.com[^"\']*(["\'])/i',
'/(href=["\'])https?:\/\/[^"\']*e36lbat\.com[^"\']*(["\'])/i',
'/(href=["\'])https?:\/\/[^"\']*eflow[^"\']*(["\'])/i'
];
foreach ($patterns as $pattern) {
if (preg_match($pattern, $html)) {
$html = preg_replace($pattern, '$1[url]$2', $html);
$modified = true;
$changes[] = " ✅ Replaced sponsor link with [url]";
}
}
// 2. Add [open] pixel if missing (before </body>)
if (strpos($html, '[open]') === false) {
// Add tracking pixel before </body>
$pixel = '<img src="[open]" width="1" height="1" style="display:none;" alt="" />';
if (stripos($html, '</body>') !== false) {
$html = str_ireplace('</body>', $pixel . "\n</body>", $html);
$modified = true;
$changes[] = " ✅ Added [open] pixel";
}
} else {
$changes[] = " [open] already present";
}
// 3. Add [unsub] link if missing
if (strpos($html, '[unsub]') === false &&
strpos($html, '[unsubscribe]') === false &&
strpos($html, '[optout]') === false) {
// Add unsubscribe link before </body>
$unsub = '<div style="text-align:center; margin-top:20px; padding:10px; font-size:11px; color:#666;">
<a href="[unsub]" style="color:#888; text-decoration:underline;">Unsubscribe</a> |
<a href="[unsub]" style="color:#888; text-decoration:underline;">Abmelden</a>
</div>';
if (stripos($html, '</body>') !== false) {
$html = str_ireplace('</body>', $unsub . "\n</body>", $html);
$modified = true;
$changes[] = " ✅ Added [unsub] link";
}
} else {
$changes[] = " unsub link already present";
}
// Save if modified
if ($modified) {
$newValue = base64_encode($html);
$stmt = $pdo->prepare("UPDATE affiliate.creatives SET value = ? WHERE id = ?");
$stmt->execute([$newValue, $c['id']]);
$fixed++;
echo " 🔧 FIXED!\n";
} else {
$skipped++;
echo " ⏭️ No changes needed\n";
}
foreach ($changes as $change) {
echo $change . "\n";
}
echo "\n";
}
echo "=== SUMMARY ===\n";
echo "Fixed: $fixed templates\n";
echo "Skipped: $skipped templates\n";
echo "Total: " . count($creatives) . " templates\n";