We are trying to upload HTML5 Ad Template for Dynamic Remarketing for Retail. But, it is now showing the preview properly as compared to manual upload of zip file. Can you please help us trouble shoot?
<?php
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\AdsApi\Examples\AdWords\v201806\ShoppingCampaigns;
require 'googleads-php-lib-master/vendor/autoload.php';
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201806\cm\AdGroupAd;
use Google\AdsApi\AdWords\v201806\cm\AdGroupAdOperation;
use Google\AdsApi\AdWords\v201806\cm\AdGroupAdService;
use Google\AdsApi\AdWords\v201806\cm\AdGroupAdStatus;
use Google\AdsApi\AdWords\v201806\cm\Dimensions;
use Google\AdsApi\AdWords\v201806\cm\MediaBundle;
use Google\AdsApi\AdWords\v201806\cm\Operator;
use Google\AdsApi\AdWords\v201806\cm\TemplateAd;
use Google\AdsApi\AdWords\v201806\cm\TemplateElement;
use Google\AdsApi\AdWords\v201806\cm\TemplateElementField;
use Google\AdsApi\AdWords\v201806\cm\TemplateElementFieldType;
use Google\AdsApi\AdWords\v201806\cm\MediaService;
use Google\AdsApi\AdWords\v201806\cm\MediaMediaType;
use Google\AdsApi\Common\MapEntries;
use Google\AdsApi\Common\OAuth2TokenBuilder;
/**
* This example adds an HTML5 ad to given ad group. To get ad groups, run
* GetAdGroups.php.
*/
class AddHtml5Ad{
//const AD_GROUP_ID = '60612836284';
const AD_GROUP_ID = '58679041196';
public static function runExample(AdWordsServices $adWordsServices,AdWordsSession $session,$adGroupId) {
try{
$adGroupAdService = $adWordsServices->get($session, AdGroupAdService::class);
$operations = [];
// Create a template ad.
$html5Ad = new TemplateAd();
$html5Ad->setName('html5');
// 419 represents uplofadsaded HTML5 bundle. See
// for details.
$html5Ad->setTemplateId(419);
$dimensions = new Dimensions();
$dimensions->setWidth(160);
$dimensions->setHeight(600);
$html5Ad->setDimensions($dimensions);
// The HTML5 zip file contains all the HTML, CSS, and images needed for the
// HTML5 ad. For help on creating an HTML5 zip file, check out Google Web
$html5Zip = file_get_contents('new/160_600.zip');
$mediaBundle = new MediaBundle();
$mediaBundle->setData($html5Zip);
//$mediaBundle->setEntryPoint('carousel/index.html');
$mediaBundle->setType('MEDIA_BUNDLE');
// Create the template elements for the ad. You can refer to
// for the list of avaliable template fields.
$media = new TemplateElementField();
$media->setName('Custom_layout');
$media->setFieldMedia($mediaBundle);
$media->setType(TemplateElementFieldType::MEDIA_BUNDLE);
$layout = new TemplateElementField();
$layout->setName('layout');
$layout->setFieldText('Custom');
$layout->setType(TemplateElementFieldType::ENUM);
$adData = new TemplateElement();
$adData->setUniqueName('adData');
$adData->setFields([$media, $layout]);
$html5Ad->setTemplateElements([$adData]);
// Create ad group ad.
$adGroupAd = new AdGroupAd();
$adGroupAd->setAdGroupId($adGroupId);
$adGroupAd->setAd($html5Ad);
// Optional: Set additional settings.
//$adGroupAd->getAd()->setAdType("DYNAMIC_SEARCH_AD");
$adGroupAd->getAd()->setAdType("THIRD_PARTY_REDIRECT_AD");
$adGroupAd->setStatus(AdGroupAdStatus::ENABLED);
// Create ad group ad operation and add it to the list.
$operation = new AdGroupAdOperation();
$operation->setOperand($adGroupAd);
$operation->setOperator(Operator::ADD);
$operations[] = $operation;
// Create the ad group ad on the server and print out some information
// about it.
$result = $adGroupAdService->mutate($operations);
foreach ($result->getValue() as $adGroupAd) {
printf(
"New HTML5 ad with ID %d and display URL '%s' was created.\n",
$adGroupAd->getAd()->getId(),
$adGroupAd->getAd()->getDisplayUrl()
);
}
}
catch(\Exception $e){
$msg=$e->getErrors()[0]->getReason();
print_r($msg);
}
}
public static function main(){
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())
->withClientId("ClientIdapp")
->withClientSecret("ClientSecret")
->withRefreshToken("refreshtoken")
->build();
$session = (new AdWordsSessionBuilder())
->fromFile('adsapi_php.ini')
->withOAuth2Credential($oAuth2Credential)
->withClientCustomerId("")
->build();
self::runExample(new AdWordsServices(),$session,intval(self::AD_GROUP_ID));
}
}
AddHtml5Ad::main();