How to get a link to a page from an ad? use googleads-php-lib v201806

29 views
Skip to first unread message

Евгений Морозов

unread,
Sep 10, 2018, 7:59:43 PM9/10/18
to AdWords API and Google Ads API Forum
I found an example 

how to modify it to return a link to site page?

example
        $totalNumEntries = 0;
       
do {
           
// Retrieve ad group ads one page at a time, continuing to request pages
           
// until all ad group ads have been retrieved.
            $page
= $adGroupAdService->get($selector);

           
// Print out some information for each ad group ad.
           
if ($page->getEntries() !== null) {
                $totalNumEntries
= $page->getTotalNumEntries();
               
foreach ($page->getEntries() as $adGroupAd) {
                    printf
(
                       
"Expanded text ad with ID %d, status '%s', and headline '%s - %s' was found.\n",
                        $adGroupAd
->getAd()->getId(),
                        $adGroupAd
->getStatus(),
                        $adGroupAd
->getAd()->getHeadlinePart1(),
                        $adGroupAd
->getAd()->getHeadlinePart2(),
                        $adGroupAd->getLink() // Link to site? https://site.com
                   );
               
}
           
}

            $selector
->getPaging()->setStartIndex(
                $selector
->getPaging()->getStartIndex() + self::PAGE_LIMIT
           
);
       
} while ($selector->getPaging()->getStartIndex() < $totalNumEntries);

        printf
("Number of results found: %d\n", $totalNumEntries);


Евгений Морозов

unread,
Sep 10, 2018, 10:59:10 PM9/10/18
to AdWords API and Google Ads API Forum


вторник, 11 сентября 2018 г., 2:59:43 UTC+3 пользователь Евгений Морозов написал:
I found an example 

how to modify it to return a link to site page?

example

class GetExpandedTextAds
{

   
const AD_GROUP_ID = 'INSERT_AD_GROUP_ID_HERE';
   
const PAGE_LIMIT = 500;

   
public static function runExample(
       
AdWordsServices $adWordsServices,
       
AdWordsSession $session,
        $adGroupId
   
) {
        $adGroupAdService
= $adWordsServices->get($session, AdGroupAdService::class);

       
// Create a selector to select all ads for the specified ad group.
        $selector
= new Selector();
        $selector
->setFields(
           
['Id', 'Status', 'HeadlinePart1', 'HeadlinePart2', 'Description']
       
);
        $selector
->setOrdering([new OrderBy('Id', SortOrder::ASCENDING)]);
        $selector
->setPredicates(
           
[
               
new Predicate('AdGroupId', PredicateOperator::IN, [$adGroupId]),
               
new Predicate(
                   
'AdType',
                   
PredicateOperator::IN,
                   
[AdType::EXPANDED_TEXT_AD]
               
),
               
new Predicate(
                   
'Status',
                   
PredicateOperator::IN,
                   
[AdGroupAdStatus::ENABLED, AdGroupAdStatus::PAUSED]
               
)
           
]
       
);
        $selector
->setPaging(new Paging(0, self::PAGE_LIMIT));


        $totalNumEntries
= 0;
       
do {
           
// Retrieve ad group ads one page at a time, continuing to request pages
           
// until all ad group ads have been retrieved.
            $page
= $adGroupAdService->get($selector);

           
// Print out some information for each ad group ad.
           
if ($page->getEntries() !== null) {
                $totalNumEntries
= $page->getTotalNumEntries();
               
foreach ($page->getEntries() as $adGroupAd) {
                    printf
(
                       
"Expanded text ad with ID %d, status '%s', and headline '%s - %s' was found.\n",
                        $adGroupAd
->getAd()->getId(),
                        $adGroupAd
->getStatus(),
                        $adGroupAd
->getAd()->getHeadlinePart1(),
                        $adGroupAd
->getAd()->getHeadlinePart2()

                   
);
               
}
           
}

            $selector
->getPaging()->setStartIndex(
                $selector
->getPaging()->getStartIndex() + self::PAGE_LIMIT
           
);
       
} while ($selector->getPaging()->getStartIndex() < $totalNumEntries);

        printf
("Number of results found: %d\n", $totalNumEntries);
   
}



   
public static function main()
   
{
       
// Generate a refreshable OAuth2 credential for authentication.
        $oAuth2Credential
= (new OAuth2TokenBuilder())->fromFile()->build();

       
// Construct an API session configured from a properties file and the
       
// OAuth2 credentials above.
        $session
= (new AdWordsSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build();
       
self::runExample(
           
new AdWordsServices(),
            $session
,
            intval
(self::AD_GROUP_ID)
       
);
   
}
}

GetExpandedTextAds::main();

im add selectors

$selector->setFields(
            ['Id', 'Status', 'HeadlinePart1', 'HeadlinePart2', 'Description', 'path1', 'path2', 'url', 'displayUrl', 'finalUrls', 'finalMobileUrls', 'finalAppUrls', 'urlData']
);

result: error

Luis Xander Talag (AdWords API Team)

unread,
Sep 11, 2018, 3:25:14 AM9/11/18
to AdWords API and Google Ads API Forum
Hi,

You could see on this guide all the available fields that you could retrieve to ExpandedTextAd object. Please take note that some URL fields are not supported as seen in the link. However, it appears on your second reply that you are receiving an error, thus, to further investigate this, could you provide the complete SOAP request and response logs when you encountered the error? Please reply via Reply privately to author.

Thanks and regards,
Luis
AdWords API Team
Reply all
Reply to author
Forward
0 new messages