Add CLI script to fetch active Meta Ads insights for campaigns, adsets, and ads

- Implemented a new PHP script to retrieve insights for the last N days (default 30).
- Supports command-line options for token, account ID, days, API version, and output file.
- Fetches data at campaign, adset, and ad levels, with filtering for active statuses.
- Handles JSON output and optional file saving, including directory creation if necessary.
- Includes error handling for cURL requests and JSON responses.
This commit is contained in:
2026-02-20 23:45:36 +01:00
parent 167ced3573
commit b54a9a71b1
34 changed files with 8516 additions and 1676 deletions

View File

@@ -14,6 +14,7 @@
<th>Nazwa klienta</th>
<th style="width: 120px;">Status</th>
<th>Google Ads Customer ID</th>
<th>Facebook Ads Account ID</th>
<th>Merchant Account ID</th>
<th>Dane od</th>
<th style="width: 190px;">Sync</th>
@@ -41,6 +42,13 @@
<span class="text-muted">— brak —</span>
<?php endif; ?>
</td>
<td>
<?php if ( !empty( $client['facebook_ads_account_id'] ) ): ?>
<span class="badge-id"><?= htmlspecialchars( $client['facebook_ads_account_id'] ); ?></span>
<?php else: ?>
<span class="text-muted">— brak —</span>
<?php endif; ?>
</td>
<td>
<?php if ( !empty( $client['google_merchant_account_id'] ) ): ?>
<span class="badge-id"><?= htmlspecialchars( $client['google_merchant_account_id'] ); ?></span>
@@ -73,6 +81,11 @@
</button>
<?php endif; ?>
<?php endif; ?>
<?php if ( !empty( $client['facebook_ads_account_id'] ) ): ?>
<button type="button" class="btn-icon btn-icon-sync client-sync-action" onclick="syncClient(<?= $client['id']; ?>, 'facebook_ads', this)" title="Odswiez Facebook Ads" <?= $is_client_active ? '' : 'disabled'; ?>>
<i class="fa-brands fa-facebook-f"></i>
</button>
<?php endif; ?>
<button type="button" class="btn-icon btn-icon-edit" onclick="editClient(<?= $client['id']; ?>)" title="Edytuj">
<i class="fa-solid fa-pen"></i>
</button>
@@ -84,7 +97,7 @@
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="8" class="empty-state">
<td colspan="9" class="empty-state">
<i class="fa-solid fa-building"></i>
<p>Brak klientów. Dodaj pierwszego klienta.</p>
</td>
@@ -121,6 +134,11 @@
<label for="client-gads-id">Google Ads Customer ID</label>
<input type="text" id="client-gads-id" name="google_ads_customer_id" class="form-control" placeholder="np. 123-456-7890 (opcjonalnie)" />
</div>
<div class="settings-field">
<label for="client-fbads-id">Facebook Ads Account ID</label>
<input type="text" id="client-fbads-id" name="facebook_ads_account_id" class="form-control" placeholder="np. act_123456789012345 (opcjonalnie)" />
<small class="text-muted">Mozesz podac act_... albo same cyfry</small>
</div>
<div class="settings-field">
<label for="client-gmc-id">Merchant Account ID</label>
<input type="text" id="client-gmc-id" name="google_merchant_account_id" class="form-control" placeholder="np. 123456789 (opcjonalnie)" />
@@ -148,6 +166,7 @@ function openClientForm()
$( '#client-name' ).val( '' );
$( '#client-active' ).val( '1' );
$( '#client-gads-id' ).val( '' );
$( '#client-fbads-id' ).val( '' );
$( '#client-gmc-id' ).val( '' );
$( '#client-gads-start' ).val( '' );
$( '#client-modal' ).fadeIn();
@@ -166,6 +185,7 @@ function editClient( id )
$( '#client-name' ).val( data.name );
$( '#client-active' ).val( parseInt( data.active, 10 ) === 0 ? '0' : '1' );
$( '#client-gads-id' ).val( data.google_ads_customer_id || '' );
$( '#client-fbads-id' ).val( data.facebook_ads_account_id || '' );
$( '#client-gmc-id' ).val( data.google_merchant_account_id || '' );
$( '#client-gads-start' ).val( data.google_ads_start_date || '' );
$( '#client-modal' ).fadeIn();
@@ -245,7 +265,8 @@ function syncClient( id, pipeline, btn )
var labels = {
campaigns: 'kampanii',
products: 'produktow',
campaigns_product_alerts_merchant: 'walidacji Merchant'
campaigns_product_alerts_merchant: 'walidacji Merchant',
facebook_ads: 'Facebook Ads'
};
$.post( '/clients/force_sync', { id: id, pipeline: pipeline }, function( response )
@@ -259,9 +280,16 @@ function syncClient( id, pipeline, btn )
{
$btn.addClass( 'is-queued' );
var cron_hint = pipeline === 'facebook_ads'
? ' Dane zostana pobrane przy najblizszym uruchomieniu /cron/cron_facebook_ads.'
: ' Dane zostana pobrane przy najblizszym uruchomieniu CRON.';
var refresh_hint = pipeline === 'facebook_ads'
? ' Wymuszenie Facebook Ads nadpisuje dane dla okresu z config.php i pobiera tylko aktywne kampanie/zestawy/reklamy.'
: '';
$.alert({
title: 'Zakolejkowano',
content: 'Synchronizacja ' + labels[ pipeline ] + ' zostala zakolejkowana. Dane zostana pobrane przy najblizszym uruchomieniu CRON.',
content: 'Synchronizacja ' + labels[ pipeline ] + ' zostala zakolejkowana.' + cron_hint + refresh_hint,
type: 'green',
autoClose: 'ok|3000'
});
@@ -362,6 +390,7 @@ function loadSyncStatus()
if ( info.campaigns ) html += renderSyncBar( 'K:', info.campaigns[0], info.campaigns[1] );
if ( info.products ) html += renderSyncBar( 'P:', info.products[0], info.products[1] );
if ( info.merchant ) html += renderSyncBar( 'M:', info.merchant[0], info.merchant[1] );
if ( info.facebook_ads ) html += renderSyncBar( 'FB:', info.facebook_ads[0], info.facebook_ads[1] );
html += '</div>';
$cell.html( html );