如何使用 AdBlock 动态域 API
AdBlock 工具日益复杂,给寻求最大化在线广告收入的出版商带来了巨大挑战。为帮助减轻这些挑战,该 API 允许出版商根据活跃域名动态调整脚本,从而大大降低内容被 AdBlock 软件阻止的风险。
通过定期轮换域名确保广告投放,出版商可以
- Maintain Revenue Streams: 防止被屏蔽的广告造成的中断。
- 增强用户体验: 在不触发 AdBlock 检测的情况下无缝交付广告内容。
- 自动域管理: 通过自动 7 天域生命周期消除手动更新。
新域名每 3 天生成一次,可通过应用程序接口使用。域名的生命周期为 6 天。
- 3 天有效
- 3 天废弃
一旦生命周期到期,域名将被停用。出于性能考虑,建议在从 API 请求新域之前将域缓存 24 小时。
此 API 仅适用于 Popunders inline-script 和 In-Stream Video 区域。
要求
要使用该应用程序接口,出版商需要.NET Framework 3.0:
- 向端点发出 GET 请求: https://ads.exoclick.com/adblock-domains.php
- 您将在回复中收到一个新的广告服务域名
- 然后,您必须将广告区标签上使用的聚合端点更改为 API 响应中提供的新端点。
实施步骤
步骤 1.获取活动域
要动态检索活动域,必须进行服务器到服务器的 API 调用。这一点至关重要,因为从客户端调用 API 调用可能会被阻止。
服务器到服务器请求(PHP 示例)
<?php
function fetchAdblockDomain()
{
$endpoint = 'https://ads.exoclick.com/adblock-domains.php'; // your ads domain
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('Error cURL: ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200) {
return $response;
} else {
throw new Exception("HTTP Error: {$httpCode} - Response: {$response}");
}
}
try {
echo fetchAdblockDomain();
} catch (Exception $e) {
echo json_encode(['message' => $e->getMessage()]);
}
?>
应用程序接口的预期响应
{
"success": true,
"message": "Domain found",
"domain": "newsub.newdomain.com" // example
}
步骤 2.更新脚本
- 获取 Zone HTML 标记上提供的脚本
- 使用端点返回的域
- 对于流视频区,更改VAST 标记:
- 之前 :
https://s.magsrv.com/v1/vast.php?idzone={zoneId}
- After :
https://newsub.newdomain.com/v1/vast.php?idzone={zoneId}
- 之前 :
- 对于 Popunders 区域,更改内嵌脚本上的 syndication_host:
- 之前 : s.pemsrv.com
- 之后 : newsub.newdomain.com
检测广告拦截器
通过以下代码片段,您可以检测客户端是否启用了 AdBlock。这样,您就可以为请求选择适当的域,并附加 block=1 以改进 AdBlock 跟踪统计。请参阅下面的示例:
// This script will be blocked by Adblock in case of the client is using it, due to the presence of the "exoclick" and "ads" keywords.
// After loading this script, a JS snipppet will attempt to read a specific variable.
// If the variable is undefined or inaccessible, we can determine that the script as been blocked.
<script src="https://www.exoclick.com/ads.js" type="text/javascript"></script>
<script>
// Initialize the insDomain variable with a default value
let insDomain = 's.magsrv.com'; // Generic host for the network
let adb = ""; // This variable may contain "&block=1" which helps track Adblock Statistics
// Fetch adblock domains using a GET request to the PHP file
async function fetchAdblockDomains() {
try {
// Perform a GET request to fetchAdblockDomain.php (Step 3.A).
// This is a server-to-server request to retreive the Adblock Domain.
const response = await fetch('fetchAdblockDomain.php', {
method: 'GET', // Specify the GET method
});
// Check if the response is successful (status 200)
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// Parse the response as JSON
const data = await response.json();
console.log('Response data:', data); // Log the response data for debugging
// Call the function to process the domains
displayDomains(data);
} catch (error) {
// Log any errors that occur during the fetch
console.error('Error fetching domains:', error);
}
}
// Process and display the domains, updating insDomain if success is true
function displayDomains(domains) {
console.log('Success status:', domains["success"]); // Log the success status
console.log('Fetched domain:', domains["domain"]); // Log the fetched domain
// Update the insDomain variable if the success flag is true
if (domains["success"] && typeof bait_b3j4hu231 === 'undefined') {
adb = '&block=1';
insDomain = domains["domain"];
console.log('Updated insDomain:', insDomain); // Log the updated value of insDomain
}
// Now you should initialize Fluid Player using the vast tag:
// "https://" + insDomain + "v1/vast.php?idzone=ZONEID" + adb
}
// Automatically call fetchAdblockDomains when the page loads
window.onload = fetchAdblockDomains;
</script>
集成实例
第 1 步 - 服务器端 PHP 文件(fetchAdblockDomain.php)
在服务器上创建名为 fetchAdblockDomain.php
的文件:
<?php
function fetchAdblockDomain()
{
$endpoint = 'https://ads.exoclick.com/adblock-domains.php';
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('Error cURL: ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
return $response;
} else {
throw new Exception("HTTP Error: ({$httpCode}) Response: {$response}");
}
}
try {
echo fetchAdblockDomain();
} catch (Exception $e) {
echo json_encode([
'success' => false,
'message' => $e->getMessage()
]);
}
?>
此 PHP 脚本会获取一个新的域名用于广告投放。
第 2 步 - 获取和应用 JavaScript 逻辑
在 "script"部分:
<script src="https://www.exoclick.com/ads.js" type="text/javascript"></script>
<script>
let insDomain = 's.magsrv.com'; // default
let adb = '';
let rdm = '';
const ZONE_ID_PRE_ROLL = 'XXXXXXX';
async function fetchAndApplyAdblockDomains() {
try {
const response = await fetch('fetchAdblockDomain.php');
const data = await response.json();
console.log('Response:', data);
if (data.success && typeof bait_b3j4hu231 === 'undefined') {
// AdBlock detected
adb = '&block=1';
insDomain = data.domain;
console.log('AdBlock detected: using API domain', insDomain);
} else if (data.success) {
// No AdBlock
insDomain = 's.magsrv.com';
console.log('No AdBlock: using default domain');
} else {
console.warn('Fallback: Using default domain due to error or no success');
}
} catch (err) {
console.error('Error fetching domain:', err);
// Fallback to default domain
}
// Initialize Fluid Player
fluidPlayer('example-player', {
layoutControls: {
primaryColor: "#28B8ED",
controlForwardBackward: {
show: true,
doubleTapMobile: false
},
timelinePreview: {
file: 'thumbnails.vtt',
type: 'VTT'
}
},
vastOptions: {
adList: [
{
roll: "preRoll",
vastTag: `https://${insDomain}/v1/vast.php?${rdm}idzone=${ZONE_ID_PRE_ROLL}${adb}`,
timer: 5
}
]
}
});
}
window.onload = fetchAndApplyAdblockDomains;
</script>
故障排除
常见问题
例如,没有返回域名:
{
"success": false,
"message": "Domain not found"
}
如果成功为假,请联系支持部门。
在调试过程中始终记录端点的响应,以发现潜在问题。
For further assistance, contact the support team here.