How to manipulate an object of Google Ads API's Enum class - python

Viewed 2339

I am using the python client library to connect to Google Ads's API.

    ga_service = client_service.get_service('GoogleAdsService')
    query = ('SELECT campaign.id, campaign.name, campaign.advertising_channel_type '
            'FROM campaign WHERE date BETWEEN \''+fecha+'\' AND \''+fecha+'\'')

    response = ga_service.search(<client_id>, query=query,page_size=1000)
    result = {}
    result['campanas'] = []

    try:
        for row in response:
            print row
            info = {}
            info['id'] = row.campaign.id.value
            info['name'] = row.campaign.name.value
            info['type'] = row.campaign.advertising_channel_type

When I parse the values this is the result I get:

{
  "campanas": [
    {
      "id": <campaign_id>, 
      "name": "Lanzamiento SIKU", 
      "type": 2
    }, 
    {
      "id": <campaign_id>, 
      "name": "lvl1 - website traffic", 
      "type": 2
    }, 
    {
      "id": <campaign_id>, 
      "name": "Lvl 2 - display", 
      "type": 3
    }
  ]
}

Why am I getting an integer for result["type"] ? When I check the traceback call I can see a string:

campaign {
  resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
  id {
    value: 397083380
  }
  name {
    value: "Lanzamiento SIKU"
  }
  advertising_channel_type: SEARCH
}

campaign {
  resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
  id {
    value: 1590766475
  }
  name {
    value: "lvl1 - website traffic"
  }
  advertising_channel_type: SEARCH
}

campaign {
  resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
  id {
    value: 1590784940
  }
  name {
    value: "Lvl 2 - display"
  }
  advertising_channel_type: DISPLAY
}

I've searched on the Documentation for the API and found out that it's because the field: advertising_channel_type is of Data Type: Enum. How can I manipulate this object of the Enum class to get the string value? There is no helpful information about this on their Documentation.

Please help !!

4 Answers

The Enum's come with some methods to translate between index and string

channel_types = client_service.get_type('AdvertisingChannelTypeEnum')

channel_types.AdvertisingChannelType.Value('SEARCH')
# => 2
channel_types.AdvertisingChannelType.Name(2)
# => 'SEARCH'

This was found by looking at docstrings, e.g.

channel_types.AdvertisingChannelType.__doc__
# => 'A utility for finding the names of enum values.'

I think the best way to do this is this one liner code:

import proto
row_dict = proto.Message.to_dict(google_ads_row, use_integers_for_enums=False)

This will convert the entire google ads row into a dictionary in just one go and automatically get the ENUM values instead of the numbers.

@Vijaysinh Parmar try following

from google.protobuf import json_format

row_dict = json_format.MessageToJson(row, use_integers_for_enums=False)

Just work around it by, create a list

lookup_list = ['DISPLAY', 'HOTEL', 'SEARCH', 'SHOPPING', 'UNKNOWN', 'UNSPECIFIED', 'VIDEO']

and change the assignment in your last row to

info['type'] = lookup_list[row.campaign.advertising_channel_type]
Related