-
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 loops hog function (#24713)
- Loading branch information
Showing
5 changed files
with
116 additions
and
4 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,49 @@ | ||
from posthog.cdp.templates.hog_function_template import HogFunctionTemplate | ||
|
||
|
||
template: HogFunctionTemplate = HogFunctionTemplate( | ||
status="beta", | ||
id="template-loops", | ||
name="Send events to Loops", | ||
description="Passes PostHog events to Loops.so", | ||
icon_url="/static/services/loops.png", | ||
hog=""" | ||
let apiKey := inputs.apiKey | ||
let payload := { | ||
'userId': event.distinct_id, | ||
'eventName': event.name == '$set' ? '$identify' : event.name, | ||
'email': person.properties.email | ||
} | ||
for (let key, value in person.properties) { | ||
payload[key] := value | ||
} | ||
fetch('https://app.loops.so/api/v1/events/send', { | ||
'method': 'POST', | ||
'headers': { | ||
'Content-Type': 'application/json', | ||
'Authorization': f'Bearer {apiKey}', | ||
}, | ||
'body': payload | ||
}) | ||
""".strip(), | ||
inputs_schema=[ | ||
{ | ||
"key": "apiKey", | ||
"type": "string", | ||
"label": "Loops API Key", | ||
"description": "Loops API Key", | ||
"default": "", | ||
"secret": True, | ||
"required": True, | ||
} | ||
], | ||
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,61 @@ | ||
from inline_snapshot import snapshot | ||
from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest | ||
from posthog.cdp.templates.loops.template_loops import template as template_loops | ||
|
||
|
||
class TestTemplateLoops(BaseHogFunctionTemplateTest): | ||
template = template_loops | ||
|
||
def _inputs(self, **kwargs): | ||
inputs = {"apiKey": "1cac089e00a708680bdb1ed9f082d5bf"} | ||
inputs.update(kwargs) | ||
return inputs | ||
|
||
def test_function_works(self): | ||
self.run_function( | ||
inputs=self._inputs(), | ||
globals={ | ||
"event": {"distinct_id": "66e614bd-d9f2-491e-9e2c-eeab3090f72f", "name": "$pageview"}, | ||
"person": { | ||
"properties": {"email": "[email protected]", "name": "Max", "company": "PostHog"}, | ||
}, | ||
}, | ||
) | ||
|
||
assert self.get_mock_fetch_calls()[0] == snapshot( | ||
( | ||
"https://app.loops.so/api/v1/events/send", | ||
{ | ||
"method": "POST", | ||
"headers": { | ||
"Content-Type": "application/json", | ||
"Authorization": "Bearer 1cac089e00a708680bdb1ed9f082d5bf", | ||
}, | ||
"body": { | ||
"userId": "66e614bd-d9f2-491e-9e2c-eeab3090f72f", | ||
"eventName": "$pageview", | ||
"email": "[email protected]", | ||
"name": "Max", | ||
"company": "PostHog", | ||
}, | ||
}, | ||
) | ||
) | ||
|
||
def test_automatic_action_mapping(self): | ||
for event_name, expected_action in [ | ||
("$identify", "$identify"), | ||
("$set", "$identify"), | ||
("$pageview", "$pageview"), | ||
("$create_alias", "$create_alias"), | ||
("$autocapture", "$autocapture"), | ||
("custom", "custom"), | ||
]: | ||
self.run_function( | ||
inputs=self._inputs(), | ||
globals={ | ||
"event": {"name": event_name, "properties": {"url": "https://example.com", "$browser": "Chrome"}}, | ||
}, | ||
) | ||
|
||
assert self.get_mock_fetch_calls()[0][1]["body"]["eventName"] == expected_action |
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