PageRenderTime 63ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/v201109/basic_operations/add_campaigns.pl

http://google-api-adwords-perl.googlecode.com/
Perl | 125 lines | 76 code | 14 blank | 35 comment | 2 complexity | 4df06508c0a864cb0bf057ec2a2bdfaa MD5 | raw file
Possible License(s): Apache-2.0
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright 2011, Google Inc. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # This example adds campaigns.
  18. #
  19. # Tags: CampaignService.mutate
  20. # Author: David Torres <api.davidtorres@gmail.com>
  21. use strict;
  22. use lib "../../../lib";
  23. use Google::Ads::AdWords::Client;
  24. use Google::Ads::AdWords::Logging;
  25. use Google::Ads::AdWords::v201109::Budget;
  26. use Google::Ads::AdWords::v201109::BudgetOptimizer;
  27. use Google::Ads::AdWords::v201109::Campaign;
  28. use Google::Ads::AdWords::v201109::CampaignOperation;
  29. use Google::Ads::AdWords::v201109::FrequencyCap;
  30. use Google::Ads::AdWords::v201109::GeoTargetTypeSetting;
  31. use Google::Ads::AdWords::v201109::ManualCPC;
  32. use Google::Ads::AdWords::v201109::Money;
  33. use Google::Ads::AdWords::v201109::NetworkSetting;
  34. use Cwd qw(abs_path);
  35. use Data::Uniqid qw(uniqid);
  36. use POSIX;
  37. sub add_campaigns {
  38. my $client = shift;
  39. my $num_campaigns = 2;
  40. my @operations = ();
  41. for (my $i = 0; $i < $num_campaigns; $i++) {
  42. # Create campaign.
  43. my $timestr = POSIX::strftime("%Y%m%d", localtime);
  44. my $campaign = Google::Ads::AdWords::v201109::Campaign->new({
  45. name => "Interplanetary Cruise #" . uniqid(),
  46. # Bidding strategy (required).
  47. biddingStrategy => Google::Ads::AdWords::v201109::ManualCPC->new(),
  48. # Budget (required).
  49. budget => Google::Ads::AdWords::v201109::Budget->new({
  50. amount => Google::Ads::AdWords::v201109::Money->new({
  51. microAmount => 5000000
  52. }),
  53. deliveryMethod => "STANDARD",
  54. period => "DAILY",
  55. }),
  56. # Network targeting (recommended).
  57. networkSetting => Google::Ads::AdWords::v201109::NetworkSetting->new({
  58. targetGoogleSearch => 1,
  59. targetSearchNetwork => 1,
  60. targetContentContextual => 0,
  61. targetContentNetwork => 0,
  62. targetPartnerSearchNetwork => 0
  63. }),
  64. # Frecuency cap (non-required).
  65. frequencyCap => Google::Ads::AdWords::v201109::FrequencyCap->new({
  66. impressions => 5,
  67. timeUnit => "DAY",
  68. level => "ADGROUP"
  69. }),
  70. # Advanced location targeting settings (non-required).
  71. settings => [Google::Ads::AdWords::v201109::GeoTargetTypeSetting->new({
  72. positiveGeoTargetType => "DONT_CARE",
  73. negativeGeoTargetType => "DONT_CARE"
  74. })],
  75. # Additional properties (non-required).
  76. startDate => POSIX::strftime("%Y%m%d", localtime),
  77. endDate => POSIX::strftime("%Y%m%d", localtime(time + 60 * 60 * 24)),
  78. status => "PAUSED",
  79. adServingOptimizationStatus => "ROTATE"
  80. });
  81. # Create operation.
  82. my $campaign_operation =
  83. Google::Ads::AdWords::v201109::CampaignOperation->new({
  84. operator => "ADD",
  85. operand => $campaign
  86. });
  87. push @operations, $campaign_operation;
  88. }
  89. # Add campaigns.
  90. my $result = $client->CampaignService()->mutate({
  91. operations => \@operations
  92. });
  93. # Display campaigns.
  94. foreach my $campaign (@{$result->get_value()}) {
  95. printf "Campaign with name \"%s\" and id \"%s\" was added.\n",
  96. $campaign->get_name(), $campaign->get_id();
  97. }
  98. return 1;
  99. }
  100. # Don't run the example if the file is being included.
  101. if (abs_path($0) ne abs_path(__FILE__)) {
  102. return 1;
  103. }
  104. # Log SOAP XML request, response and API errors.
  105. Google::Ads::AdWords::Logging::enable_all_logging();
  106. # Get AdWords Client, credentials will be read from ~/adwords.properties.
  107. my $client = Google::Ads::AdWords::Client->new({version => "v201109"});
  108. # By default examples are set to die on any server returned fault.
  109. $client->set_die_on_faults(1);
  110. # Call the example
  111. add_campaigns($client);