This commit is contained in:
2026-03-20 15:10:25 +01:00
parent 8ceba50b32
commit 7d3d513ffb
2 changed files with 158 additions and 2 deletions

View File

@@ -431,8 +431,8 @@
},
".htaccess": {
"type": "-",
"size": 716,
"lmtime": 1772117932512,
"size": 819,
"lmtime": 1773929242197,
"modified": false
},
"index.php": {
@@ -738,6 +738,7 @@
"modified": false
}
},
"raporty": {},
"robots.txt": {
"type": "-",
"size": 25,

155
api.php
View File

@@ -114,6 +114,73 @@ function api_normalize_product_text( $value )
return $value;
}
function api_resolve_client( $mdb, $client_id, $google_ads_id )
{
if ( $client_id > 0 )
{
return $mdb -> query(
'SELECT id, google_ads_customer_id
FROM clients
WHERE id = :client_id
LIMIT 1',
[
':client_id' => (int) $client_id
]
) -> fetch( \PDO::FETCH_ASSOC );
}
if ( $google_ads_id !== '' )
{
$google_ads_id_clean = str_replace( '-', '', $google_ads_id );
return $mdb -> query(
'SELECT id, google_ads_customer_id
FROM clients
WHERE REPLACE( google_ads_customer_id, \'-\', \'\' ) = :google_ads_id
LIMIT 1',
[
':google_ads_id' => $google_ads_id_clean
]
) -> fetch( \PDO::FETCH_ASSOC );
}
return null;
}
function api_resolve_campaign_for_client( $mdb, $client_id, $campaign_db_id, $campaign_external_id )
{
if ( $campaign_db_id > 0 )
{
return $mdb -> query(
'SELECT id, campaign_id, campaign_name
FROM campaigns
WHERE id = :campaign_id
AND client_id = :client_id
LIMIT 1',
[
':campaign_id' => (int) $campaign_db_id,
':client_id' => (int) $client_id
]
) -> fetch( \PDO::FETCH_ASSOC );
}
if ( $campaign_external_id !== '' )
{
return $mdb -> query(
'SELECT id, campaign_id, campaign_name
FROM campaigns
WHERE campaign_id = :campaign_external_id
AND client_id = :client_id
LIMIT 1',
[
':campaign_external_id' => $campaign_external_id,
':client_id' => (int) $client_id
]
) -> fetch( \PDO::FETCH_ASSOC );
}
return null;
}
// dodawanie domeny przez API
if ( \S::get( 'action' ) == 'domain_tester_add' )
{
@@ -677,6 +744,94 @@ if ( \S::get( 'action' ) == 'products_to_optimize' )
] );
}
// Odczyt historii ROAS 30 dni dla kampanii (domyslnie 30 rekordow, opcjonalnie cala historia)
if ( \S::get( 'action' ) == 'campaign_roas_history_get' )
{
api_validate_api_key( $mdb );
$client_id = (int) \S::get( 'client_id' );
$google_ads_id = trim( (string) \S::get( 'google_ads_id' ) );
$campaign_db_id = (int) \S::get( 'campaign_db_id' );
$campaign_external_id = trim( (string) \S::get( 'campaign_id' ) );
$all = (int) \S::get( 'all' ) === 1;
$limit = (int) \S::get( 'limit' );
if ( $client_id <= 0 && $google_ads_id === '' )
{
api_json_response( [ 'result' => 'error', 'message' => 'Missing required param: client_id or google_ads_id' ], 422 );
}
if ( $campaign_db_id <= 0 && $campaign_external_id === '' )
{
api_json_response( [ 'result' => 'error', 'message' => 'Missing required param: campaign_db_id or campaign_id' ], 422 );
}
$client = api_resolve_client( $mdb, $client_id, $google_ads_id );
if ( !$client )
{
api_json_response( [ 'result' => 'error', 'message' => 'Client not found' ], 404 );
}
$campaign = api_resolve_campaign_for_client( $mdb, (int) $client['id'], $campaign_db_id, $campaign_external_id );
if ( !$campaign )
{
api_json_response( [ 'result' => 'error', 'message' => 'Campaign not found for selected client' ], 404 );
}
if ( !$all )
{
if ( $limit <= 0 )
{
$limit = 30;
}
if ( $limit > 3650 )
{
$limit = 3650;
}
}
$history_where = [
'campaign_id' => (int) $campaign['id'],
'ORDER' => [ 'date_add' => 'DESC' ]
];
if ( !$all )
{
$history_where['LIMIT'] = $limit;
}
$history_rows = $mdb -> select(
'campaigns_history',
[ 'date_add', 'roas_30_days' ],
$history_where
);
$history = [];
foreach ( (array) $history_rows as $row )
{
$history[] = [
'date' => (string) ( $row['date_add'] ?? '' ),
'roas_30_days' => (float) ( $row['roas_30_days'] ?? 0 )
];
}
api_json_response( [
'result' => 'ok',
'client_id' => (int) $client['id'],
'google_ads_id' => (string) ( $client['google_ads_customer_id'] ?? '' ),
'campaign_db_id' => (int) $campaign['id'],
'campaign_id' => (string) ( $campaign['campaign_id'] ?? '' ),
'campaign_name' => (string) ( $campaign['campaign_name'] ?? '' ),
'all' => $all ? 1 : 0,
'count' => count( $history ),
'history' => $history
] );
}
// Open Page Rank - zapis
if ( \S::get( 'action' ) == 'domain_opr_save' )
{