采集google的campaign、ad_group、ad数据到新表bps_ads_campaign、bps_ads_set、bps_ads_ad
This commit is contained in:
parent
7fa9118f2f
commit
83dcc0b7b5
@ -70,23 +70,69 @@ class GoogleAdsAdService extends BaseService
|
||||
* @return mixed
|
||||
* @throws ApiException
|
||||
*/
|
||||
public function runListAds(int $customerId,$options): mixed
|
||||
public function runListAds(int $customerId, $options): mixed
|
||||
{
|
||||
|
||||
// $googleAdsClient = $this->googleAdsClient;
|
||||
$googleAdsClient = new GoogleAdsClientService($options['refresh_token'], $options['login_customer_id']);
|
||||
$googleAdsClient = new GoogleAdsClientService($options['refresh_token'], $options['login_customer_id']);
|
||||
|
||||
// Creates a single shared budget to be used by the campaigns added below.
|
||||
$groupAdsResourceName = self::getAds($googleAdsClient->getGoogleAdsClientWithloginCustomerId(), $customerId);
|
||||
// $groupAdsResourceName = self::getAds($googleAdsClient->getGoogleAdsClientWithloginCustomerId(), $customerId);
|
||||
$groupAdsResourceName = self::getAdsNew($googleAdsClient->getGoogleAdsClientWithloginCustomerId(), $customerId);
|
||||
// dump(json_encode($groupAdsResourceName));
|
||||
if (is_array($groupAdsResourceName)) {
|
||||
self::saveAds($groupAdsResourceName);
|
||||
// self::saveAds($groupAdsResourceName);
|
||||
self::saveAdsNew($groupAdsResourceName);
|
||||
}
|
||||
|
||||
return $groupAdsResourceName;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 在数据库中保存广告信息
|
||||
* @param $groupadsResourceName
|
||||
* @return void
|
||||
*/
|
||||
public static function saveAdsNew($groupadsResourceName)
|
||||
{
|
||||
$tableName = 'bps_ads_ad';
|
||||
$tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'public' . $tableName;
|
||||
foreach ($groupadsResourceName as $data) {
|
||||
// 构建 SQL 语句,添加 ad_type 和 metadata
|
||||
$sql = "INSERT INTO {$tableName}
|
||||
(ad_id, ad_set_id, campaign_id, account_id, name, status, metadata,platform_type, last_sync_time, update_at)
|
||||
VALUES
|
||||
(:ad_id, :ad_group_id, :campaign_id, :customer_id, :ad_name, :status, :metadata, 2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT (platform_type,ad_id)
|
||||
DO UPDATE SET
|
||||
ad_set_id = EXCLUDED.ad_set_id,
|
||||
campaign_id = EXCLUDED.campaign_id,
|
||||
account_id = EXCLUDED.account_id,
|
||||
name = EXCLUDED.name,
|
||||
status = EXCLUDED.status,
|
||||
metadata = EXCLUDED.metadata,
|
||||
last_sync_time = CURRENT_TIMESTAMP, -- 使用当前时间戳
|
||||
update_at = CURRENT_TIMESTAMP -- 使用当前时间戳
|
||||
WHERE {$tableName}.platform_type = 2";
|
||||
|
||||
// 绑定数据
|
||||
$bindData = [
|
||||
'ad_id' => $data['ad_id'],
|
||||
'ad_group_id' => $data['ad_group_id'],
|
||||
'campaign_id' => $data['campaign_id'],
|
||||
'customer_id' => $data['customer_id'],
|
||||
'ad_name' => $data['ad_name'],
|
||||
'status' => $data['status'],
|
||||
'metadata' => $data['metadata'],
|
||||
];
|
||||
|
||||
// 执行 SQL 插入语句
|
||||
ThinkDb::execute($sql, $bindData);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 在数据库中保存广告信息
|
||||
* @param $groupadsResourceName
|
||||
@ -231,6 +277,97 @@ class GoogleAdsAdService extends BaseService
|
||||
return $resourceNames;
|
||||
}
|
||||
|
||||
public
|
||||
static function getAdsNew(GoogleAdsClient $googleAdsClient, int $customerId)
|
||||
{
|
||||
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
|
||||
// Creates a query that retrieves all groups.
|
||||
|
||||
// $response = $googleAdsServiceClient->search($customerId, $query);
|
||||
$query = "SELECT
|
||||
ad_group_ad.ad.id,
|
||||
ad_group.id,
|
||||
campaign.id,
|
||||
customer.id,
|
||||
ad_group_ad.ad.name,
|
||||
ad_group_ad.status,
|
||||
ad_group_ad.ad.resource_name,
|
||||
ad_group_ad.ad.type,
|
||||
ad_group_ad.ad.responsive_display_ad.logo_images,
|
||||
ad_group_ad.ad.responsive_display_ad.square_logo_images,
|
||||
ad_group_ad.ad.responsive_display_ad.marketing_images,
|
||||
ad_group_ad.ad.responsive_display_ad.square_marketing_images,
|
||||
ad_group_ad.ad.responsive_display_ad.youtube_videos
|
||||
FROM ad_group_ad
|
||||
WHERE
|
||||
ad_group_ad.status != 'REMOVED' ";
|
||||
// Issues a search stream request.
|
||||
/** @var GoogleAdsServerStreamDecorator $stream */
|
||||
$stream = $googleAdsServiceClient->searchStream(
|
||||
SearchGoogleAdsStreamRequest::build($customerId, $query)
|
||||
);
|
||||
$resourceNames = [];
|
||||
// Iterates over all rows in all messages and prints the requested field values for
|
||||
// the campaign in each row.
|
||||
foreach ($stream->iterateAllElements() as $googleAdsRow) {
|
||||
/** @var GoogleAdsRow $googleAdsRow */
|
||||
// 假设 $googleAdsRow 是从 Google Ads API 中获取的对象
|
||||
// $finalUrlsList = $googleAdsRow->getAdGroupAd()->getAd()->getFinalUrls();
|
||||
// 将最终的 URL 列表转换为 PHP 数组
|
||||
// $finalUrlsArray = iterator_to_array($finalUrlsList);
|
||||
$resourceName = [];
|
||||
$resourceName['ad_id'] = $googleAdsRow->getAdGroupAd()->getAd()->getId();
|
||||
$resourceName['ad_name'] = $googleAdsRow->getAdGroupAd()->getAd()->getName();
|
||||
$resourceName['ad_group_id'] = $googleAdsRow->getAdGroup()->getId();
|
||||
$resourceName['campaign_id'] = $googleAdsRow->getCampaign()->getId();
|
||||
$resourceName['customer_id'] = $googleAdsRow->getCustomer()->getId();
|
||||
// $resourceName['final_urls'] = $finalUrlsArray;
|
||||
$resourceName['status'] = $googleAdsRow->getAdGroupAd()->getStatus();
|
||||
// $resourceName['resource_name'] = $googleAdsRow->getAdGroupAd()->getAd()->getResourceName();
|
||||
$adType = $googleAdsRow->getAdGroupAd()->getAd()->getType();
|
||||
//ad_type 19=> RESPONSIVE_DISPLAY_AD 自适应展示广告 详细看model定义
|
||||
if ($adType === 19) {
|
||||
// 获取 squareMarketingImages 中的每个 asset
|
||||
$squareMarketingImages = $googleAdsRow->getAdGroupAd()->getAd()->getResponsiveDisplayAd()->getSquareMarketingImages();
|
||||
$squareMarketingAssets = [];
|
||||
foreach ($squareMarketingImages as $image) {
|
||||
$squareMarketingAssets[] = $image->getAsset();
|
||||
}
|
||||
// 获取 logoImages 中的每个 asset
|
||||
$logoImages = $googleAdsRow->getAdGroupAd()->getAd()->getResponsiveDisplayAd()->getLogoImages();
|
||||
$logoAssets = [];
|
||||
foreach ($logoImages as $logo) {
|
||||
$logoAssets[] = $logo->getAsset();
|
||||
}
|
||||
// 获取 marketingImages 中的每个 asset
|
||||
$marketingImages = $googleAdsRow->getAdGroupAd()->getAd()->getResponsiveDisplayAd()->getMarketingImages();
|
||||
$marketingAssets = [];
|
||||
foreach ($marketingImages as $marketing) {
|
||||
$marketingAssets[] = $marketing->getAsset();
|
||||
}
|
||||
// 获取 youtubeVideos 中的每个 asset
|
||||
$youtubeVideos = $googleAdsRow->getAdGroupAd()->getAd()->getResponsiveDisplayAd()->getYoutubeVideos();
|
||||
$youtubeAssets = [];
|
||||
foreach ($youtubeVideos as $video) {
|
||||
$youtubeAssets[] = $video->getAsset();
|
||||
}
|
||||
// 将提取的资产保存到 metadata 数组
|
||||
$resourceName['metadata']['square_marketing_images'] = $squareMarketingAssets;
|
||||
$resourceName['metadata']['logo_images'] = $logoAssets;
|
||||
$resourceName['metadata']['marketing_images'] = $marketingAssets;
|
||||
$resourceName['metadata']['youtube_videos'] = $youtubeAssets;
|
||||
|
||||
// 将 metadata 转换为 JSON 格式
|
||||
$resourceName['metadata'] = json_encode($resourceName['metadata']); // 存储为 JSONB 格式
|
||||
} else {
|
||||
$resourceName['metadata'] = null;
|
||||
}
|
||||
$resourceNames[] = $resourceName;
|
||||
|
||||
}
|
||||
return $resourceNames;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This example updates the CPC bid and status for a given ad group. To get ad groups, run
|
||||
@ -246,7 +383,7 @@ class GoogleAdsAdService extends BaseService
|
||||
{
|
||||
// dump($options);
|
||||
// $googleAdsClient = $this->googleAdsClient;
|
||||
$googleAdsClient = new GoogleAdsClientService($options['refresh_token'],$options['login_customer_id']);
|
||||
$googleAdsClient = new GoogleAdsClientService($options['refresh_token'], $options['login_customer_id']);
|
||||
// Creates a single shared budget to be used by the campaigns added below.
|
||||
|
||||
$resourceNames = self::updateAd($googleAdsClient->getGoogleAdsClientWithloginCustomerId(), $options['customer_id'], $options['group_id'], $options['ad_id'], $options['status']);
|
||||
|
@ -204,10 +204,12 @@ class GoogleAdsCampaignService extends BaseService
|
||||
{
|
||||
|
||||
$googleAdsClient = new GoogleAdsClientService($options['refresh_token'], $options['login_customer_id']);
|
||||
$campaignsResourceName = self::getCampaigns($googleAdsClient->getGoogleAdsClientWithloginCustomerId(), $customerId);
|
||||
// $campaignsResourceName = self::getCampaigns($googleAdsClient->getGoogleAdsClientWithloginCustomerId(), $customerId);
|
||||
$campaignsResourceName = self::getCampaignsNew($googleAdsClient->getGoogleAdsClientWithloginCustomerId(), $customerId);
|
||||
// dump(json_encode($campaignsResourceName));
|
||||
if (is_array($campaignsResourceName)) {
|
||||
self::saveCampaigns($campaignsResourceName);
|
||||
// self::saveCampaigns($campaignsResourceName);
|
||||
self::saveCampaignsNew($campaignsResourceName);
|
||||
}
|
||||
|
||||
return $campaignsResourceName;
|
||||
@ -234,6 +236,60 @@ class GoogleAdsCampaignService extends BaseService
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Runs the example.
|
||||
*
|
||||
* @param GoogleAdsClient $googleAdsClient the Google Ads API client
|
||||
* @param int $customerId the customer ID
|
||||
*/
|
||||
// [START get_campaigns]
|
||||
|
||||
public static function getCampaignsNew(GoogleAdsClient $googleAdsClient, int $customerId)
|
||||
{
|
||||
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
|
||||
// Creates a query that retrieves all campaigns.
|
||||
// $query = 'SELECT campaign.id,campaign.name,campaign.status,campaign.campaign_budget,campaign_budget.id,campaign_budget.name,campaign_budget.amount_micros FROM campaign ORDER BY campaign.id';
|
||||
$query = "SELECT
|
||||
campaign.id,
|
||||
campaign.name,
|
||||
campaign.status,
|
||||
campaign.advertising_channel_type,
|
||||
campaign.start_date,
|
||||
campaign.end_date,
|
||||
campaign_budget.amount_micros,
|
||||
customer.id
|
||||
FROM campaign
|
||||
WHERE
|
||||
campaign.status != 'REMOVED' ";
|
||||
// Issues a search stream request.
|
||||
/** @var GoogleAdsServerStreamDecorator $stream */
|
||||
$stream = $googleAdsServiceClient->searchStream(
|
||||
SearchGoogleAdsStreamRequest::build($customerId, $query)
|
||||
);
|
||||
$resourceNames = [];
|
||||
// Iterates over all rows in all messages and prints the requested field values for
|
||||
// the campaign in each row.
|
||||
foreach ($stream->iterateAllElements() as $googleAdsRow) {
|
||||
/** @var GoogleAdsRow $googleAdsRow */
|
||||
// printf(
|
||||
// "Campaign with ID %d and name '%s' was found.BudgetID %s: %d %s",
|
||||
// $googleAdsRow->getCampaign()->getId(),
|
||||
// $googleAdsRow->getCampaign()->getName(),
|
||||
// $googleAdsRow->getCampaignBudget()->getName(),
|
||||
// $googleAdsRow->getCampaignBudget()->getAmountMicros(),
|
||||
//
|
||||
// $googleAdsRow->getCampaignBudget()->getName(),
|
||||
// PHP_EOL
|
||||
// );
|
||||
$resourceName['campaign_id'] = $googleAdsRow->getCampaign()->getId();
|
||||
$resourceName['campaign_name'] = $googleAdsRow->getCampaign()->getName();
|
||||
$resourceName['status'] = $googleAdsRow->getCampaign()->getStatus();
|
||||
$resourceName['customer_id'] = $googleAdsRow->getCustomer()->getId();
|
||||
$resourceNames[] = $resourceName;
|
||||
}
|
||||
return $resourceNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the example.
|
||||
*
|
||||
@ -423,6 +479,35 @@ class GoogleAdsCampaignService extends BaseService
|
||||
ThinkDb::execute($sql, $data);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 在数据库中保存广告系列信息
|
||||
* @param $campaignsResourceName
|
||||
* @return void
|
||||
*/
|
||||
public static function saveCampaignsNew($campaignsResourceName)
|
||||
{
|
||||
$tableName = 'bps_ads_campaign';
|
||||
$tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'public' . $tableName;
|
||||
|
||||
foreach ($campaignsResourceName as $data) {
|
||||
|
||||
$sql = "INSERT INTO {$tableName}
|
||||
(campaign_id, account_id, name, status, platform_type, last_sync_time, update_at)
|
||||
VALUES (:campaign_id, :customer_id, :campaign_name, :status, 2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT (platform_type,campaign_id)
|
||||
DO UPDATE SET
|
||||
account_id = EXCLUDED.account_id,
|
||||
name = EXCLUDED.name,
|
||||
status = EXCLUDED.status,
|
||||
last_sync_time = CURRENT_TIMESTAMP, -- 使用当前时间戳
|
||||
update_at = CURRENT_TIMESTAMP -- 使用当前时间戳
|
||||
WHERE {$tableName}.platform_type = 2";
|
||||
|
||||
ThinkDb::execute($sql, $data);
|
||||
// 打印最后执行的 SQL 语句
|
||||
// dump(ThinkDb::getLastSql());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -65,10 +65,12 @@ class GoogleAdsGroupService extends BaseService
|
||||
// dump($customerId,$options['refresh_token']);
|
||||
$googleAdsClient = new GoogleAdsClientService($options['refresh_token'],$options['login_customer_id']);
|
||||
// Creates a single shared budget to be used by the campaigns added below.
|
||||
$groupsResourceName = self::getGroups($googleAdsClient->getGoogleAdsClientWithloginCustomerId(), $customerId);
|
||||
// $groupsResourceName = self::getGroups($googleAdsClient->getGoogleAdsClientWithloginCustomerId(), $customerId);
|
||||
$groupsResourceName = self::getGroupsNew($googleAdsClient->getGoogleAdsClientWithloginCustomerId(), $customerId);
|
||||
// dump(json_encode($groupsResourceName));
|
||||
if (is_array($groupsResourceName)) {
|
||||
self::saveGroups($groupsResourceName);
|
||||
// self::saveGroups($groupsResourceName);
|
||||
self::saveGroupsNew($groupsResourceName);
|
||||
}
|
||||
|
||||
return $groupsResourceName;
|
||||
@ -101,6 +103,32 @@ class GoogleAdsGroupService extends BaseService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在数据库中保存广告系列信息
|
||||
* @param $groupsResourceName
|
||||
* @return void
|
||||
*/
|
||||
public static function saveGroupsNew($groupsResourceName)
|
||||
{
|
||||
$tableName = 'bps_ads_set';
|
||||
$tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'public' . $tableName;
|
||||
foreach ($groupsResourceName as $data) {
|
||||
$sql = "INSERT INTO {$tableName}
|
||||
(ad_set_id, campaign_id, account_id, name, status,platform_type, last_sync_time, update_at)
|
||||
VALUES (:ad_group_id, :campaign_id, :customer_id, :ad_group_name, :status,2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT (platform_type,ad_set_id)
|
||||
DO UPDATE SET
|
||||
campaign_id = EXCLUDED.campaign_id,
|
||||
account_id = EXCLUDED.account_id,
|
||||
name = EXCLUDED.name,
|
||||
status = EXCLUDED.status,
|
||||
last_sync_time = CURRENT_TIMESTAMP, -- 使用当前时间戳
|
||||
update_at = CURRENT_TIMESTAMP
|
||||
WHERE {$tableName}.platform_type = 2";
|
||||
ThinkDb::execute($sql, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Runs the example.
|
||||
@ -110,6 +138,51 @@ class GoogleAdsGroupService extends BaseService
|
||||
*/
|
||||
// [START get_campaigns]
|
||||
|
||||
public static function getGroupsNew(GoogleAdsClient $googleAdsClient, int $customerId)
|
||||
{
|
||||
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
|
||||
// Creates a query that retrieves all groups.
|
||||
|
||||
// $response = $googleAdsServiceClient->search($customerId, $query);
|
||||
$query = "SELECT
|
||||
ad_group.id,
|
||||
ad_group.name,
|
||||
campaign.id,
|
||||
customer.id,
|
||||
ad_group.status,
|
||||
ad_group.cpc_bid_micros
|
||||
FROM ad_group
|
||||
WHERE
|
||||
ad_group.status != 'REMOVED' ";
|
||||
// Issues a search stream request.
|
||||
/** @var GoogleAdsServerStreamDecorator $stream */
|
||||
$stream = $googleAdsServiceClient->searchStream(
|
||||
SearchGoogleAdsStreamRequest::build($customerId, $query)
|
||||
);
|
||||
$resourceNames = [];
|
||||
// Iterates over all rows in all messages and prints the requested field values for
|
||||
// the campaign in each row.
|
||||
foreach ($stream->iterateAllElements() as $googleAdsRow) {
|
||||
/** @var GoogleAdsRow $googleAdsRow */
|
||||
// printf(
|
||||
// "Campaign with ID %d and name '%s' was found.BudgetID %s: %d %s",
|
||||
// $googleAdsRow->getCampaign()->getId(),
|
||||
// $googleAdsRow->getCampaign()->getName(),
|
||||
// $googleAdsRow->getCampaignBudget()->getName(),
|
||||
// $googleAdsRow->getCampaignBudget()->getAmountMicros(),
|
||||
//
|
||||
// $googleAdsRow->getCampaignBudget()->getName(),
|
||||
// PHP_EOL
|
||||
// );
|
||||
$resourceName['ad_group_id'] = $googleAdsRow->getAdGroup()->getId();
|
||||
$resourceName['ad_group_name'] = $googleAdsRow->getAdGroup()->getName();
|
||||
$resourceName['status'] = $googleAdsRow->getAdGroup()->getStatus();
|
||||
$resourceName['campaign_id'] = $googleAdsRow->getCampaign()->getId();
|
||||
$resourceName['customer_id'] = $googleAdsRow->getCustomer()->getId();
|
||||
$resourceNames[] = $resourceName;
|
||||
}
|
||||
return $resourceNames;
|
||||
}
|
||||
public static function getGroups(GoogleAdsClient $googleAdsClient, int $customerId)
|
||||
{
|
||||
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
|
||||
|
@ -210,7 +210,7 @@ class GoogleOAuthService
|
||||
ThinkDb::execute($sql, $data);
|
||||
|
||||
$options = [];
|
||||
$options['data'] = $refreshToken;
|
||||
$options['refresh_token'] = $refreshToken;
|
||||
$this->queue($options);
|
||||
}
|
||||
|
||||
@ -316,7 +316,7 @@ class GoogleOAuthService
|
||||
// $options = ['to' => 'tom@gmail.com', 'content' => 'hello'];
|
||||
// 投递消息
|
||||
// Redis::send($queue, $options['data']);
|
||||
QueueClient::send($queue, $options['data'],10); //异步投递 延后15秒
|
||||
QueueClient::send($queue, $options,10); //异步投递 延后15秒
|
||||
// 投递延迟消息,消息会在60秒后处理
|
||||
// Redis::send($queue, $options['data'], 60);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user