Solved

[Dashboard REST API] event filtration

  • 16 June 2021
  • 7 replies
  • 779 views

Badge

Hi all, just stacked with a small problem.

This piece of code works perfect, but if I uncomment filter clause I got an “HTTP 400: Bad Request”. Same as in documentation, but error =(

params = {
"event_type":"registerComplete",
# "filters": [
# {
# "subprop_type": "event",
# "subprop_key": "EmailVerified",
# "subprop_op": "is",
# "subprop_value": ["true"]
# }
# ]
}

dates = '&start=20210615&end=20210615'
url = SEGMENT_URI + json.dumps(params) + dates

response = requests.get(url, auth = (api_android, secret_android))

Thanks.

icon

Best answer by Denis Holmes 25 June 2021, 11:49

View original

7 replies

Userlevel 7
Badge +10

Hey @alex_t 
Assuming you are using the https://amplitude.com/api/2/events/segmentation end point here, I don’t see the required event parameter ‘e’ being used in this code snippet and only the start and end parameters. Could I be missing some of the context in this snippet?

 

An alternate way to format this request would be to build the individual required parameters using a dictionary for the params.

params_with_filter = {
'e': '{"event_type":"registerComplete","filters":[{"subprop_type":"event","subprop_key":"EmailVerified","subprop_op":"is","subprop_value":["true"]}]}',
'start': '20210615',
'end': '20210615'
}

params_without_filter = {
'e': '{"event_type":"registerComplete"}',
'start': '20210615',
'end': '20210615'
}



url = 'https://amplitude.com/api/2/events/segmentation'

r = requests.get(url, params = params_without_filter, auth = (api_android, secret_android))

 

Let me know if this code helps.

Badge

Hi, Saish.

The code you’ve provided returns an error

params_with_filter = {
'e': '{"event_type":"registerComplete","filters":[{"subprop_type":"event","subprop_key":"EmailVerified","subprop_op":"is","subprop_value":["true"]}]}',
'start': '20210615',
'end': '20210615'
}

url = 'https://amplitude.com/api/2/events/segmentation'

response = requests.get(url, params = params_with_filter, auth = (api_android, secret_android))

And result is:

<Response [400]>
HTTP 400: Bad Request

 

I was trying several ways to pass data and didn’t provide full listing, but as I mentioned:

without filter clause - it works and I get an json with data

with filter clause - not

 

 

Userlevel 5
Badge +3

Hi all, just stacked with a small problem.

This piece of code works perfect, but if I uncomment filter clause I got an “HTTP 400: Bad Request”. Same as in documentation, but error =(

params = {
"event_type":"registerComplete",
# "filters": [
# {
# "subprop_type": "event",
# "subprop_key": "EmailVerified",
# "subprop_op": "is",
# "subprop_value": ["true"]
# }
# ]
}

dates = '&start=20210615&end=20210615'
url = SEGMENT_URI + json.dumps(params) + dates

response = requests.get(url, auth = (api_android, secret_android))

Thanks.

Do you have an event property called EmailVerified under your registerComplete event?

Userlevel 5
Badge +3

Another thing to check would be to delete and re-type your quotation marks (“).

They are notorious for getting switched to a wrong version (there are several different versions that look pretty much the same, but are actually different characters) when copy-pasting from one program to another.

Userlevel 6
Badge +8

Hi @alex_t , thanks for writing into the Community! Denis here and I’ll be happy to help.

 

A big thank you to our star repliers @Saish Redkar and @MikkoKarvonen , you both continue to amaze with your excellent knowledge sharing and insight. I appreciate that massively guys!!

As mentioned by @Saish Redkar , I would ensure that that ?e= is added at the end of your base URL. So your base URL might look something like this

url = "https://amplitude.com/api/2/events/segmentation?e=" + json.dumps(params) + dates

Can you ensure that the ?e= is added to your SEGMENT_URI? I would add it there and to the actual params as sometimes that might cause parsing and formatting issues. 

As mentioned by @MikkoKarvonen , I would ensure that you have written in the double quotations again. These can cause issues when pasting from one place to another. 

As Mikko also said, do you have this event registerComplete in your organization? And is it spelled the exact same way with no spaces and camelcase? 

Is the property you are filtering on, EmailVerified, also spelled the same way and is an event property? If it is a user property, the subprop_type must be user but if it is an event property, subprop_type of event should work. I have attached a simple event from my own organization that I am hoping can guide you with your instrumentation. I cannot include my keys for safety reason but hopefully you will be able to work with it and get your own working. 

params = {    "event_type":"AAA",     "filters":[         {             "subprop_type": "user",             "subprop_key": "City",             "subprop_op": "is",             "subprop_value": ["Amsterdam"]         }]}dates = '&start=20210608&end=20210608'url = "https://amplitude.com/api/2/events/segmentation?e=" + json.dumps(params) + datesresponse = requests.get(url,params=params,auth=("dc07d85f595a149f933f58fa3394e4f6", "5bc9b786c1dd18dc46e530040972d309"))print(response.json())

 

@alex_t If your API call still isn’t working after checking these steps, can you please send me a private message and I will help you out with the API call. Thank you!

Userlevel 7
Badge +10

Hey @alex_t ,

@Denis Holmes and @MikkoKarvonen pretty much cover the additional methods which could help in debugging your issue. Happy to share out a working code snippet via Github if that helps out further. 

Userlevel 6
Badge +8

Hi all!

 

It seems like the user property was causing the issue. When we use a custom user property to filter on, we have to use gp: before it. So, the chart in question would have looked like the following :

curl -u API_KEY:SECRET_KEY 'https://amplitude.com/api/2/events/segmentation?e=\{"event_type":"registerComplete","filters":\[\{"subprop_type":"user","subprop_key":"gp:country","subprop_op":"is","subprop_value":\["(none)"\]\}\]\}&start=20210608&end=20210608'
 

So, we must use a gp: before the event name when we use a custom event. I hope this explains the situation for everyone ! If there are any problems with it, feel free to reply/comment beneath!

 

Have a great weekend all!

Reply