feat: replace chb with consent groups and preserve runtime config (api#317, #17)
deploy-preland / deploy zoomstage.online (push) Successful in 42s
deploy-preland / deploy zoomstage.online (push) Successful in 42s
This commit is contained in:
1 parent
188b78a115
commit
89cc16eb4a
12 files changed
+208
-9
No files matched your search
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/** Собственный контракт cons Preland; существование группы проверяет основной API. */
|
||||
declare(strict_types=1);
|
||||
|
||||
/** Переходный runtime-конфиг: новый код работает и до точечного обновления host config. */
|
||||
function prelandNormalizeConsConfig(array $config): array
|
||||
{
|
||||
$config['utm'] = is_array($config['utm'] ?? null) ? $config['utm'] : [];
|
||||
unset($config['utm']['chb']);
|
||||
if (!is_array($config['utm']['cons'] ?? null)) {
|
||||
$config['utm']['cons'] = ['weights' => ['1' => 100, '2' => 0], 'default' => '2'];
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
|
||||
/** Concrete номера не ограничены random-весами: новые группы создаются в CPanel API. */
|
||||
function prelandResolveCons(mixed $value, array $settings): string
|
||||
{
|
||||
$valid = static fn (mixed $candidate): bool => (is_string($candidate) || is_int($candidate))
|
||||
&& preg_match('/^[1-9][0-9]{0,9}$/D', (string) $candidate) === 1
|
||||
&& (float) $candidate <= 2147483647;
|
||||
$default = $valid($settings['default'] ?? null) ? (string) $settings['default'] : '2';
|
||||
if ($valid($value)) return (string) $value;
|
||||
if ($value !== 'rnd') return $default;
|
||||
|
||||
$weights = [];
|
||||
foreach ((array) ($settings['weights'] ?? []) as $number => $weight) {
|
||||
if ($valid($number) && is_numeric($weight) && is_finite((float) $weight) && (float) $weight > 0) {
|
||||
$weights[(string) $number] = (float) $weight;
|
||||
}
|
||||
}
|
||||
$total = array_sum($weights);
|
||||
if ($total <= 0 || !is_finite($total)) return $default;
|
||||
$point = (mt_rand() / (mt_getrandmax() + 1.0)) * $total;
|
||||
foreach ($weights as $number => $weight) {
|
||||
if ($point < $weight) return (string) $number;
|
||||
$point -= $weight;
|
||||
}
|
||||
return (string) array_key_last($weights);
|
||||
}
|
||||
|
||||
/** Legacy chb не должен проходить как неизвестный внешний рекламный параметр. */
|
||||
function prelandNormalizeConsQuery(array $query, array $settings): array
|
||||
{
|
||||
unset($query['chb']);
|
||||
$query['cons'] = prelandResolveCons($query['cons'] ?? null, $settings);
|
||||
return $query;
|
||||
}
|
||||
Reference in new issue
Block a user