-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdp): add gleap.io hog function (#24884)
- Loading branch information
Showing
4 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from posthog.cdp.templates.hog_function_template import HogFunctionTemplate | ||
|
||
template: HogFunctionTemplate = HogFunctionTemplate( | ||
status="beta", | ||
id="template-gleap", | ||
name="Add contacts to Gleap", | ||
description="Updates a contact in Gleap", | ||
icon_url="/static/services/gleap.png", | ||
hog=""" | ||
let action := inputs.action | ||
let name := event.name | ||
if (empty(inputs.userId)) { | ||
print('No User ID set. Skipping...') | ||
return | ||
} | ||
let attributes := inputs.include_all_properties ? person.properties : {} | ||
attributes['userId'] := inputs.userId | ||
for (let key, value in inputs.attributes) { | ||
if (not empty(value)) { | ||
attributes[key] := value | ||
} | ||
} | ||
let res := fetch(f'https://api.gleap.io/admin/identify', { | ||
'method': 'POST', | ||
'headers': { | ||
'User-Agent': 'PostHog Gleap.io App', | ||
'Api-Token': inputs.apiKey, | ||
'Content-Type': 'application/json' | ||
}, | ||
'body': attributes | ||
}) | ||
if (res.status >= 400) { | ||
print('Error from gleap.io api:', res.status, res.body) | ||
} | ||
""".strip(), | ||
inputs_schema=[ | ||
{ | ||
"key": "apiKey", | ||
"type": "string", | ||
"label": "Gleap.io API Key", | ||
"secret": True, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "userId", | ||
"type": "string", | ||
"label": "User ID", | ||
"description": "You can choose to fill this from an `email` property or an `id` property. If the value is empty nothing will be sent. See here for more information: https://docs.gleap.io/server/rest-api", | ||
"default": "{person.uuid}", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "include_all_properties", | ||
"type": "boolean", | ||
"label": "Include all properties as attributes", | ||
"description": "If set, all person properties will be included as attributes. Individual attributes can be overridden below.", | ||
"default": False, | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "attributes", | ||
"type": "dictionary", | ||
"label": "Attribute mapping", | ||
"description": "Map of Gleap.io attributes and their values. You can use the filters section to filter out unwanted events.", | ||
"default": { | ||
"email": "{person.properties.email}", | ||
"name": "{person.properties.name}", | ||
"phone": "{person.properties.phone}", | ||
}, | ||
"secret": False, | ||
"required": False, | ||
}, | ||
], | ||
filters={ | ||
"events": [ | ||
{"id": "$identify", "name": "$identify", "type": "events", "order": 0}, | ||
{"id": "$set", "name": "$set", "type": "events", "order": 1}, | ||
], | ||
"actions": [], | ||
"filter_test_accounts": True, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from inline_snapshot import snapshot | ||
from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest | ||
from posthog.cdp.templates.gleap.template_gleap import ( | ||
template as template_gleap, | ||
) | ||
|
||
|
||
def create_inputs(**kwargs): | ||
inputs = { | ||
"apiKey": "uB6Jymn60NN5EEIWgiUzZx13geVlEx26", | ||
"include_all_properties": False, | ||
"userId": "edad9282-25d0-4cf1-af0e-415535ee1161", | ||
"attributes": {"name": "example", "email": "[email protected]"}, | ||
} | ||
inputs.update(kwargs) | ||
|
||
return inputs | ||
|
||
|
||
class TestTemplateGleap(BaseHogFunctionTemplateTest): | ||
template = template_gleap | ||
|
||
def test_function_works(self): | ||
self.run_function( | ||
inputs=create_inputs(), | ||
globals={ | ||
"event": {"name": "$identify"}, | ||
}, | ||
) | ||
|
||
assert self.get_mock_fetch_calls()[0] == snapshot( | ||
( | ||
"https://api.gleap.io/admin/identify", | ||
{ | ||
"method": "POST", | ||
"headers": { | ||
"User-Agent": "PostHog Gleap.io App", | ||
"Api-Token": "uB6Jymn60NN5EEIWgiUzZx13geVlEx26", | ||
"Content-Type": "application/json", | ||
}, | ||
"body": { | ||
"userId": "edad9282-25d0-4cf1-af0e-415535ee1161", | ||
"name": "example", | ||
"email": "[email protected]", | ||
}, | ||
}, | ||
) | ||
) | ||
|
||
def test_body_includes_all_properties_if_set(self): | ||
self.run_function( | ||
inputs=create_inputs(include_all_properties=False), | ||
globals={ | ||
"person": {"properties": {"account_status": "paid"}}, | ||
}, | ||
) | ||
|
||
assert self.get_mock_fetch_calls()[0][1]["body"] == snapshot( | ||
{"userId": "edad9282-25d0-4cf1-af0e-415535ee1161", "name": "example", "email": "[email protected]"} | ||
) | ||
|
||
self.run_function( | ||
inputs=create_inputs(include_all_properties=True), | ||
globals={ | ||
"person": {"properties": {"account_status": "paid"}}, | ||
}, | ||
) | ||
|
||
assert self.get_mock_fetch_calls()[0][1]["body"] == snapshot( | ||
{ | ||
"userId": "edad9282-25d0-4cf1-af0e-415535ee1161", | ||
"account_status": "paid", | ||
"name": "example", | ||
"email": "[email protected]", | ||
} | ||
) | ||
|
||
def test_function_requires_identifier(self): | ||
self.run_function(inputs=create_inputs(userId="")) | ||
|
||
assert not self.get_mock_fetch_calls() | ||
assert self.get_mock_print_calls() == snapshot([("No User ID set. Skipping...",)]) |