Skip to content

Commit

Permalink
feat(cdp): add gleap.io hog function (#24884)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarconLP authored Sep 11, 2024
1 parent 1a769ff commit a09635d
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
Binary file added frontend/public/services/gleap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions posthog/cdp/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .avo.template_avo import template as avo
from .loops.template_loops import template as loops
from .rudderstack.template_rudderstack import template as rudderstack
from .gleap.template_gleap import template as gleap


HOG_FUNCTION_TEMPLATES = [
Expand All @@ -38,6 +39,7 @@
loops,
rudderstack,
avo,
gleap,
]


Expand Down
91 changes: 91 additions & 0 deletions posthog/cdp/templates/gleap/template_gleap.py
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,
},
)
82 changes: 82 additions & 0 deletions posthog/cdp/templates/gleap/test_template_gleap.py
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...",)])

0 comments on commit a09635d

Please sign in to comment.