Skip to content
This repository was archived by the owner on Sep 1, 2021. It is now read-only.

Commit 655274d

Browse files
committed
Added clickthrough tracking
1 parent ecf3d1b commit 655274d

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,10 @@ The client can be configured to use a managed deploy by adjusting the `base_endp
213213
{'results': {'documents': [{'suggestion': 'cat'}]}, 'meta': {'request_id': '390be384ad5888353e1b32adcfaaf1c9'}}
214214
```
215215

216-
```ruby
217-
engine_name = 'favorite-videos'
218-
219-
options = {
220-
:size => 3,
221-
:types => {
222-
:documents => {
223-
:fields => ['title']
224-
}
225-
}
226-
}
216+
### Clickthrough Tracking
227217

228-
client.query_suggestion(engine_name, 'cat', options)
218+
```python
219+
>>> client.click(engine_name, {'query': 'cat', 'document_id': 'INscMGmhmX4'})
229220
```
230221

231222

swiftype_app_search/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,18 @@ def query_suggestion(self, engine_name, query, options=None):
200200
options['query'] = query
201201
return self.swiftype_session.request('get', endpoint, json=options)
202202

203+
def click(self, engine_name, options):
204+
"""
205+
Sends a click event to the Swiftype App Search Api, to track a click-through event.
206+
See https://swiftype.com/documentation/app-search/ for more details
207+
on options and return values.
208+
209+
:param engine_name: Name of engine to search over.
210+
:param options: Dict of search options.
211+
"""
212+
endpoint = "engines/{}/click".format(engine_name)
213+
return self.swiftype_session.request_ignore_response('post', endpoint, json=options)
214+
203215

204216
@staticmethod
205217
def create_signed_search_key(api_key, api_key_name, options):

swiftype_app_search/swiftype_request_session.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ def raise_if_error(self, response):
3333
response.raise_for_status()
3434

3535
def request(self, http_method, endpoint, base_url=None, **kwargs):
36+
return self.request_ignore_response(http_method, endpoint, base_url, **kwargs).json()
37+
38+
def request_ignore_response(self, http_method, endpoint, base_url=None, **kwargs):
3639
base_url = base_url or self.base_url
3740
url = "{}/{}".format(base_url, endpoint)
3841
response = self.session.request(http_method, url, **kwargs)
3942
self.raise_if_error(response)
40-
return response.json()
43+
return response

tests/test_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,12 @@ def test_query_suggestion(self):
242242
m.register_uri('GET', url, json=expected_return, status_code=200)
243243
response = self.client.query_suggestion(self.engine_name, query, {})
244244
self.assertEqual(response, expected_return)
245+
246+
def test_click(self):
247+
with requests_mock.Mocker() as m:
248+
url = "{}/{}".format(
249+
self.client.swiftype_session.base_url,
250+
"engines/{}/click".format(self.engine_name)
251+
)
252+
m.register_uri('POST', url, json={}, status_code=200)
253+
self.client.click(self.engine_name, {'query': 'cat', 'document_id': 'INscMGmhmX4'})

0 commit comments

Comments
 (0)