#!/usr/bin/perl -w
use
lib
"$Bin/../../lib"
;
qw(TEXT PRICE)
;
use
Google::Ads::GoogleAds::V13::Services::AdGroupCustomizerService::AdGroupCustomizerOperation;
use
Google::Ads::GoogleAds::V13::Services::CustomizerAttributeService::CustomizerAttributeOperation;
sub
add_ad_customizer {
my
(
$api_client
,
$customer_id
,
$ad_group_id
) =
@_
;
my
$string_customizer_name
=
"Planet_"
. uniqid();
my
$price_customizer_name
=
"Price_"
. uniqid();
my
$text_customizer_attribute_resource_name
=
create_text_customizer_attribute(
$api_client
,
$customer_id
,
$string_customizer_name
);
my
$price_customizer_attribute_resource_name
=
create_price_customizer_attribute(
$api_client
,
$customer_id
,
$price_customizer_name
);
link_customizer_attributes(
$api_client
,
$customer_id
,
$ad_group_id
,
$text_customizer_attribute_resource_name
,
$price_customizer_attribute_resource_name
);
create_ad_with_customizations(
$api_client
,
$customer_id
,
$ad_group_id
,
$string_customizer_name
,
$price_customizer_name
);
return
1;
}
sub
create_text_customizer_attribute {
my
(
$api_client
,
$customer_id
,
$customizer_name
) =
@_
;
my
$text_attribute
=
Google::Ads::GoogleAds::V13::Resources::CustomizerAttribute->new({
name
=>
$customizer_name
,
type
=> TEXT
});
my
$text_attribute_operation
=
Google::Ads::GoogleAds::V13::Services::CustomizerAttributeService::CustomizerAttributeOperation
->new({
create
=>
$text_attribute
});
my
$response
=
$api_client
->CustomizerAttributeService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$text_attribute_operation
]});
my
$customizer_attribute_resource_name
=
$response
->{results}[0]{resourceName};
printf
"Added text customizer attribute with resource name '%s'.\n"
,
$customizer_attribute_resource_name
;
return
$customizer_attribute_resource_name
;
}
sub
create_price_customizer_attribute {
my
(
$api_client
,
$customer_id
,
$customizer_name
) =
@_
;
my
$price_attribute
=
Google::Ads::GoogleAds::V13::Resources::CustomizerAttribute->new({
name
=>
$customizer_name
,
type
=> PRICE
});
my
$price_attribute_operation
=
Google::Ads::GoogleAds::V13::Services::CustomizerAttributeService::CustomizerAttributeOperation
->new({
create
=>
$price_attribute
});
my
$response
=
$api_client
->CustomizerAttributeService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$price_attribute_operation
]});
my
$customizer_attribute_resource_name
=
$response
->{results}[0]{resourceName};
printf
"Added price customizer attribute with resource name '%s'.\n"
,
$customizer_attribute_resource_name
;
return
$customizer_attribute_resource_name
;
}
sub
link_customizer_attributes {
my
(
$api_client
,
$customer_id
,
$ad_group_id
,
$text_customizer_attribute_resource_name
,
$price_customizer_attribute_resource_name
) =
@_
;
my
$ad_group_customizer_operations
= [];
my
$marsCustomizer
=
Google::Ads::GoogleAds::V13::Resources::AdGroupCustomizer->new({
customizerAttribute
=>
$text_customizer_attribute_resource_name
,
value
=> Google::Ads::GoogleAds::V13::Common::CustomizerValue->new({
type
=> TEXT,
stringValue
=>
"Mars"
}
),
adGroup
=> Google::Ads::GoogleAds::V13::Utils::ResourceNames::ad_group(
$customer_id
,
$ad_group_id
)});
push
@$ad_group_customizer_operations
,
Google::Ads::GoogleAds::V13::Services::AdGroupCustomizerService::AdGroupCustomizerOperation
->new({
create
=>
$marsCustomizer
});
my
$priceCustomizer
=
Google::Ads::GoogleAds::V13::Resources::AdGroupCustomizer->new({
customizerAttribute
=>
$price_customizer_attribute_resource_name
,
value
=> Google::Ads::GoogleAds::V13::Common::CustomizerValue->new({
type
=> PRICE,
stringValue
=>
"100.0€"
}
),
adGroup
=> Google::Ads::GoogleAds::V13::Utils::ResourceNames::ad_group(
$customer_id
,
$ad_group_id
)});
push
@$ad_group_customizer_operations
,
Google::Ads::GoogleAds::V13::Services::AdGroupCustomizerService::AdGroupCustomizerOperation
->new({
create
=>
$priceCustomizer
});
my
$mutate_ad_group_customizers_response
=
$api_client
->AdGroupCustomizerService()->mutate({
customerId
=>
$customer_id
,
operations
=>
$ad_group_customizer_operations
});
foreach
my
$result
(@{
$mutate_ad_group_customizers_response
->{results}}) {
printf
"Added an ad group customizer with resource name '%s'.\n"
,
$result
->{resourceName};
}
}
sub
create_ad_with_customizations {
my
(
$api_client
,
$customer_id
,
$ad_group_id
,
$string_customizer_name
,
$price_customizer_name
)
=
@_
;
my
$responsive_search_ad_info
=
Google::Ads::GoogleAds::V13::Common::ResponsiveSearchAdInfo->new({
headlines
=> [
Google::Ads::GoogleAds::V13::Common::AdTextAsset->new({
text
=>
"Luxury cruise to {CUSTOMIZER.$string_customizer_name:Venus}"
,
pinnedField
=> HEADLINE_1
}
),
Google::Ads::GoogleAds::V13::Common::AdTextAsset->new({
text
=>
"Only {CUSTOMIZER.$price_customizer_name:10.0€}"
,
}
),
Google::Ads::GoogleAds::V13::Common::AdTextAsset->new({
text
=>
"Cruise to {CUSTOMIZER.$string_customizer_name:Venus} for {CUSTOMIZER.$price_customizer_name:10.0€}"
,
})
],
descriptions
=> [
Google::Ads::GoogleAds::V13::Common::AdTextAsset->new({
text
=>
"Tickets are only {CUSTOMIZER.$price_customizer_name:10.0€}!"
,
}
),
Google::Ads::GoogleAds::V13::Common::AdTextAsset->new({
text
=>
"Buy your tickets to {CUSTOMIZER.$string_customizer_name:Venus} now!"
})]});
my
$ad
= Google::Ads::GoogleAds::V13::Resources::Ad->new({
responsiveSearchAd
=>
$responsive_search_ad_info
,
my
$ad_group_ad
= Google::Ads::GoogleAds::V13::Resources::AdGroupAd->new({
ad
=>
$ad
,
adGroup
=> Google::Ads::GoogleAds::V13::Utils::ResourceNames::ad_group(
$customer_id
,
$ad_group_id
)});
my
$ad_group_ad_operation
=
Google::Ads::GoogleAds::V13::Services::AdGroupAdService::AdGroupAdOperation
->new({
create
=>
$ad_group_ad
});
my
$response
=
$api_client
->AdGroupAdService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$ad_group_ad_operation
]});
my
$ad_group_ad_resource_name
=
$response
->{results}[0]{resourceName};
printf
"Added an ad with resource name '%s'.\n"
,
$ad_group_ad_resource_name
;
}
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);
my
$customer_id
=
undef
;
my
$ad_group_id
=
undef
;
GetOptions(
"customer_id=s"
=> \
$customer_id
,
"ad_group_id=i"
=> \
$ad_group_id
);
pod2usage(2)
if
not check_params(
$customer_id
,
$ad_group_id
);
add_ad_customizer(
$api_client
,
$customer_id
=~ s/-//gr,
$ad_group_id
);