#!/usr/bin/perl -w
use
lib
"$Bin/../../lib"
;
qw(BROAD EXACT PHRASE)
;
use
Google::Ads::GoogleAds::V16::Services::AdGroupCriterionService::AdGroupCriterionOperation;
use
Google::Ads::GoogleAds::V16::Services::CampaignBudgetService::CampaignBudgetOperation;
use
Google::Ads::GoogleAds::V16::Services::CampaignCriterionService::CampaignCriterionOperation;
use
Google::Ads::GoogleAds::V16::Services::CustomizerAttributeService::CustomizerAttributeOperation;
use
Google::Ads::GoogleAds::V16::Services::CustomerCustomizerService::CustomerCustomizerOperation;
use
Google::Ads::GoogleAds::V16::Services::GeoTargetConstantService::LocationNames;
use
constant
KEYWORD_TEXT_EXACT
=>
"example of exact match"
;
use
constant
KEYWORD_TEXT_PHRASE
=>
"example of phrase match"
;
use
constant
KEYWORD_TEXT_BROAD
=>
"example of broad match"
;
use
constant
GEO_LOCATION_1
=>
"Buenos Aires"
;
use
constant
GEO_LOCATION_2
=>
"San Isidro"
;
use
constant
GEO_LOCATION_3
=>
"Mar del Plata"
;
sub
add_responsive_search_ad_full {
my
(
$api_client
,
$customer_id
,
$customizer_attribute_name
) =
@_
;
if
(
defined
$customizer_attribute_name
) {
my
$customizer_attribute_resource_name
=
create_customizer_attribute(
$api_client
,
$customer_id
,
$customizer_attribute_name
);
link_customizer_attribute_to_customer(
$api_client
,
$customer_id
,
$customizer_attribute_resource_name
);
}
my
$campaign_budget
= create_campaign_budget(
$api_client
,
$customer_id
);
my
$campaign_resource_name
=
create_campaign(
$api_client
,
$customer_id
,
$campaign_budget
);
my
$ad_group_resource_name
=
create_ad_group(
$api_client
,
$customer_id
,
$campaign_resource_name
);
create_ad_group_ad(
$api_client
,
$customer_id
,
$ad_group_resource_name
,
$customizer_attribute_name
);
add_keywords(
$api_client
,
$customer_id
,
$ad_group_resource_name
);
add_geo_targeting(
$api_client
,
$customer_id
,
$campaign_resource_name
);
return
1;
}
sub
create_customizer_attribute {
my
(
$api_client
,
$customer_id
,
$customizer_attribute_name
) =
@_
;
my
$customizer_attribute
=
Google::Ads::GoogleAds::V16::Resources::CustomizerAttribute->new({
name
=>
$customizer_attribute_name
,
type
=> PRICE
});
my
$operation
=
Google::Ads::GoogleAds::V16::Services::CustomizerAttributeService::CustomizerAttributeOperation
->new({
create
=>
$customizer_attribute
});
my
$response
=
$api_client
->CustomizerAttributeService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$operation
]});
my
$resource_name
=
$response
->{results}[0]{resourceName};
printf
"Added a customizer attribute with resource name '%s'.\n"
,
$resource_name
;
return
$resource_name
;
}
sub
link_customizer_attribute_to_customer {
my
(
$api_client
,
$customer_id
,
$customizer_attribute_resource_name
) =
@_
;
my
$customer_customizer
=
Google::Ads::GoogleAds::V16::Resources::CustomerCustomizer->new({
customizerAttribute
=>
$customizer_attribute_resource_name
,
value
=> Google::Ads::GoogleAds::V16::Common::CustomizerValue->new({
type
=> PRICE,
stringValue
=>
"100USD"
})});
my
$operation
=
Google::Ads::GoogleAds::V16::Services::CustomerCustomizerService::CustomerCustomizerOperation
->new({
create
=>
$customer_customizer
});
my
$response
=
$api_client
->CustomerCustomizerService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$operation
]});
printf
"Added a customer customizer with resource name '%s'.\n"
,
$response
->{results}[0]{resourceName};
}
sub
create_ad_text_asset {
my
(
$api_client
,
$text
,
$pinned_field
) =
@_
;
my
$ad_text_asset
= Google::Ads::GoogleAds::V16::Common::AdTextAsset->new({
text
=>
$text
});
if
(
defined
$pinned_field
) {
$ad_text_asset
->{pinnedField} =
$pinned_field
;
}
return
$ad_text_asset
;
}
sub
create_ad_text_asset_with_customizer {
my
(
$api_client
,
$customizer_attribute_resource_name
) =
@_
;
my
$ad_text_asset
= create_ad_text_asset(
$api_client
,
"Just {CUSTOMIZER.$customizer_attribute_resource_name:10USD}"
);
return
$ad_text_asset
;
}
sub
create_campaign_budget {
my
(
$api_client
,
$customer_id
) =
@_
;
my
$campaign_budget
=
Google::Ads::GoogleAds::V16::Resources::CampaignBudget->new({
name
=>
"Campaign budget "
. uniqid(),
amountMicros
=> 50000000,
deliveryMethod
=> STANDARD
});
my
$campaign_budget_operation
=
Google::Ads::GoogleAds::V16::Services::CampaignBudgetService::CampaignBudgetOperation
->new({
create
=>
$campaign_budget
});
my
$campaign_budgets_response
=
$api_client
->CampaignBudgetService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$campaign_budget_operation
]});
my
$campaign_budget_resource_name
=
$campaign_budgets_response
->{results}[0]{resourceName};
return
$campaign_budget_resource_name
;
}
sub
create_campaign {
my
(
$api_client
,
$customer_id
,
$campaign_budget
) =
@_
;
my
$campaign
= Google::Ads::GoogleAds::V16::Resources::Campaign->new({
name
=>
"Testing RSA via API "
. uniqid(),
campaignBudget
=>
$campaign_budget
,
status
=> PAUSED,
advertisingChannelType
=> SEARCH,
targetSpend
=> Google::Ads::GoogleAds::V16::Common::TargetSpend->new(),
networkSettings
=>
Google::Ads::GoogleAds::V16::Resources::NetworkSettings->new({
targetGoogleSearch
=>
"true"
,
targetSearchNetwork
=>
"true"
,
targetContentNetwork
=>
"true"
,
targetPartnerSearchNetwork
=>
"false"
}
),
});
my
$campaign_operation
=
Google::Ads::GoogleAds::V16::Services::CampaignService::CampaignOperation->
new({
create
=>
$campaign
});
my
$campaigns_response
=
$api_client
->CampaignService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$campaign_operation
]});
my
$resource_name
=
$campaigns_response
->{results}[0]{resourceName};
printf
"Created App campaign with resource name: '%s'.\n"
,
$resource_name
;
return
$resource_name
;
}
sub
create_ad_group {
my
(
$api_client
,
$customer_id
,
$campaign_resource_name
) =
@_
;
my
$ad_group
= Google::Ads::GoogleAds::V16::Resources::AdGroup->new({
name
=>
"Testing RSA via API "
. uniqid(),
status
=> Google::Ads::GoogleAds::V16::Enums::AdGroupStatusEnum::ENABLED,
campaign
=>
$campaign_resource_name
,
type
=> SEARCH_STANDARD,
});
my
$ad_group_operation
=
Google::Ads::GoogleAds::V16::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 '%s'.\n"
,
$ad_group_resource_name
;
return
$ad_group_resource_name
;
}
sub
create_ad_group_ad {
my
(
$api_client
,
$customer_id
,
$ad_group_resource_name
,
$customizer_attribute_name
)
=
@_
;
my
$pinned_headline
=
create_ad_text_asset(
$api_client
,
"Headline 1 testing"
, HEADLINE_1);
my
$ad_group_ad
= Google::Ads::GoogleAds::V16::Resources::AdGroupAd->new({
adGroup
=>
$ad_group_resource_name
,
status
=>
Google::Ads::GoogleAds::V16::Enums::AdGroupAdStatusEnum::ENABLED,
ad
=> Google::Ads::GoogleAds::V16::Resources::Ad->new({
responsiveSearchAd
=>
Google::Ads::GoogleAds::V16::Common::ResponsiveSearchAdInfo->new({
headlines
=> [
$pinned_headline
,
create_ad_text_asset(
$api_client
,
"Headline 2 testing"
),
create_ad_text_asset(
$api_client
,
"Headline 3 testing"
),
],
descriptions
=> [
create_ad_text_asset(
$api_client
,
"Desc 1 testing"
),
defined
$customizer_attribute_name
? create_ad_text_asset_with_customizer(
$api_client
,
$customizer_attribute_name
)
: create_ad_text_asset(
$api_client
,
"Desc 2 testing"
),
],
path1
=>
"all-inclusive"
,
path2
=>
"deals"
}
),
my
$operation
=
Google::Ads::GoogleAds::V16::Services::AdGroupAdService::AdGroupAdOperation
->new({
create
=>
$ad_group_ad
});
my
$response
=
$api_client
->AdGroupAdService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$operation
]});
printf
"Created responsive search ad with resource name '%s'.\n"
,
$response
->{results}[0]{resourceName};
}
sub
add_keywords {
my
(
$api_client
,
$customer_id
,
$ad_group_resource_name
) =
@_
;
my
$operations
= [];
my
$keywords
= {
EXACT() => KEYWORD_TEXT_EXACT,
BROAD() => KEYWORD_TEXT_BROAD,
PHRASE() => KEYWORD_TEXT_PHRASE,
};
foreach
my
$keyword
(
keys
%$keywords
) {
my
$keyword_info
= Google::Ads::GoogleAds::V16::Common::KeywordInfo->new({
text
=>
$keywords
->{
$keyword
},
matchType
=>
$keyword
,
});
my
$ad_group_criterion
=
Google::Ads::GoogleAds::V16::Resources::AdGroupCriterion->new({
adGroup
=>
$ad_group_resource_name
,
status
=>
Google::Ads::GoogleAds::V16::Enums::AdGroupCriterionStatusEnum::ENABLED,
keyword
=>
$keyword_info
,
});
my
$ad_group_criterion_operation
=
Google::Ads::GoogleAds::V16::Services::AdGroupCriterionService::AdGroupCriterionOperation
->new({
create
=>
$ad_group_criterion
});
push
@$operations
,
$ad_group_criterion_operation
;
}
my
$response
=
$api_client
->AdGroupCriterionService()->mutate({
customerId
=>
$customer_id
,
operations
=>
$operations
});
foreach
my
$result
(@{
$response
->{results}}) {
printf
"Created keyword with resource name: '%s'.\n"
,
$result
->{resourceName};
}
}
sub
add_geo_targeting {
my
(
$api_client
,
$customer_id
,
$campaign_resource_name
) =
@_
;
my
$suggest_response
=
$api_client
->GeoTargetConstantService()->suggest({
locale
=> LOCALE,
countryCode
=> COUNTRY_CODE,
locationNames
=>
Google::Ads::GoogleAds::V16::Services::GeoTargetConstantService::LocationNames
->new({
names
=> [GEO_LOCATION_1, GEO_LOCATION_2, GEO_LOCATION_3]})});
my
$operations
= [];
foreach
my
$geo_target_constant_suggestion
(
@{
$suggest_response
->{geoTargetConstantSuggestions}})
{
printf
"geo target constant: '%s' is found in locale '%s' with reach %d"
.
" for the search term '%s'.\n"
,
$geo_target_constant_suggestion
->{geoTargetConstant}{resourceName},
$geo_target_constant_suggestion
->{locale},
$geo_target_constant_suggestion
->{reach},
$geo_target_constant_suggestion
->{searchTerm};
my
$campaign_criterion
=
Google::Ads::GoogleAds::V16::Resources::CampaignCriterion->new({
location
=> Google::Ads::GoogleAds::V16::Common::LocationInfo->new({
geoTargetConstant
=>
$geo_target_constant_suggestion
->{geoTargetConstant}{resourceName}
}
),
campaign
=>
$campaign_resource_name
});
push
@$operations
,
Google::Ads::GoogleAds::V16::Services::CampaignCriterionService::CampaignCriterionOperation
->new({
create
=>
$campaign_criterion
});
}
if
(
scalar
@$operations
== 0) {
return
;
}
my
$campaign_criterion_response
=
$api_client
->CampaignCriterionService()->mutate({
customerId
=>
$customer_id
,
operations
=>
$operations
});
my
$campaign_criterion_results
=
$campaign_criterion_response
->{results};
printf
"Added %d campaign criteria:\n"
,
scalar
@$campaign_criterion_results
;
foreach
my
$campaign_criterion_result
(
@$campaign_criterion_results
) {
printf
"\t%s\n"
,
$campaign_criterion_result
->{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);
my
$customer_id
;
my
$customizer_attribute_name
;
GetOptions(
"customer_id=s"
=> \
$customer_id
,
"customizer_attribute_name=s"
=> \
$customizer_attribute_name
,
);
pod2usage(2)
if
not check_params(
$customer_id
);
add_responsive_search_ad_full(
$api_client
,
$customer_id
=~ s/-//gr,
$customizer_attribute_name
);