#!/usr/bin/perl -w
use
lib
"$Bin/../../lib"
;
qw(NON_NATIVE)
;
use
Google::Ads::GoogleAds::V12::Services::AdGroupCriterionService::AdGroupCriterionOperation;
my
$customer_id
=
"INSERT_CUSTOMER_ID_HERE"
;
my
$merchant_center_account_id
=
"INSERT_MERCHANT_CENTER_ACCOUNT_ID_HERE"
;
my
$campaign_budget_id
=
"INSERT_CAMPAIGN_BUDGET_ID_HERE"
;
my
$user_list_id
=
"INSERT_USER_LIST_ID_HERE"
;
sub
add_merchant_center_dynamic_remarketing_campaign {
my
(
$api_client
,
$customer_id
,
$merchant_center_account_id
,
$campaign_budget_id
,
$user_list_id
)
=
@_
;
my
$campaign_resource_name
=
create_campaign(
$api_client
,
$customer_id
,
$merchant_center_account_id
,
$campaign_budget_id
);
my
$ad_group_resource_name
=
create_ad_group(
$api_client
,
$customer_id
,
$campaign_resource_name
);
create_ad(
$api_client
,
$customer_id
,
$ad_group_resource_name
);
attach_user_list(
$api_client
,
$customer_id
,
$ad_group_resource_name
,
$user_list_id
);
return
1;
}
sub
create_campaign {
my
(
$api_client
,
$customer_id
,
$merchant_center_account_id
,
$campaign_budget_id
)
=
@_
;
my
$shopping_settings
=
Google::Ads::GoogleAds::V12::Resources::ShoppingSetting->new({
campaignPriority
=> 0,
merchantId
=>
$merchant_center_account_id
,
salesCountry
=>
"ZZ"
,
enableLocal
=>
"true"
});
my
$campaign
= Google::Ads::GoogleAds::V12::Resources::Campaign->new({
name
=>
"Shopping campaign #"
. uniqid(),
advertisingChannelType
=> DISPLAY,
status
=> Google::Ads::GoogleAds::V12::Enums::CampaignStatusEnum::PAUSED,
campaignBudget
=>
Google::Ads::GoogleAds::V12::Utils::ResourceNames::campaign_budget(
$customer_id
,
$campaign_budget_id
),
manualCpc
=> Google::Ads::GoogleAds::V12::Common::ManualCpc->new(),
shoppingSetting
=>
$shopping_settings
});
my
$campaign_operation
=
Google::Ads::GoogleAds::V12::Services::CampaignService::CampaignOperation->
new({
create
=>
$campaign
});
my
$campaigns_response
=
$api_client
->CampaignService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$campaign_operation
]});
my
$campaign_resource_name
=
$campaigns_response
->{results}[0]{resourceName};
printf
"Created campaign with resource name '%s'.\n"
,
$campaign_resource_name
;
return
$campaign_resource_name
;
}
sub
create_ad_group {
my
(
$api_client
,
$customer_id
,
$campaign_resource_name
) =
@_
;
my
$ad_group
= Google::Ads::GoogleAds::V12::Resources::AdGroup->new({
name
=>
"Dynamic remarketing ad group"
,
campaign
=>
$campaign_resource_name
,
status
=> Google::Ads::GoogleAds::V12::Enums::AdGroupStatusEnum::ENABLED
});
my
$ad_group_operation
=
Google::Ads::GoogleAds::V12::Services::AdGroupService::AdGroupOperation->
new({
create
=>
$ad_group
});
my
$ad_groups_response
=
$api_client
->AdGroupService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$ad_group_operation
]});
my
$ad_group_resource_name
=
$ad_groups_response
->{results}[0]{resourceName};
printf
"Created ad group with resource name '%s'.\n"
,
$ad_group_resource_name
;
return
$ad_group_resource_name
;
}
sub
create_ad {
my
(
$api_client
,
$customer_id
,
$ad_group_resource_name
) =
@_
;
my
$marketing_image_resource_name
= upload_asset(
$api_client
,
$customer_id
,
"Marketing Image"
);
my
$square_marketing_image_resource_name
= upload_asset(
$api_client
,
$customer_id
,
"Square Marketing Image"
);
my
$responsive_display_ad_info
=
Google::Ads::GoogleAds::V12::Common::ResponsiveDisplayAdInfo->new({
marketingImages
=> [
Google::Ads::GoogleAds::V12::Common::AdImageAsset->new({
asset
=>
$marketing_image_resource_name
})
],
squareMarketingImages
=> [
Google::Ads::GoogleAds::V12::Common::AdImageAsset->new({
asset
=>
$square_marketing_image_resource_name
})
],
headlines
=> [
Google::Ads::GoogleAds::V12::Common::AdTextAsset->new({
text
=>
"Travel"
})
],
longHeadline
=> Google::Ads::GoogleAds::V12::Common::AdTextAsset->new({
text
=>
"Travel the World"
}
),
descriptions
=> [
Google::Ads::GoogleAds::V12::Common::AdTextAsset->new({
text
=>
"Take to the air!"
})
],
businessName
=>
"Interplanetary Cruises"
,
callToActionText
=>
"Apply Now"
,
mainColor
=>
"#0000ff"
,
accentColor
=>
"#ffff00"
,
allowFlexibleColor
=>
"false"
,
formatSetting
=> NON_NATIVE,
});
my
$ad_group_ad
= Google::Ads::GoogleAds::V12::Resources::AdGroupAd->new({
adGroup
=>
$ad_group_resource_name
,
ad
=> Google::Ads::GoogleAds::V12::Resources::Ad->new({
responsiveDisplayAd
=>
$responsive_display_ad_info
,
my
$ad_group_ad_operation
=
Google::Ads::GoogleAds::V12::Services::AdGroupAdService::AdGroupAdOperation
->new({
create
=>
$ad_group_ad
});
my
$ad_group_ads_response
=
$api_client
->AdGroupAdService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$ad_group_ad_operation
]});
printf
"Created ad group ad with resource name '%s'.\n"
,
$ad_group_ads_response
->{results}[0]{resourceName};
}
sub
upload_asset {
my
(
$api_client
,
$customer_id
,
$image_url
,
$asset_name
) =
@_
;
my
$image_data
= get_base64_data_from_url(
$image_url
);
my
$asset
= Google::Ads::GoogleAds::V12::Resources::Asset->new({
name
=>
$asset_name
,
type
=> IMAGE,
imageAsset
=> Google::Ads::GoogleAds::V12::Common::ImageAsset->new({
data
=>
$image_data
})});
my
$asset_operation
=
Google::Ads::GoogleAds::V12::Services::AssetService::AssetOperation->new({
create
=>
$asset
});
my
$assets_response
=
$api_client
->AssetService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$asset_operation
]});
my
$image_asset_resource_name
=
$assets_response
->{results}[0]{resourceName};
printf
"Created image asset with resource name '%s'.\n"
,
$image_asset_resource_name
;
return
$image_asset_resource_name
;
}
sub
attach_user_list {
my
(
$api_client
,
$customer_id
,
$ad_group_resource_name
,
$user_list_id
) =
@_
;
my
$ad_group_criterion
=
Google::Ads::GoogleAds::V12::Resources::AdGroupCriterion->new({
adGroup
=>
$ad_group_resource_name
,
userList
=> Google::Ads::GoogleAds::V12::Common::UserListInfo->new({
userList
=>
Google::Ads::GoogleAds::V12::Utils::ResourceNames::user_list(
$customer_id
,
$user_list_id
)})});
my
$ad_group_criterion_operation
=
Google::Ads::GoogleAds::V12::Services::AdGroupCriterionService::AdGroupCriterionOperation
->new({
create
=>
$ad_group_criterion
});
my
$ad_group_criteria_response
=
$api_client
->AdGroupCriterionService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$ad_group_criterion_operation
]});
printf
"Created ad group criterion with resource name '%s'.\n"
,
$ad_group_criteria_response
->{results}[0]{resourceName};
}
if
(abs_path($0) ne abs_path(__FILE__)) {
return
1;
}
my
$api_client
= Google::Ads::GoogleAds::Client->new();
$api_client
->set_die_on_faults(1);
GetOptions(
"customer_id=s"
=> \
$customer_id
,
"merchant_center_account_id=i"
=> \
$merchant_center_account_id
,
"campaign_budget_id=i"
=> \
$campaign_budget_id
,
"user_list_id=i"
=> \
$user_list_id
);
pod2usage(2)
if
not check_params(
$customer_id
,
$merchant_center_account_id
,
$campaign_budget_id
,
$user_list_id
);
add_merchant_center_dynamic_remarketing_campaign(
$api_client
,
$customer_id
=~ s/-//gr,
$merchant_center_account_id
,
$campaign_budget_id
,
$user_list_id
);