PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/googleads-php-lib/examples/Dfp/v201602/CustomTargetingService/CreateCustomTargetingKeysAndValues.php

https://github.com/markvince/CakePHP-GAStats-Plugin
PHP | 147 lines | 78 code | 18 blank | 51 comment | 4 complexity | 3e013f5c436ca4b8c15b5452aa06e682 MD5 | raw file
  1. <?php
  2. /**
  3. * This example creates new custom targeting keys and values. To determine which
  4. * custom targeting keys and values exist, run
  5. * GetAllCustomTargetingKeysAndValues.php. To target these custom
  6. * targeting keys and values, run TargetCustomCriteria.php.
  7. *
  8. * PHP version 5
  9. *
  10. * Copyright 2013, Google Inc. All Rights Reserved.
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. *
  24. * @package GoogleApiAdsDfp
  25. * @subpackage v201602
  26. * @category WebServices
  27. * @copyright 2013, Google Inc. All Rights Reserved.
  28. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License,
  29. * Version 2.0
  30. */
  31. error_reporting(E_STRICT | E_ALL);
  32. // You can set the include path to src directory or reference
  33. // DfpUser.php directly via require_once.
  34. // $path = '/path/to/dfp_api_php_lib/src';
  35. $path = dirname(__FILE__) . '/../../../../src';
  36. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  37. require_once 'Google/Api/Ads/Dfp/Lib/DfpUser.php';
  38. require_once dirname(__FILE__) . '/../../../Common/ExampleUtils.php';
  39. try {
  40. // Get DfpUser from credentials in "../auth.ini"
  41. // relative to the DfpUser.php file's directory.
  42. $user = new DfpUser();
  43. // Log SOAP XML request and response.
  44. $user->LogDefaults();
  45. // Get the CustomTargetingService.
  46. $customTargetingService =
  47. $user->GetService('CustomTargetingService', 'v201602');
  48. // Create predefined key.
  49. $genderKey = new CustomTargetingKey();
  50. $genderKey->displayName = 'gender';
  51. $genderKey->name = 'g';
  52. $genderKey->type = 'PREDEFINED';
  53. // Create predefined key that may be used for content targeting.
  54. $genreKey = new CustomTargetingKey();
  55. $genreKey->displayName = 'genre';
  56. $genreKey->name = 'genre';
  57. $genreKey->type = 'PREDEFINED';
  58. // Create free-form key.
  59. $carModelKey = new CustomTargetingKey();
  60. $carModelKey->displayName = 'car model';
  61. $carModelKey->name = 'c';
  62. $carModelKey->type = 'FREEFORM';
  63. // Create the custom targeting keys on the server.
  64. $keys = $customTargetingService->createCustomTargetingKeys(
  65. array($genderKey, $genreKey, $carModelKey));
  66. if (isset($keys)) {
  67. foreach ($keys as $key) {
  68. printf("A custom targeting key with ID '%s', name '%s', and display " .
  69. "name '%s' was created.\n", $key->id, $key->name, $key->displayName);
  70. }
  71. } else {
  72. print "No keys were created.\n";
  73. }
  74. // Create custom targeting value for the predefined gender key.
  75. $genderMaleValue = new CustomTargetingValue();
  76. $genderMaleValue->customTargetingKeyId = $keys[0]->id;
  77. $genderMaleValue->displayName = 'male';
  78. // Name is set to 1 so that the actual name can be hidden from website
  79. // users.
  80. $genderMaleValue->name = '1';
  81. $genderMaleValue->matchType = 'EXACT';
  82. $genderFemaleValue = new CustomTargetingValue();
  83. $genderFemaleValue->customTargetingKeyId = $keys[0]->id;
  84. $genderFemaleValue->displayName = 'female';
  85. // Name is set to 2 so that the actual name can be hidden from website
  86. // users.
  87. $genderFemaleValue->name = '2';
  88. $genderFemaleValue->matchType = 'EXACT';
  89. // Create custom targeting value for the predefined genre key.
  90. $genreComedyValue = new CustomTargetingValue();
  91. $genreComedyValue->customTargetingKeyId = $keys[1]->id;
  92. $genreComedyValue->displayName = 'comedy';
  93. $genreComedyValue->name = 'comedy';
  94. $genreComedyValue->matchType = 'EXACT';
  95. $genreDramaValue = new CustomTargetingValue();
  96. $genreDramaValue->customTargetingKeyId = $keys[1]->id;
  97. $genreDramaValue->displayName = 'drama';
  98. $genreDramaValue->name = 'drama';
  99. $genreDramaValue->matchType = 'EXACT';
  100. // Create custom targeting value for the free-form age key. These are
  101. // values that would be suggested in the UI or can be used when targeting
  102. // with a free-form custom criterion.
  103. $carModelHondaCivicValue = new CustomTargetingValue();
  104. $carModelHondaCivicValue->customTargetingKeyId = $keys[2]->id;
  105. $carModelHondaCivicValue->displayName = 'honda civic';
  106. $carModelHondaCivicValue->name = 'honda civic';
  107. // Setting match type to exact will match exactly 'honda civic'.
  108. $carModelHondaCivicValue->matchType = 'EXACT';
  109. // Create the custom targeting values on the server.
  110. $values = $customTargetingService->createCustomTargetingValues(array(
  111. $genderMaleValue, $genderFemaleValue, $genreComedyValue,
  112. $genreDramaValue, $carModelHondaCivicValue));
  113. if (isset($values)) {
  114. foreach ($values as $value) {
  115. printf("A custom targeting value with ID '%s', belonging to key " .
  116. "with ID '%s', name '%s', and display name '%s' was created.\n",
  117. $value->id, $value->customTargetingKeyId, $value->name,
  118. $value->displayName);
  119. }
  120. } else {
  121. print "No values were created.\n";
  122. }
  123. } catch (OAuth2Exception $e) {
  124. ExampleUtils::CheckForOAuth2Errors($e);
  125. } catch (ValidationException $e) {
  126. ExampleUtils::CheckForOAuth2Errors($e);
  127. } catch (Exception $e) {
  128. print $e->getMessage() . "\n";
  129. }