#!/usr/bin/perl -w
use
lib
"$Bin/../../lib"
;
use
Google::Ads::GoogleAds::V11::Services::SharedCriterionService::SharedCriterionOperation;
use
Google::Ads::GoogleAds::V11::Services::CampaignSharedSetService::CampaignSharedSetOperation;
my
$customer_id
=
"INSERT_CUSTOMER_ID_HERE"
;
my
$campaign_id
=
"INSERT_CAMPAIGN_ID_HERE"
;
sub
create_and_attach_shared_keyword_set {
my
(
$api_client
,
$customer_id
,
$campaign_id
) =
@_
;
my
$shared_set
= Google::Ads::GoogleAds::V11::Resources::SharedSet->new({
name
=>
"API Negative keyword list - "
. uniqid(),
type
=> NEGATIVE_KEYWORDS
});
my
$shared_set_operation
=
Google::Ads::GoogleAds::V11::Services::SharedSetService::SharedSetOperation
->new({
create
=>
$shared_set
});
my
$shared_sets_response
=
$api_client
->SharedSetService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$shared_set_operation
]});
my
$shared_set_resource_name
=
$shared_sets_response
->{results}[0]{resourceName};
printf
"Created shared set: '%s'.\n"
,
$shared_set_resource_name
;
my
$shared_criterion_operations
= [];
my
$keywords
= [
'mars cruise'
,
'mars hotels'
];
foreach
my
$keyword
(
@$keywords
) {
my
$shared_criterion
=
Google::Ads::GoogleAds::V11::Resources::SharedCriterion->new({
keyword
=> Google::Ads::GoogleAds::V11::Common::KeywordInfo->new({
text
=>
$keyword
,
matchType
=> BROAD
}
),
sharedSet
=>
$shared_set_resource_name
});
my
$shared_criterion_operation
=
Google::Ads::GoogleAds::V11::Services::SharedCriterionService::SharedCriterionOperation
->new({
create
=>
$shared_criterion
});
push
@$shared_criterion_operations
,
$shared_criterion_operation
;
}
my
$shared_criteria_response
=
$api_client
->SharedCriterionService()->mutate({
customerId
=>
$customer_id
,
operations
=>
$shared_criterion_operations
});
my
$shared_criterion_results
=
$shared_criteria_response
->{results};
printf
"Added %d shared criterion:\n"
,
scalar
@$shared_criterion_results
;
foreach
my
$shared_criterion_result
(
@$shared_criterion_results
) {
printf
"\t%s\n"
,
$shared_criterion_result
->{resourceName};
}
my
$campaign_shared_set
=
Google::Ads::GoogleAds::V11::Resources::CampaignSharedSet->new({
campaign
=> Google::Ads::GoogleAds::V11::Utils::ResourceNames::campaign(
$customer_id
,
$campaign_id
),
sharedSet
=>
$shared_set_resource_name
});
my
$campaign_shared_set_operation
=
Google::Ads::GoogleAds::V11::Services::CampaignSharedSetService::CampaignSharedSetOperation
->new({
create
=>
$campaign_shared_set
});
my
$campaign_shared_sets_response
=
$api_client
->CampaignSharedSetService()->mutate({
customerId
=>
$customer_id
,
operations
=> [
$campaign_shared_set_operation
]});
printf
"Created campaign shared set: '%s'.\n"
,
$campaign_shared_sets_response
->{results}[0]{resourceName};
return
1;
}
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
,
"campaign_id=i"
=> \
$campaign_id
);
pod2usage(2)
if
not check_params(
$customer_id
,
$campaign_id
);
create_and_attach_shared_keyword_set(
$api_client
,
$customer_id
=~ s/-//gr,
$campaign_id
);