Is there a way to get final urls for each keyword from api

268 views
Skip to first unread message

Yali Wang

unread,
Nov 24, 2016, 12:31:36 AM11/24/16
to AdWords API Forum
Hi All,

I recently met an issue with final urls, I tried to get final urls for each keyword but it always return null for some reason, could you please help me with this issue ? Thanks a lot.



public class FinalUrls {

private static final int PAGE_SIZE = 100;

public static void main(String[] args) throws Exception {
// Generate a refreshable OAuth2 credential.
Credential oAuth2Credential = new OfflineCredentials.Builder()
.forApi(OfflineCredentials.Api.ADWORDS)
.fromFile()
.build()
.generateCredential();

// Construct an AdWordsSession.
AdWordsSession session = new AdWordsSession.Builder()
.fromFile()
.withOAuth2Credential(oAuth2Credential)
.build();

Long adGroupId = Long.parseLong("3310344562");

AdWordsServices adWordsServices = new AdWordsServices();

getFinalUrls(adWordsServices, session, adGroupId);
}

public static void getFinalUrls(AdWordsServices adWordsServices, AdWordsSession session, Long adGroupId) throws Exception {

int offset = 0;

// Get the AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService =
adWordsServices.get(session, AdGroupCriterionServiceInterface.class);

SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder
.fields(
AdGroupCriterionField.Id,
AdGroupCriterionField.CriteriaType,
AdGroupCriterionField.KeywordMatchType,
AdGroupCriterionField.KeywordText,
AdGroupCriterionField.FinalUrls)
.orderAscBy(AdGroupCriterionField.KeywordText)
.offset(offset)
.limit(PAGE_SIZE)
.in(AdGroupCriterionField.AdGroupId, adGroupId.toString())
.in(AdGroupCriterionField.CriteriaType, "KEYWORD")
.build();

AdGroupCriterionPage page = adGroupCriterionService.get(selector);

// Display ad group criteria.
if (page.getEntries() != null && page.getEntries().length > 0) {
// Display results.
for (AdGroupCriterion adGroupCriterionResult : page.getEntries()) {
Keyword keyword = (Keyword) adGroupCriterionResult.getCriterion();
System.out.printf(
"Keyword with text '%s', match type '%s', criteria type '%s', and ID %d was found.%n",
keyword.getText(), keyword.getMatchType(), keyword.getType(), keyword.getId());
BiddableAdGroupCriterion biddableAdGroupCriterion= (BiddableAdGroupCriterion) adGroupCriterionResult;
System.out.printf("Keyword final url is %s", bac.getBidModifier());
if (biddableAdGroupCriterion == null) {
System.out.println("object is null");
} else {
System.out.printf("Keyword final url is %s", biddableAdGroupCriterion.getFinalUrls().getUrls(0));
System.out.println();
}
}
} else {
System.out.println("No ad group criteria were found.");
}

offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
}
}

Peter Oliquino

unread,
Nov 24, 2016, 1:38:50 AM11/24/16
to AdWords API Forum
Hi Yali,

I tried your code using my test account and was able to get results. You might want to remove the line below as well to have a clearer output :

System.out.printf("Keyword final url is %s", bac.getBidModifier());

I could not see which part of your code where you instantiated the "bac" object so I just removed it. Below is the sample SOAP request and response that I got :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Header>
        <ns1:RequestHeader xmlns:ns1="https://adwords.google.com/api/adwords/cm/v201609" soapenv:mustUnderstand="0">
            <ns1:clientCustomerId>CUSTOMER_ID</ns1:clientCustomerId>
            <ns1:developerToken>DEVELOPER_TOKEN</ns1:developerToken>
            <ns1:userAgent>USER_AGENT</ns1:userAgent>
            <ns1:validateOnly>false</ns1:validateOnly>
            <ns1:partialFailure>false</ns1:partialFailure>
        </ns1:RequestHeader>
    </soapenv:Header>
    <soapenv:Body>
        <get xmlns="https://adwords.google.com/api/adwords/cm/v201609">
            <serviceSelector>
                <fields>Id</fields>
                <fields>CriteriaType</fields>
                <fields>KeywordMatchType</fields>
                <fields>KeywordText</fields>
                <fields>FinalUrls</fields>
                <predicates>
                    <field>AdGroupId</field>
                    <operator>IN</operator>
                    <values>THE_ADGROUP_ID</values>
                </predicates>
                <predicates>
                    <field>CriteriaType</field>
                    <operator>IN</operator>
                    <values>KEYWORD</values>
                </predicates>
                <ordering>
                    <field>KeywordText</field>
                    <sortOrder>ASCENDING</sortOrder>
                </ordering>
                <paging>
                    <startIndex>0</startIndex>
                    <numberResults>100</numberResults>
                </paging>
            </serviceSelector>
        </get>
    </soapenv:Body>
</soapenv:Envelope>
[main] INFO com.google.api.ads.adwords.lib.client.AdWordsServiceClient.soapXmlLogger - SOAP Response:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
        <ResponseHeader xmlns="https://adwords.google.com/api/adwords/cm/v201609">
            <requestId>REQUEST_ID</requestId>
            <serviceName>AdGroupCriterionService</serviceName>
            <methodName>get</methodName>
            <operations>1</operations>
            <responseTime>396</responseTime>
        </ResponseHeader>
    </soap:Header>
    <soap:Body>
        <getResponse xmlns="https://adwords.google.com/api/adwords/cm/v201609">
            <rval>
                <totalNumEntries>1</totalNumEntries>
                <Page.Type>AdGroupCriterionPage</Page.Type>
                <entries xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="BiddableAdGroupCriterion">
                    <adGroupId>THE_ADGROUP_ID</adGroupId>
                    <criterionUse>BIDDABLE</criterionUse>
                    <criterion xsi:type="Keyword">
                        <id>THE_KEYWORD_ID</id>
                        <type>KEYWORD</type>
                        <Criterion.Type>Keyword</Criterion.Type>
                        <text>Test Me</text>
                        <matchType>BROAD</matchType>
                    </criterion>
                    <AdGroupCriterion.Type>BiddableAdGroupCriterion</AdGroupCriterion.Type>
                    <finalUrls>
                        <urls>http://www.testme.com</urls>
                    </finalUrls>
                </entries>
            </rval>
        </getResponse>
    </soap:Body>
</soap:Envelope>

Could you retry your request and let me know if the issue persists? If yes, kindly include the SOAP request and response generated including the clientCustomerId used, so I can take a closer look at the issue. Please reply using Reply privately to author in doing so.

Thanks and regards,
Peter
AdWords API Team

Reply all
Reply to author
Forward
Message has been deleted
0 new messages