Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cdp): snapchat pixel #26890

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .run/PostHog.run.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="PostHog" type="Python.DjangoServer" factoryName="Django server">
<module name="posthog" />
<option name="ENV_FILES" value="" />
<option name="ENV_FILES" value="$PROJECT_DIR$/.env" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
Expand Down
Binary file added frontend/public/services/snapchat.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 frontend/src/lib/integrations/integrationsLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import IconGoogleCloudStorage from 'public/services/google-cloud-storage.png'
import IconHubspot from 'public/services/hubspot.png'
import IconSalesforce from 'public/services/salesforce.png'
import IconSlack from 'public/services/slack.png'
import IconSnapchat from 'public/services/snapchat.png'
import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'
import { urls } from 'scenes/urls'

Expand All @@ -24,6 +25,7 @@ const ICONS: Record<IntegrationKind, any> = {
'google-pubsub': IconGoogleCloud,
'google-cloud-storage': IconGoogleCloudStorage,
'google-ads': IconGoogleAds,
snapchat: IconSnapchat,
}

export const integrationsLogic = kea<integrationsLogicType>([
Expand Down
1 change: 1 addition & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3758,6 +3758,7 @@ export type IntegrationKind =
| 'google-pubsub'
| 'google-cloud-storage'
| 'google-ads'
| 'snapchat'

export interface IntegrationType {
id: number
Expand Down
4 changes: 4 additions & 0 deletions posthog/cdp/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
from ._siteapps.template_debug_posthog import template as debug_posthog
from ._internal.template_broadcast import template_new_broadcast as _broadcast
from ._internal.template_blank import blank_site_destination, blank_site_app
from .snapchat_ads.template_snapchat_ads import template as snapchat_ads
from .snapchat_ads.template_pixel import template_snapchat_pixel as snapchat_pixel
from ._transformations.template_pass_through import template as pass_through_transformation

HOG_FUNCTION_TEMPLATES = [
Expand Down Expand Up @@ -92,6 +94,8 @@
salesforce_create,
salesforce_update,
sendgrid,
snapchat_ads,
snapchat_pixel,
zapier,
zendesk,
early_access_features,
Expand Down
269 changes: 269 additions & 0 deletions posthog/cdp/templates/snapchat_ads/template_pixel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
from posthog.cdp.templates.hog_function_template import HogFunctionMappingTemplate, HogFunctionTemplate

common_inputs = [
{
"key": "eventProperties",
"type": "dictionary",
"description": "Map of Snapchat event attributes and their values. Check out this page for more details: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"label": "Event parameters",
"default": {
"price": "{toFloat(event.properties.price ?? event.properties.value ?? event.properties.revenue)}",
"currency": "{event.properties.currency}",
"item_ids": "{event.properties.item_ids}",
"item_category": "{event.properties.category}",
"description": "{event.properties.description}",
"search_string": "{event.properties.search_string}",
"number_items": "{toInt(event.properties.number_items ?? event.properties.quantity)}",
"payment_info_available": "{toInt(event.properties.payment_info_available)}",
"sign_up_method": "{event.properties.sign_up_method}",
"brands": "{event.properties.brands}",
"success": "{toInt(event.properties.success) in (0, 1) ? toInt(event.properties.success) : null}",
"transaction_id": "{event.properties.orderId ?? event.properties.transactionId ?? event.properties.transaction_id}",
"client_dedup_id": "{event.uuid}",
},
"secret": False,
"required": False,
},
]

template_snapchat_pixel: HogFunctionTemplate = HogFunctionTemplate(
status="client-side",
type="site_destination",
id="template-snapchat-pixel",
name="Snapchat Pixel",
description="Track how many Snapchat users interact with your website.",
icon_url="/static/services/snapchat.png",
category=["Advertisement"],
hog="""
// Adds window.snaptr and lazily loads the Snapchat Pixel script
function initSnippet() {
(function(e,t,n){if(e.snaptr)return;var a=e.snaptr=function()
{a.handleRequest?a.handleRequest.apply(a,arguments):a.queue.push(arguments)};
a.queue=[];var s='script';r=t.createElement(s);r.async=!0;
r.src=n;var u=t.getElementsByTagName(s)[0];
u.parentNode.insertBefore(r,u);})(window,document,
'https://sc-static.net/scevent.min.js');
}

export function onLoad({ inputs }) {
initSnippet();
let userProperties = {};
for (const [key, value] of Object.entries(inputs.userProperties)) {
if (value) {
userProperties[key] = value;
}
};
snaptr('init', inputs.pixelId, userProperties);
}
export function onEvent({ inputs }) {
let eventProperties = {};
for (const [key, value] of Object.entries(inputs.eventProperties)) {
if (value) {
eventProperties[key] = value;
}
};
snaptr('track', inputs.eventType, eventProperties);
}
""".strip(),
inputs_schema=[
{
"key": "pixelId",
"type": "string",
"label": "Pixel ID",
"description": "You must obtain a Pixel ID to use the Snapchat Pixel. 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.",
"default": "",
"secret": False,
"required": True,
},
{
"key": "userProperties",
"type": "dictionary",
"description": "Map of Snapchat user parameters and their values. Check out this page for more details: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"label": "User parameters",
"default": {
"user_email": "{person.properties.email}",
"user_phone_number": "{person.properties.phone}",
},
"secret": False,
"required": False,
},
],
mapping_templates=[
HogFunctionMappingTemplate(
name="Page Viewed",
include_by_default=True,
filters={"events": [{"id": "$pageview", "name": "Pageview", "type": "events"}]},
inputs_schema=[
{
"key": "eventType",
"type": "string",
"label": "Event Type",
"description": "Check out this page for possible event types: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"default": "PAGE_VIEW",
"required": True,
},
*common_inputs,
],
),
HogFunctionMappingTemplate(
name="Order Completed",
include_by_default=True,
filters={"events": [{"id": "Order Completed", "type": "events"}]},
inputs_schema=[
{
"key": "eventType",
"type": "string",
"label": "Event Type",
"description": "Check out this page for possible event types: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"default": "PURCHASE",
"required": True,
},
*common_inputs,
],
),
HogFunctionMappingTemplate(
name="Checkout Started",
include_by_default=True,
filters={"events": [{"id": "Checkout Started", "type": "events"}]},
inputs_schema=[
{
"key": "eventType",
"type": "string",
"label": "Event Type",
"description": "Check out this page for possible event types: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"default": "START_CHECKOUT",
"required": True,
},
*common_inputs,
],
),
HogFunctionMappingTemplate(
name="Product Added",
include_by_default=True,
filters={"events": [{"id": "Product Added", "type": "events"}]},
inputs_schema=[
{
"key": "eventType",
"type": "string",
"label": "Event Type",
"description": "Check out this page for possible event types: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"default": "ADD_CART",
"required": True,
},
*common_inputs,
],
),
HogFunctionMappingTemplate(
name="Payment Info Entered",
include_by_default=True,
filters={"events": [{"id": "Payment Info Entered", "type": "events"}]},
inputs_schema=[
{
"key": "eventType",
"type": "string",
"label": "Event Type",
"description": "Check out this page for possible event types: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"default": "ADD_BILLING",
"required": True,
},
*common_inputs,
],
),
HogFunctionMappingTemplate(
name="Promotion Clicked",
include_by_default=True,
filters={"events": [{"id": "Promotion Clicked", "type": "events"}]},
inputs_schema=[
{
"key": "eventType",
"type": "string",
"label": "Event Type",
"description": "Check out this page for possible event types: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"default": "AD_CLICK",
"required": True,
},
*common_inputs,
],
),
HogFunctionMappingTemplate(
name="Promotion Viewed",
include_by_default=True,
filters={"events": [{"id": "Promotion Viewed", "type": "events"}]},
inputs_schema=[
{
"key": "eventType",
"type": "string",
"label": "Event Type",
"description": "Check out this page for possible event types: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"default": "AD_VIEW",
"required": True,
},
*common_inputs,
],
),
HogFunctionMappingTemplate(
name="Product Added to Wishlist",
include_by_default=True,
filters={"events": [{"id": "Product Added to Wishlist", "type": "events"}]},
inputs_schema=[
{
"key": "eventType",
"type": "string",
"label": "Event Type",
"description": "Check out this page for possible event types: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"default": "ADD_TO_WISHLIST",
"required": True,
},
*common_inputs,
],
),
HogFunctionMappingTemplate(
name="Product Viewed",
include_by_default=True,
filters={"events": [{"id": "Product Viewed", "type": "events"}]},
inputs_schema=[
{
"key": "eventType",
"type": "string",
"label": "Event Type",
"description": "Check out this page for possible event types: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"default": "VIEW_CONTENT",
"required": True,
},
*common_inputs,
],
),
HogFunctionMappingTemplate(
name="Product List Viewed",
include_by_default=True,
filters={"events": [{"id": "Product List Viewed", "type": "events"}]},
inputs_schema=[
{
"key": "eventType",
"type": "string",
"label": "Event Type",
"description": "Check out this page for possible event types: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"default": "VIEW_CONTENT",
"required": True,
},
*common_inputs,
],
),
HogFunctionMappingTemplate(
name="Products Searched",
include_by_default=True,
filters={"events": [{"id": "Products Searched", "type": "events"}]},
inputs_schema=[
{
"key": "eventType",
"type": "string",
"label": "Event Type",
"description": "Check out this page for possible event types: https://businesshelp.snapchat.com/s/article/pixel-direct-implementation",
"default": "SEARCH",
"required": True,
},
*common_inputs,
],
),
],
)
Loading
Loading