Google Ads API is returning to beta status. Please read our blog post for more details.

Create a Responsive Display Ad

Responsive display ad is the recommended ad type for Smart Display campaigns. To create a responsive display ad, you need to create a new ResponsiveDisplayAdInfo object and set it to an AdGroupAd object.

ResponsiveDisplayAdInfo allows you to reuse assets previously created in your account to set many of its fields, such as

  • marketing_images
  • square_marketing_images
  • headlines
  • long_headline
  • descriptions
  • youtube_videos

Refer to ResponsiveDisplayAdInfo for specs on images and texts that can be used to create an asset for each field.

Code example

Creating the responsive display ad

The following snippet creates a responsive display ad, along with instantiating an image asset, and assigning the ad to a new ad group ad.

PHP

private static function createResponsiveDisplayAd(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $adGroupResourceName,
    string $marketingImageAssetResourceName = null,
    string $squareMarketingImageAssetResourceName = null
) {
    // Creates a new image asset for marketing image and square marketing image if there are no
    // assets' resource names specified.
    $marketingImageAssetResourceName = $marketingImageAssetResourceName ?:
        self::createImageAsset(
            $googleAdsClient,
            $customerId,
            self::MARKETING_IMAGE_URL,
            'Marketing Image'
        );
    $squareMarketingImageAssetResourceName = $squareMarketingImageAssetResourceName ?:
        self::createImageAsset(
            $googleAdsClient,
            $customerId,
            self::SQUARE_MARKETING_IMAGE_URL,
            'Square Marketing Image'
        );

    // Creates a responsive display ad info.
    $responsiveDisplayAdInfo = new ResponsiveDisplayAdInfo([
        // Sets some basic required information for the responsive display ad.
        'headlines' => [new AdTextAsset(['text' => new StringValue(['value' => 'Travel'])])],
        'long_headline' => new AdTextAsset([
            'text' => new StringValue(['value' => 'Travel the World'])
        ]),
        'descriptions' => [
            new AdTextAsset(['text' => new StringValue(['value' => 'Take to the air!'])])
        ],
        'business_name' => new StringValue(['value' => 'Google']),
        // Sets the marketing image and square marketing image to the previously created
        // image assets.
        'marketing_images' => [
            new AdImageAsset([
                'asset' => new StringValue(['value' => $marketingImageAssetResourceName])
            ])
        ],
        'square_marketing_images' => [
            new AdImageAsset([
                'asset' => new StringValue(['value' => $squareMarketingImageAssetResourceName])
            ])
        ],
        // Optional: Sets call to action text, price prefix and promotion text.
        'call_to_action_text' => new StringValue(['value' => 'Shop Now']),
        'price_prefix' => new StringValue(['value' => 'as low as']),
        'promo_text' => new StringValue(['value' => 'Free shipping!']),
    ]);

    // Creates an ad group ad with the created responsive display ad info.
    $adGroupAd = new AdGroupAd([
        'ad_group' => $adGroupResourceName,
        'status' => AdGroupAdStatus::PAUSED,
        'ad' => new Ad([
            'final_urls' => [new StringValue(['value' => 'https://www.example.com'])],
            'responsive_display_ad' => $responsiveDisplayAdInfo
        ])
    ]);

    // Creates an ad group ad operation.
    $adGroupAdOperation = new AdGroupAdOperation();
    $adGroupAdOperation->setCreate($adGroupAd);

    // Issues a mutate request to add the ad group ad.
    $adGroupAdServiceClient = $googleAdsClient->getAdGroupAdServiceClient();
    /** @var MutateAdGroupAdsResponse $adGroupAdResponse */
    $adGroupAdResponse = $adGroupAdServiceClient->mutateAdGroupAds(
        $customerId,
        [$adGroupAdOperation]
    );

    // Prints out some information about the newly created ad.
    $adGroupAdResourceName = $adGroupAdResponse->getResults()[0]->getResourceName();
    printf("Added ad group ad named '%s'.%s", $adGroupAdResourceName, PHP_EOL);
}

Creating the ad image asset

Here is the function that creates the image asset:

PHP

private static function createImageAsset(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $imageUrl,
    string $imageName
) {
    // Creates a media file.
    $asset = new Asset([
        'name' => new StringValue(['value' => $imageName]),
        'type' => AssetType::IMAGE,
        'image_asset' => new ImageAsset([
            'data' => new BytesValue(['value' => file_get_contents($imageUrl)])
        ])
    ]);

    // Creates an asset operation.
    $assetOperation = new AssetOperation();
    $assetOperation->setCreate($asset);

    // Issues a mutate request to add the asset.
    $assetServiceClient = $googleAdsClient->getAssetServiceClient();
    $response = $assetServiceClient->mutateAssets($customerId, [$assetOperation]);

    // Prints out information about the newly added asset.
    $assetResourceName = $response->getResults()[0]->getResourceName();
    printf(
        "A new image asset has been added with resource name: '%s'.%s",
        $assetResourceName,
        PHP_EOL
    );

    return $assetResourceName;
}

Send feedback about...

Google Ads API Beta
Google Ads API Beta
Need help? Visit our support page.