How to retrieve Location from adwords api

720 views
Skip to first unread message

John Geliberte

unread,
Aug 27, 2015, 3:49:34 AM8/27/15
to AdWords API Forum
How can i Retrieve the Location from adwords using the api?

I failed to get them im using java by the way and here is my code for pulling the location from adwords .

  public static void runExample(
      AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
    // Get the CampaignService.
    CampaignCriterionServiceInterface campaignCriterionService =
        adWordsServices.get(session, CampaignCriterionServiceInterface.class);

    int offset = 0;

    // Create selector.
    SelectorBuilder builder = new SelectorBuilder();
    Selector selector = builder
        .fields(
            CampaignCriterionField.CampaignId,
            CampaignCriterionField.Id,
            CampaignCriterionField.CriteriaType,
            CampaignCriterionField.PlatformName,
            CampaignCriterionField.LanguageName,
            CampaignCriterionField.LocationName,
            CampaignCriterionField.KeywordText)
        .in(CampaignCriterionField.CriteriaType, "KEYWORD", "LANGUAGE", "LOCATION", "PLATFORM")
        .offset(0)
        .limit(PAGE_SIZE)
        .equals(CampaignCriterionField.CampaignId,"235598893")
        .build();

    CampaignCriterionPage page = null;
    do {
      page = campaignCriterionService.get(selector);
      //Location location = (Location)campaignCriterionService.get(selector);
      if (page.getEntries() != null) {
        // Display campaigns.
        for (CampaignCriterion campaignCriterion : page.getEntries()) {
       
        //Location loc = (Location)campaignCriterion.getCriterion();
        //System.out.println("hello"+loc.getLocationName());
       
          System.out.printf("Campaign criterion with campaign id '%s', criterion id '%s', "
              + "and type '%s' was found.\n", campaignCriterion.getCampaignId(), campaignCriterion
              .getCriterion().getId(), campaignCriterion.getCriterion().getCriterionType());
          
        }
        Thread.sleep(1000);
      } else {
        System.out.println("No campaign criteria were found.");
      }
      offset += PAGE_SIZE;
      selector = builder.increaseOffsetBy(PAGE_SIZE).build();
    } while (offset < page.getTotalNumEntries());

Hope you can help me with this problem.. thanks in advance.

Umesh Dengale

unread,
Aug 27, 2015, 2:33:00 PM8/27/15
to AdWords API Forum
Hello,

In the above code, the campaignCriterion.getCriterion().getId() will get the CriterionId.  You could use the LocationCriteriaService to get the name of the city or country associated with the CriterionId or you could download city (Cities-DMA Regions) and country (Geographical Targeting) codes as .csv. You could store it in your database and get the city/country names by querying your database when needed.

Cheers,
Umesh, Adwords API Team. 

John Geliberte

unread,
Aug 27, 2015, 8:32:35 PM8/27/15
to AdWords API Forum
Hi Umesh, can you please provide an example on how to use the Location Criteria Service? Im having a difficult time working on that service. thank you

Umesh Dengale

unread,
Aug 28, 2015, 10:30:04 AM8/28/15
to AdWords API Forum
Hi John,

You could use the LookupLocation Java client library example to get the locations from the CriterionId using the LocationCriteriaService. In this example you could use predicate Id (CriterionId) instead of location (e.g. .in("Id", "1006094","9059985") ). You could find the other language example here.

Cheers,
Umesh, AdWords API Team.
Reply all
Reply to author
Forward
0 new messages