maraujo / pySocialWatcher

Social Watcher on Facebook Marketing API
MIT License
109 stars 49 forks source link

Trouble with "and" behavior queries #10

Closed ianbstewart closed 6 years ago

ianbstewart commented 6 years ago

This isn't really an issue as much as me not understanding how "and" queries work. I'm trying to collect data for all Hispanic ex-pats from Mexico with the following query:

{   "name": "Hispanic ex-pats",
    "behavior": [
        {"and" : [6003133212372, 6003277229371], "name" : "Hispanic Mexican Ex-pats"}
    ],
     "geo_locations" : 
     [
        {
            "name" : "countries",
            "location_types" : ["home"],
            "values" : [
                "US"
            ]
        }
     ],
     "ages_ranges": [
        {"min":18, "max" : 25}
    ],
    "genders": [0]
}

But this generates an invalid query (according to Facebook). Do you know whether the query is written wrong?

maraujo commented 6 years ago

The "and" query gets the response from Facebook server of people who have BOTH behaviors. The "or" query gets the response from Facebook server of people who have one, or other or BOTH behaviors.

Matheus Araújo M.Sc in Computer Science Pursuing Ph.D. at the University of Minnesota.

On Wed, May 23, 2018 at 3:07 PM, Ian notifications@github.com wrote:

This isn't really an issue as much as me not understanding how "and" queries work. I'm trying to collect data for all Hispanic ex-pats from Mexico with the following query:

{ "name": "Hispanic ex-pats", "behavior": [ {"and" : [6003133212372, 6003277229371], "name" : "Hispanic Mexican Ex-pats"} ], "geo_locations" : [ { "name" : "countries", "location_types" : ["home"], "values" : [ "US" ] } ], "ages_ranges": [ {"min":18, "max" : 25} ], "genders": [0] }

But this generates an invalid query (according to Facebook). Do you know whether the query is written wrong?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/maraujo/pySocialWatcher/issues/10, or mute the thread https://github.com/notifications/unsubscribe-auth/AH2sobtGt-D-C0_WEqKk6fKvpr8m1gCaks5t1W0qgaJpZM4UKhjX .

ianbstewart commented 6 years ago

Right. I understand the difference between "and" and "or" but I can't figure out how to write the JSON query to issue an "and" query.

ianbstewart commented 6 years ago

I added a hacky fix to utils.select_advance_targeting_type_array_ids:

        if input_value.has_key("and"):
            for id_and in input_value["and"]:
                ## TODO: make the behavior AND query request less hacky
                if(segment_type == constants.INPUT_BEHAVIOR_FIELD):
                    if(len(targeting['flexible_spec']) == 1):
                        targeting['flexible_spec'].append({api_field_name : []})
                    targeting['flexible_spec'][1][api_field_name].append({"id" : id_and})
                else:
                    targeting["flexible_spec"].append({segment_type: {"id" : id_and}})
joaopalotti commented 6 years ago

Hi there,

I run into problems with the AND in the behavior field as well.

When using the json provided by @ianbstewart and the current GitHub version (which included this fix), I get the following error:

File "/home/palotti/p2/local/lib/python2.7/site-packages/pysocialwatcher-0.1j-py2.7.egg/pysocialwatcher/utils.py", line 375, in select_advance_targeting_type_array_ids targeting['flexible_spec'][1][api_field_name].append({"id" : id_and}) IndexError: list index out of range

Do you guys get this error as well?

I suggest the following modification instead: https://github.com/joaopalotti/pySocialWatcher/commit/bff9fe0b89ed911b01f74dc255ce9e9c7d56705b

(Now it does work here, but it probably needs to be tested elsewhere).

Cheers,

Joao