Skip to content

Commit

Permalink
feat(cdp): add meta ads hog function (#25552)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarconLP authored Oct 14, 2024
1 parent 07218a5 commit 3208def
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
Binary file added frontend/public/services/meta-ads.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 @@ -23,6 +23,7 @@
from .engage.template_engage import template as engage, TemplateEngageMigrator
from .zendesk.template_zendesk import template as zendesk
from .knock.template_knock import template as knock
from .meta_ads.template_meta_ads import template as meta_ads
from .activecampaign.template_activecampaign import template as activecampaign
from .google_cloud_storage.template_google_cloud_storage import (
template as google_cloud_storage,
Expand Down Expand Up @@ -50,6 +51,7 @@
mailgun,
mailjet_create_contact,
mailjet_update_contact_list,
meta_ads,
posthog,
rudderstack,
salesforce_create,
Expand Down
133 changes: 133 additions & 0 deletions posthog/cdp/templates/meta_ads/template_meta_ads.py
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,
},
)
47 changes: 47 additions & 0 deletions posthog/cdp/templates/meta_ads/test_template_meta_ads.py
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",
},
},
)
)

0 comments on commit 3208def

Please sign in to comment.