Skip to content

Instantly share code, notes, and snippets.

@jrsouth
Created February 23, 2024 17:15
Show Gist options
  • Save jrsouth/4f43c46fb95fdc8efa53e6a47d974120 to your computer and use it in GitHub Desktop.
Save jrsouth/4f43c46fb95fdc8efa53e6a47d974120 to your computer and use it in GitHub Desktop.
Full-text search of all Dotdigital campaign content
<?php
// @TODO: Take from CLI?
$searchString = 'sideBoxMiddleAd';
$verbose = false; // Bit of extra output, mostly so you can see progress
$apiRegion = 'r1';
$apiUser = 'apiuser-[API_USER_ID]0@apiconnector.com';
$apiPassword = '[API_USER_PASSWORD]';
// ----------------------------------------------------------
// -- Config above, code and processing below, don't touch --
// ----------------------------------------------------------
// Setup
$basicAuthString = base64_encode($apiUser . ':' . $apiPassword);
$baseApiUrl = 'https://' . $apiRegion . '-api.dotdigital.com/v2/';
$requestHeaders = [
'accept: application/json',
'authorization: Basic ' . $basicAuthString,
'content-type: application/json',
];
// Fetch all campaigns
echo "⭐⭐⭐ Fetching the campaign list from Dotdigital ⭐⭐⭐\n\n";
$ch = curl_init($baseApiUrl . 'campaigns');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
$response = curl_exec($ch);
$responseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
// @TODO: Paginate, make sure we've got 'em all.
// (For now issue a warning if we've got exactly
// 1000, which suggests we've been truncated.)
if ($responseCode != '200') {
echo "\n⛔ Unable to collect campaigns. Please check your credentials.\n\n";
die('Error fetching campaigns');
}
$allCampaigns = json_decode($response, TRUE);
// Test with a subset
$allCampaigns = array_slice($allCampaigns, 0, 5);
echo "✅ Successfully fetched " . count($allCampaigns) . " campaigns\n";
if (count($allCampaigns) == 1000) {
echo "\n\n⚠️ That's a very suspicious number of campaigns, and the fetching logic probably needs to be rewritten to handle pagination.\n";
}
// Let's go
echo "\n\n⭐⭐⭐ Searching the source HTML of each campaign ⭐⭐⭐\n\n";
echo "Searching for:\n----------\n{$searchString}\n----------\n\n";
$error = FALSE;
$matchingCampaigns = 0;
foreach ($allCampaigns as $campaign) {
if ($verbose) {
echo "Checking campaign " . $campaign['id'] . " [" . $campaign['name'] . "]\n";
}
// Get the full campaign data
$ch = curl_init($baseApiUrl . 'campaigns/' . $campaign['id'] . '/with-details');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
$response = curl_exec($ch);
$responseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($responseCode == '200') {
$campaignData = json_decode($response, TRUE);
if (str_contains($campaignData['htmlContent'], $searchString)) {
$matchingCampaigns++;
echo "✅ Campaign {$campaignData['id']} contains the search string\n";
echo " Campaign name: {$campaignData['name']}\n";
echo " Subject line: {$campaignData['subject']}\n";
echo " From: {$campaignData['fromName']} <{$campaignData['fromAddress']['email']}>\n";
echo " Tag(s): " . (count($campaignData['tags']) ? implode(", ", array_map(fn ($tag) => $tag['name'], $campaignData['tags'])) : '<none>') . "\n";
echo " Status: {$campaignData['status']}\n";
if ($campaignData['status'] == "Unsent") {
echo " Edit at: https://r1-app.dotdigital.com/Campaigns/Step/EasyEditor.aspx?id={$campaignData['id']}\n";
} else {
echo " Sent: {$campaignData['sentDate']}\n";
}
echo "\n";
}
} else {
$error = TRUE;
echo "⛔ Campaign data could not be retrieved for campaign {$campaign['id']}. HTTP response code was $responseCode and the response content was:\n$response\n\n";
}
}
// Done, report
echo "\n\n⭐⭐⭐ Finished ⭐⭐⭐";
if ($matchingCampaigns) {
echo "\n\n✅ The search string was found within {$matchingCampaigns} campaign" . ($matchingCampaigns == 1 ? '' : 's') . ", please check the details above\n";
} else {
echo "\n\n⛔ The search string was not found\n";
}
if ($error) {
echo "\n\n⛔ Error(s) were detected while searching, please check the detail above\n";
}
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment