-
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 engage hog function (#24917)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3287258
commit 8f31adc
Showing
5 changed files
with
121 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,82 @@ | ||
from posthog.cdp.templates.hog_function_template import HogFunctionTemplate, HogFunctionTemplateMigrator | ||
from copy import deepcopy | ||
import dataclasses | ||
|
||
template: HogFunctionTemplate = HogFunctionTemplate( | ||
status="beta", | ||
id="template-engage-so", | ||
name="Send events to Engage.so", | ||
description="Send events to Engage.so", | ||
icon_url="/static/services/engage.png", | ||
hog=""" | ||
let body := event | ||
body['event'] := event.name | ||
fetch('https://api.engage.so/posthog', { | ||
'method': 'POST', | ||
'headers': { | ||
'Authorization': f'Basic {base64Encode(f'{inputs.public_key}:{inputs.private_key}')}', | ||
'Content-Type': 'application/json' | ||
}, | ||
'body': body | ||
}) | ||
""".strip(), | ||
inputs_schema=[ | ||
{ | ||
"key": "public_key", | ||
"type": "string", | ||
"label": "Public key", | ||
"description": "Get your public key from your Engage dashboard (Settings -> Account)", | ||
"secret": True, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "private_key", | ||
"type": "string", | ||
"label": "Private key", | ||
"description": "Get your private key from your Engage dashboard (Settings -> Account)", | ||
"secret": True, | ||
"required": True, | ||
}, | ||
], | ||
filters={ | ||
"events": [ | ||
{"id": "$identify", "name": "$identify", "type": "events", "order": 0}, | ||
{"id": "$set", "name": "$set", "type": "events", "order": 1}, | ||
{"id": "$groupidentify", "name": "$groupidentify", "type": "events", "order": 2}, | ||
{"id": "$unset", "name": "$unset", "type": "events", "order": 3}, | ||
{"id": "$create_alias", "name": "$create_alias", "type": "events", "order": 4}, | ||
], | ||
"actions": [], | ||
"filter_test_accounts": True, | ||
}, | ||
) | ||
|
||
|
||
class TemplateEngageMigrator(HogFunctionTemplateMigrator): | ||
plugin_url = "https://github.com/PostHog/posthog-engage-so-plugin" | ||
|
||
@classmethod | ||
def migrate(cls, obj): | ||
hf = deepcopy(dataclasses.asdict(template)) | ||
|
||
public_key = obj.config.get("publicKey", "") | ||
private_key = obj.config.get("secret", "") | ||
|
||
hf["filters"] = {} | ||
|
||
hf["filters"]["events"] = [ | ||
{"id": "$identify", "name": "$identify", "type": "events", "order": 0}, | ||
{"id": "$set", "name": "$set", "type": "events", "order": 1}, | ||
{"id": "$groupidentify", "name": "$groupidentify", "type": "events", "order": 2}, | ||
{"id": "$unset", "name": "$unset", "type": "events", "order": 3}, | ||
{"id": "$create_alias", "name": "$create_alias", "type": "events", "order": 4}, | ||
] | ||
|
||
hf["inputs"] = { | ||
"public_key": {"value": public_key}, | ||
"private_key": {"value": private_key}, | ||
} | ||
|
||
return hf |
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,32 @@ | ||
from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest | ||
from posthog.cdp.templates.engage.template_engage import template as template_engage | ||
|
||
|
||
class TestTemplateEngageso(BaseHogFunctionTemplateTest): | ||
template = template_engage | ||
|
||
def _inputs(self, **kwargs): | ||
inputs = {"public_key": "PUBLIC_KEY", "private_key": "PRIVATE_KEY"} | ||
inputs.update(kwargs) | ||
return inputs | ||
|
||
def test_function_works(self): | ||
res = self.run_function(inputs=self._inputs()) | ||
|
||
assert res.result is None | ||
|
||
event = self.createHogGlobals()["event"] | ||
|
||
event["event"] = event["name"] | ||
|
||
assert self.get_mock_fetch_calls()[0] == ( | ||
"https://api.engage.so/posthog", | ||
{ | ||
"method": "POST", | ||
"headers": { | ||
"Authorization": "Basic UFVCTElDX0tFWTpQUklWQVRFX0tFWQ==", | ||
"Content-Type": "application/json", | ||
}, | ||
"body": event, | ||
}, | ||
) |
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