PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/googleads-php-lib/examples/Dfp/v201605/ProductTemplateService/CreateProductTemplates.php

https://github.com/markvince/CakePHP-GAStats-Plugin
PHP | 157 lines | 69 code | 31 blank | 57 comment | 0 complexity | 704053a3be75094d3f190489666739b5 MD5 | raw file
  1. <?php
  2. /**
  3. * This example creates a new product template. To determine which product
  4. * templates exist, run GetAllProductTemplates.php.
  5. *
  6. * PHP version 5
  7. *
  8. * Copyright 2014, Google Inc. All Rights Reserved.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * @package GoogleApiAdsDfp
  23. * @subpackage v201605
  24. * @category WebServices
  25. * @copyright 2014, Google Inc. All Rights Reserved.
  26. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License,
  27. * Version 2.0
  28. */
  29. error_reporting(E_STRICT | E_ALL);
  30. // You can set the include path to src directory or reference
  31. // DfpUser.php directly via require_once.
  32. // $path = '/path/to/dfp_api_php_lib/src';
  33. $path = dirname(__FILE__) . '/../../../../src';
  34. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  35. require_once 'Google/Api/Ads/Dfp/Lib/DfpUser.php';
  36. require_once dirname(__FILE__) . '/../../../Common/ExampleUtils.php';
  37. try {
  38. // Get DfpUser from credentials in "../auth.ini"
  39. // relative to the DfpUser.php file's directory.
  40. $user = new DfpUser();
  41. // Log SOAP XML request and response.
  42. $user->LogDefaults();
  43. // Get the ProductTemplateService.
  44. $productTemplateService = $user->GetService('ProductTemplateService',
  45. 'v201605');
  46. // Get the NetworkService.
  47. $networkService = $user->GetService('NetworkService', 'v201605');
  48. // Create a product template.
  49. $productTemplate = new ProductTemplate();
  50. $productTemplate->name = sprintf('Product template #%s', uniqid());
  51. $productTemplate->description = 'This product template creates standard '
  52. . 'proposal line items targeting Chrome browsers with product '
  53. . 'segmentation on ad units and geo targeting.';
  54. // Set the name macro which will be used to generate the names of the
  55. // products. This will create a segmentation based on the line item type, ad
  56. // unit, and location.
  57. $productTemplate->nameMacro =
  58. '<line-item-type> - <ad-unit> - <template-name> - <location>';
  59. // Set the product type so the created proposal line items will be trafficked
  60. // in DFP.
  61. $productTemplate->productType = 'DFP';
  62. // Set rate type to create CPM priced proposal line items.
  63. $productTemplate->rateType = 'CPM';
  64. $productTemplate->deliveryRateType = 'AS_FAST_AS_POSSIBLE';
  65. // Optionally set the creative rotation of the product to serve one or more
  66. // creatives.
  67. $productTemplate->roadblockingType = 'ONE_OR_MORE';
  68. // Create the master creative placeholder.
  69. $creativeMasterPlaceholder = new CreativePlaceholder();
  70. $creativeMasterPlaceholder->size = new Size(728, 90, false);
  71. // Create companion creative placeholders.
  72. $companionCreativePlaceholder = new CreativePlaceholder();
  73. $companionCreativePlaceholder->size = new Size(300, 250, false);
  74. // Set the size of creatives that can be associated with the product template.
  75. $productTemplate->creativePlaceholders =
  76. array($creativeMasterPlaceholder, $companionCreativePlaceholder);
  77. // Set the type of proposal line item to be created from the product template.
  78. $productTemplate->lineItemType = 'STANDARD';
  79. // Get the root ad unit ID used to target the whole site.
  80. $rootAdUnitId = $networkService->getCurrentNetwork()->effectiveRootAdUnitId;
  81. // Create ad unit targeting for the root ad unit (i.e. the whole network).
  82. $adUnitTargeting = new AdUnitTargeting();
  83. $adUnitTargeting->adUnitId = $rootAdUnitId;
  84. $adUnitTargeting->includeDescendants = true;
  85. // Create geo targeting for the US.
  86. $countryLocation = new DfpLocation();
  87. $countryLocation->id = 2840;
  88. // Create geo targeting for Hong Kong.
  89. $regionLocation = new DfpLocation();
  90. $regionLocation->id = 2344;
  91. $geoTargeting = new GeoTargeting();
  92. $geoTargeting->targetedLocations = array($countryLocation, $regionLocation);
  93. // Add browser targeting to Chrome on the product template distinct from
  94. // product segmentation.
  95. $chromeBrowser = new Browser();
  96. $chromeBrowser->id = 500072;
  97. $browserTargeting = new BrowserTargeting();
  98. $browserTargeting->browsers = array($chromeBrowser);
  99. $technologyTargeting = new TechnologyTargeting();
  100. $technologyTargeting->browserTargeting = $browserTargeting;
  101. $productTemplateTargeting = new Targeting();
  102. $productTemplateTargeting->technologyTargeting = $technologyTargeting;
  103. $productTemplate->builtInTargeting = $productTemplateTargeting;
  104. // Allow placement targeting to be customized on the proposal line item.
  105. $customizableAttributes = new CustomizableAttributes();
  106. $customizableAttributes->allowPlacementTargetingCustomization = true;
  107. $productTemplate->customizableAttributes = $customizableAttributes;
  108. // Add inventory and geo targeting as product segmentation.
  109. $productSegmentation = new ProductSegmentation();
  110. $productSegmentation->adUnitSegments = array($adUnitTargeting);
  111. $productSegmentation->geoSegment = $geoTargeting;
  112. $productTemplate->productSegmentation = $productSegmentation;
  113. // Create the product template on the server.
  114. $productTemplates = $productTemplateService->createProductTemplates(
  115. array($productTemplate));
  116. foreach ($productTemplates as $createdProductTemplate) {
  117. printf("A product template with ID %d and name '%s' was created.\n",
  118. $createdProductTemplate->id, $createdProductTemplate->name);
  119. }
  120. } catch (OAuth2Exception $e) {
  121. ExampleUtils::CheckForOAuth2Errors($e);
  122. } catch (ValidationException $e) {
  123. ExampleUtils::CheckForOAuth2Errors($e);
  124. } catch (Exception $e) {
  125. printf("%s\n", $e->getMessage());
  126. }