0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-25 11:17:50 +01:00

feat(cdp): braze template (#24886)

This commit is contained in:
Marius Andra 2024-09-11 17:15:40 +02:00 committed by GitHub
parent 4c4bf8e777
commit 338e41269a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 133 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -1,6 +1,7 @@
from .webhook.template_webhook import template as webhook
from .slack.template_slack import template as slack
from .hubspot.template_hubspot import template as hubspot
from .braze.template_braze import template as braze
from .customerio.template_customerio import template as customerio, TemplateCustomerioMigrator
from .intercom.template_intercom import template as intercom, TemplateIntercomMigrator
from .sendgrid.template_sendgrid import template as sendgrid, TemplateSendGridMigrator
@ -42,6 +43,7 @@ HOG_FUNCTION_TEMPLATES = [
avo,
gleap,
google_pubsub,
braze,
]

View File

@ -0,0 +1,83 @@
from posthog.cdp.templates.hog_function_template import HogFunctionTemplate
template: HogFunctionTemplate = HogFunctionTemplate(
status="beta",
id="template-braze",
name="Send events to Braze",
description="Send events to Braze",
icon_url="/static/services/braze.png",
hog="""
let getPayload := () -> [{
'attributes': inputs.attributes,
'events': [inputs.event]
}]
let res := fetch(f'{inputs.brazeEndpoint}/users/track', {
'method': 'POST',
'headers': {
'Authorization': f'Bearer {inputs.apiKey}',
'Content-Type': 'application/json'
},
'body': getPayload()
})
if (res.status >= 200 and res.status < 300) {
print('Event sent successfully!')
} else {
throw Error(f'Error sending event: {res.status} {res.body}')
}
""".strip(),
inputs_schema=[
{
"key": "brazeEndpoint",
"type": "choice",
"label": "Braze REST Endpoint",
"description": "The endpoint identifier where your Braze instance is located, see the docs here: https://www.braze.com/docs/api/basics",
"choices": [
{"label": "US-01", "value": "https://rest.iad-01.braze.com"},
{"label": "US-02", "value": "https://rest.iad-02.braze.com"},
{"label": "US-03", "value": "https://rest.iad-03.braze.com"},
{"label": "US-04", "value": "https://rest.iad-04.braze.com"},
{"label": "US-05", "value": "https://rest.iad-05.braze.com"},
{"label": "US-06", "value": "https://rest.iad-06.braze.com"},
{"label": "US-08", "value": "https://rest.iad-08.braze.com"},
{"label": "EU-01", "value": "https://rest.fra-01.braze.eu"},
{"label": "EU-02", "value": "https://rest.fra-02.braze.eu"},
],
"default": "",
"secret": False,
"required": True,
},
{
"key": "apiKey",
"type": "string",
"label": "Your Braze API Key",
"description": "See the docs here: https://www.braze.com/docs/api/api_key/",
"default": "",
"secret": True,
"required": True,
},
{
"key": "attributes",
"type": "json",
"label": "Attributes to set",
"default": {"email": "{person.properties.email}"},
"secret": False,
"required": True,
},
{
"key": "event",
"type": "json",
"label": "Event payload",
"default": {
"properties": "{event.properties}",
"external_id": "{event.distinct_id}",
"name": "{event.name}",
"time": "{event.timestamp}",
},
"secret": False,
"required": True,
},
],
)

View File

@ -0,0 +1,48 @@
from freezegun import freeze_time
from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest
from posthog.cdp.templates.braze.template_braze import template as template_braze
class TestTemplateBraze(BaseHogFunctionTemplateTest):
template = template_braze
@freeze_time("2024-04-16T12:34:51Z")
def test_function_works(self):
res = self.run_function(
inputs={
"brazeEndpoint": "https://rest.fra-01.braze.eu",
"apiKey": "my_secret_key",
"attributes": {"email": "{person.properties.email}"},
"event": {
"name": "{event.name}",
"time": "{event.timestamp}",
"properties": "{event.properties}",
"external_id": "{event.distinct_id}",
},
}
)
assert res.result is None
assert self.get_mock_fetch_calls()[0] == (
"https://rest.fra-01.braze.eu/users/track",
{
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer my_secret_key",
},
"body": [
{
"attributes": {"email": "{person.properties.email}"},
"events": [
{
"external_id": "{event.distinct_id}",
"name": "{event.name}",
"properties": "{event.properties}",
"time": "{event.timestamp}",
},
],
}
],
"method": "POST",
},
)