-
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 meta ads hog function (#25552)
- Loading branch information
Showing
4 changed files
with
182 additions
and
0 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,133 @@ | ||
from posthog.cdp.templates.hog_function_template import HogFunctionTemplate | ||
|
||
template: HogFunctionTemplate = HogFunctionTemplate( | ||
status="alpha", | ||
id="template-meta-ads", | ||
name="Google Ads Conversions", | ||
description="Send conversion events to Meta Ads", | ||
icon_url="/static/services/meta-ads.png", | ||
category=["Advertisement"], | ||
hog=""" | ||
let res := fetch(f'https://graph.facebook.com/v21.0/{inputs.pixelId}/events', { | ||
'method': 'POST', | ||
'headers': { | ||
'Content-Type': 'application/json', | ||
}, | ||
'body': { | ||
'data': [ | ||
{ | ||
'event_name': inputs.eventName, | ||
'event_time': inputs.eventTime, | ||
'action_source': inputs.actionSource, | ||
'user_data': inputs.userData | ||
} | ||
], | ||
'access_token': inputs.accessToken | ||
} | ||
}) | ||
if (res.status >= 400) { | ||
print('Error from graph.facebook.com api:', res.status, res.body) | ||
} | ||
""".strip(), | ||
inputs_schema=[ | ||
{ | ||
"key": "accessToken", | ||
"type": "string", | ||
"label": "Access token", | ||
"description": "Check out this page on how to obtain such a token: https://developers.facebook.com/docs/marketing-api/conversions-api/get-started", | ||
"secret": True, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "pixelId", | ||
"type": "string", | ||
"label": "Pixel ID", | ||
"description": "You must obtain a Pixel ID to use the Conversions API. If you’ve already set up a Pixel for your website, we recommend that you use the same Pixel ID for your browser and server events.", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "eventName", | ||
"type": "string", | ||
"label": "Event name", | ||
"description": "A standard event or custom event name.", | ||
"default": "{event.event}", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "eventTime", | ||
"type": "string", | ||
"label": "Event time", | ||
"description": "A Unix timestamp in seconds indicating when the actual event occurred. You must send this date in GMT time zone.", | ||
"default": "{toInt(toUnixTimestamp(event.timestamp))}", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "actionSource", | ||
"label": "Action source", | ||
"type": "choice", | ||
"choices": [ | ||
{ | ||
"label": "Email - Conversion happened over email.", | ||
"value": "email", | ||
}, | ||
{ | ||
"label": "Website - Conversion was made on your website.", | ||
"value": "website", | ||
}, | ||
{ | ||
"label": "App - Conversion was made on your mobile app.", | ||
"value": "app", | ||
}, | ||
{ | ||
"label": "Phone call - Conversion was made over the phone.", | ||
"value": "phone_call", | ||
}, | ||
{ | ||
"label": "Chat - Conversion was made via a messaging app, SMS, or online messaging feature.", | ||
"value": "chat", | ||
}, | ||
{ | ||
"label": "Physical store - Conversion was made in person at your physical store.", | ||
"value": "physical_store", | ||
}, | ||
{ | ||
"label": "System generated - Conversion happened automatically, for example, a subscription renewal that’s set to auto-pay each month.", | ||
"value": "system_generated", | ||
}, | ||
{ | ||
"label": "Business messaging - Conversion was made from ads that click to Messenger, Instagram or WhatsApp.", | ||
"value": "business_messaging", | ||
}, | ||
{ | ||
"label": "Other - Conversion happened in a way that is not listed.", | ||
"value": "other", | ||
}, | ||
], | ||
"description": "This field allows you to specify where your conversions occurred. Knowing where your events took place helps ensure your ads go to the right people.", | ||
"default": "website", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "userData", | ||
"type": "dictionary", | ||
"label": "User data", | ||
"description": "A map that contains customer information data. See this page for options: https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/customer-information-parameters", | ||
"default": { | ||
"em": "{sha256Hex(person.properties.email)}", | ||
"fn": "{sha256Hex(person.properties.first_name)}", | ||
"ln": "{sha256Hex(person.properties.last_name)}", | ||
}, | ||
"secret": False, | ||
"required": True, | ||
}, | ||
], | ||
filters={ | ||
"events": [], | ||
"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,47 @@ | ||
from inline_snapshot import snapshot | ||
from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest | ||
from posthog.cdp.templates.meta_ads.template_meta_ads import ( | ||
template as template_meta_ads, | ||
) | ||
|
||
|
||
class TestTemplateMetaAds(BaseHogFunctionTemplateTest): | ||
template = template_meta_ads | ||
|
||
def _inputs(self, **kwargs): | ||
inputs = { | ||
"accessToken": "accessToken12345", | ||
"pixelId": "123451234512345", | ||
"eventName": "checkout", | ||
"eventTime": "1728812163", | ||
"actionSource": "website", | ||
"userData": {"em": "3edfaed7454eedb3c72bad566901af8bfbed1181816dde6db91dfff0f0cffa98"}, | ||
} | ||
inputs.update(kwargs) | ||
return inputs | ||
|
||
def test_function_works(self): | ||
self.mock_fetch_response = lambda *args: {"status": 200, "body": {"ok": True}} # type: ignore | ||
self.run_function(self._inputs()) | ||
assert self.get_mock_fetch_calls()[0] == snapshot( | ||
( | ||
"https://graph.facebook.com/v21.0/123451234512345/events", | ||
{ | ||
"body": { | ||
"access_token": "accessToken12345", | ||
"data": [ | ||
{ | ||
"event_name": "checkout", | ||
"event_time": "1728812163", | ||
"action_source": "website", | ||
"user_data": {"em": "3edfaed7454eedb3c72bad566901af8bfbed1181816dde6db91dfff0f0cffa98"}, | ||
} | ||
], | ||
}, | ||
"method": "POST", | ||
"headers": { | ||
"Content-Type": "application/json", | ||
}, | ||
}, | ||
) | ||
) |