Skip to content

Commit

Permalink
feat(cdp): add engage hog function (#24917)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
MarconLP and github-actions[bot] authored Sep 13, 2024
1 parent 3287258 commit 8f31adc
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 4 deletions.
Binary file added frontend/public/services/engage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions posthog/cdp/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .rudderstack.template_rudderstack import template as rudderstack
from .gleap.template_gleap import template as gleap
from .google_pubsub.template_google_pubsub import template as google_pubsub, TemplateGooglePubSubMigrator
from .engage.template_engage import template as engage, TemplateEngageMigrator
from .zendesk.template_zendesk import template as zendesk
from .google_cloud_storage.template_google_cloud_storage import (
template as google_cloud_storage,
Expand Down Expand Up @@ -48,6 +49,7 @@
avo,
gleap,
google_pubsub,
engage,
zendesk,
google_cloud_storage,
braze,
Expand All @@ -62,6 +64,7 @@
TemplateSendGridMigrator.plugin_url: TemplateSendGridMigrator,
TemplateGooglePubSubMigrator.plugin_url: TemplateGooglePubSubMigrator,
TemplateGoogleCloudStorageMigrator.plugin_url: TemplateGoogleCloudStorageMigrator,
TemplateEngageMigrator.plugin_url: TemplateEngageMigrator,
TemplatePostHogMigrator.plugin_url: TemplatePostHogMigrator,
TemplateHubspotMigrator.plugin_url: TemplateHubspotMigrator,
TemplateLoopsMigrator.plugin_url: TemplateLoopsMigrator,
Expand Down
82 changes: 82 additions & 0 deletions posthog/cdp/templates/engage/template_engage.py
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
32 changes: 32 additions & 0 deletions posthog/cdp/templates/engage/test_template_engage.py
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,
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
FROM events LEFT JOIN (
SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id
FROM person_static_cohort
WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [13]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [1]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(events.team_id, 420), 1, ifNull(equals(__in_cohort.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, format_csv_allow_double_quotes=0, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0
Expand All @@ -42,7 +42,7 @@
FROM events LEFT JOIN (
SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
FROM static_cohort_people
WHERE in(cohort_id, [13])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE in(cohort_id, [1])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE and(1, equals(__in_cohort.matched, 1))
LIMIT 100
'''
Expand All @@ -55,7 +55,7 @@
FROM events LEFT JOIN (
SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id
FROM person_static_cohort
WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [14]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [2]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(events.team_id, 420), 1, ifNull(equals(__in_cohort.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, format_csv_allow_double_quotes=0, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0
Expand All @@ -66,7 +66,7 @@
FROM events LEFT JOIN (
SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
FROM static_cohort_people
WHERE in(cohort_id, [14])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE in(cohort_id, [2])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE and(1, equals(__in_cohort.matched, 1))
LIMIT 100
'''
Expand Down

0 comments on commit 8f31adc

Please sign in to comment.