Add an extension setting

Before associating an extension setting with a customer, campaign, or ad group, you must first create the necessary ExtensionFeedItem objects. The extension feed items contain information about what the extension setting will do. We're going to add sitelinks extensions as an example.

First, create the extension feed items:

PHP

private static function createExtensionFeedItems(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    string $campaignResourceName
) {
    // Creates the first sitelink feed item.
    $sitelinkFeedItem1 =
        self::createSitelinkFeedItem('Store Hours', 'http://www.example.com/storehours');

    // Creates an extension feed item from the sitelink feed item.
    $extensionFeedItem1 = new ExtensionFeedItem([
        'extension_type' => ExtensionType::SITELINK,
        'sitelink_feed_item' => $sitelinkFeedItem1,
        'targeted_campaign' => new StringValue(['value' => $campaignResourceName])
    ]);

    // Creates the second sitelink feed item.
    $sitelinkFeedItem2 = self::createSitelinkFeedItem(
        'Thanksgiving Specials',
        'http://www.example.com/thanksgiving'
    );

    // Start date is set to November 20th. If it's already passed that date for this year,
    // use the same date for the next year instead.
    $targetYearString = date('Y');
    if (strtotime('now') > strtotime('20 November')) {
        $targetYearString = date('Y', strtotime('+1 year'));
    }
    $startTimeString = date('Y-m-d H:i:s', mktime(0, 0, 0, 11, 20, intval($targetYearString)));
    // Use the same year as start time when creating end time.
    $endTimeString = date('Y-m-d H:i:s', mktime(23, 59, 59, 11, 27, intval($targetYearString)));

    // Targets this sitelink for United States only.
    // A list of country codes can be referenced here:
    // https://developers.google.com/adwords/api/docs/appendix/geotargeting
    $unitedStates = ResourceNames::forGeoTargetConstant(2840);

    // Creates an extension feed item from the sitelink feed item.
    $extensionFeedItem2 = new ExtensionFeedItem([
        'extension_type' => ExtensionType::SITELINK,
        'sitelink_feed_item' => $sitelinkFeedItem2,
        'targeted_campaign' => new StringValue(['value' => $campaignResourceName]),
        'start_date_time' => new StringValue(['value' => $startTimeString]),
        'end_date_time' => new StringValue(['value' => $endTimeString]),
        'targeted_geo_target_constant' => new StringValue(['value' => $unitedStates])
    ]);

    // Creates the third sitelink feed item.
    $sitelinkFeedItem3 =
        self::createSitelinkFeedItem('Wifi available', 'http://www.example.com/mobile/wifi');

    // Creates an extension feed item from the sitelink feed item.
    $extensionFeedItem3 = new ExtensionFeedItem([
        'extension_type' => ExtensionType::SITELINK,
        'sitelink_feed_item' => $sitelinkFeedItem3,
        'targeted_campaign' => new StringValue(['value' => $campaignResourceName]),
        'device' => FeedItemTargetDevice::MOBILE,
        'targeted_keyword' => new KeywordInfo([
            'text' => new StringValue(['value' => 'free wifi']),
            'match_type' => KeywordMatchType::BROAD
        ])
    ]);

    // Creates the fourth sitelink feed item.
    $sitelinkFeedItem4 =
        self::createSitelinkFeedItem('Happy hours', 'http://www.example.com/happyhours');

    // Creates an extension feed item from the sitelink feed item.
    $extensionFeedItem4 = new ExtensionFeedItem([
        'extension_type' => ExtensionType::SITELINK,
        'sitelink_feed_item' => $sitelinkFeedItem4,
        'targeted_campaign' => new StringValue(['value' => $campaignResourceName]),
        'ad_schedules' => [
            self::createAdScheduleInfo(
                DayOfWeek::MONDAY,
                18,
                MinuteOfHour::ZERO,
                21,
                MinuteOfHour::ZERO
            ),
            self::createAdScheduleInfo(
                DayOfWeek::TUESDAY,
                18,
                MinuteOfHour::ZERO,
                21,
                MinuteOfHour::ZERO
            ),
            self::createAdScheduleInfo(
                DayOfWeek::WEDNESDAY,
                18,
                MinuteOfHour::ZERO,
                21,
                MinuteOfHour::ZERO
            ),
            self::createAdScheduleInfo(
                DayOfWeek::THURSDAY,
                18,
                MinuteOfHour::ZERO,
                21,
                MinuteOfHour::ZERO
            ),
            self::createAdScheduleInfo(
                DayOfWeek::FRIDAY,
                18,
                MinuteOfHour::ZERO,
                21,
                MinuteOfHour::ZERO
            )
        ]
    ]);

    // Issues a mutate request to add the extension feed items.
    $extensionFeedItemServiceClient = $googleAdsClient->getExtensionFeedItemServiceClient();
    $response = $extensionFeedItemServiceClient->mutateExtensionFeedItems(
        $customerId,
        [
            new ExtensionFeedItemOperation(['create' => $extensionFeedItem1]),
            new ExtensionFeedItemOperation(['create' => $extensionFeedItem2]),
            new ExtensionFeedItemOperation(['create' => $extensionFeedItem3]),
            new ExtensionFeedItemOperation(['create' => $extensionFeedItem4])
        ]
    );

    // Prints some information about the created extension feed items.
    printf(
        "Created %d extension feed items with the following resource names:%s",
        $response->getResults()->count(),
        PHP_EOL
    );
    $createdExtensionFeedItemsResourceNames = [];
    foreach ($response->getResults() as $addedExtensionFeedItem) {
        /** @var ExtensionFeedItem $addedExtensionFeedItem */
        $addedExtensionFeedItemResourceName = $addedExtensionFeedItem->getResourceName();
        print $addedExtensionFeedItemResourceName . PHP_EOL;
        $createdExtensionFeedItemsResourceNames[] = $addedExtensionFeedItemResourceName;
    }
    return $createdExtensionFeedItemsResourceNames;
}

Ruby

client = Google::Ads::GoogleAds::GoogleAdsClient.new

extension_feed_item = client.resource.extension_feed_item do |efi|
  efi.sitelink_feed_item = client.resource.sitelink_feed_item do |sfi|
    sfi.link_text = 'Store Hours'
    sfi.final_urls << 'http://www.example.com/storehours'
  end
end

operation = client.operation.create_resource.extension_feed_item(extension_feed_item)
    

Then, associate the extension feed items with an existing campaign:

PHP

// Creates a campaign extension setting using the previously created extension feed items.
$campaignExtensionSetting = new CampaignExtensionSetting([
    'campaign' => new StringValue(['value' => $campaignResourceName]),
    'extension_type' => ExtensionType::SITELINK,
    'extension_feed_items' => $extensionFeedItems
]);

// Creates the campaign extension setting operation.
$campaignExtensionSettingOperation = new CampaignExtensionSettingOperation();
$campaignExtensionSettingOperation->setCreate($campaignExtensionSetting);

// Issues a mutate request to add the campaign extension setting.
$campaignExtensionSettingServiceClient =
    $googleAdsClient->getCampaignExtensionSettingServiceClient();
$response = $campaignExtensionSettingServiceClient->mutateCampaignExtensionSettings(
    $customerId,
    [$campaignExtensionSettingOperation]
);

Ruby

extension_setting = client.resource.campaign_extension_setting do |ces|
  ces.extension_type = :SITELINK
  ces.campaign = client.path.campaign(customer_id, campaign_id)
  ces.extension_feed_items << efi_resource_name
end

operation = client.operation.create_resource.campaign_extension_setting(extension_setting)

extension_setting_resource_name =
    client.service.campaign_extension_setting.mutate_campaign_extension_settings(customer_id, [operation])