-
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 google ads hog function (#25530)
- Loading branch information
Showing
11 changed files
with
217 additions
and
4 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
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
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,93 @@ | ||
from posthog.cdp.templates.hog_function_template import HogFunctionTemplate | ||
|
||
template: HogFunctionTemplate = HogFunctionTemplate( | ||
status="alpha", | ||
id="template-google-ads", | ||
name="Google Ads Conversions", | ||
description="Send conversion events to Google Ads", | ||
icon_url="/static/services/google-ads.png", | ||
category=["Advertisement"], | ||
hog=""" | ||
let res := fetch(f'https://googleads.googleapis.com/v17/customers/{replaceAll(inputs.customerId, '-', '')}:uploadClickConversions', { | ||
'method': 'POST', | ||
'headers': { | ||
'Authorization': f'Bearer {inputs.oauth.access_token}', | ||
'Content-Type': 'application/json', | ||
'developer-token': inputs.developerToken | ||
}, | ||
'body': { | ||
'conversions': [ | ||
{ | ||
'gclid': inputs.gclid, | ||
'conversionAction': f'customers/{replaceAll(inputs.customerId, '-', '')}/conversionActions/{replaceAll(inputs.conversionActionId, 'AW-', '')}', | ||
'conversionDateTime': inputs.conversionDateTime | ||
} | ||
], | ||
'partialFailure': true, | ||
'validateOnly': true | ||
} | ||
}) | ||
if (res.status >= 400) { | ||
throw Error(f'Error from googleads.googleapis.com (status {res.status}): {res.body}') | ||
} | ||
""".strip(), | ||
inputs_schema=[ | ||
{ | ||
"key": "oauth", | ||
"type": "integration", | ||
"integration": "google-ads", | ||
"label": "Google Ads account", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "developerToken", | ||
"type": "string", | ||
"label": "Developer token", | ||
"description": "This should be a 22-character long alphanumeric string. Check out this page on how to obtain such a token: https://developers.google.com/google-ads/api/docs/get-started/dev-token", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "customerId", | ||
"type": "string", | ||
"label": "Customer ID", | ||
"description": "ID of your Google Ads Account. This should be 10-digits and in XXX-XXX-XXXX format.", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "conversionActionId", | ||
"type": "string", | ||
"label": "Conversion action ID", | ||
"description": "You will find this information in the event snippet for your conversion action, for example send_to: AW-CONVERSION_ID/AW-CONVERSION_LABEL. This should be in the AW-CONVERSION_ID format.", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "gclid", | ||
"type": "string", | ||
"label": "Google Click ID (gclid)", | ||
"description": "The Google click ID (gclid) associated with this conversion.", | ||
"default": "{person.gclid}", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "conversionDateTime", | ||
"type": "string", | ||
"label": "Conversion Date Time", | ||
"description": 'The date time at which the conversion occurred. Must be after the click time. The timezone must be specified. The format is "yyyy-mm-dd hh:mm:ss+|-hh:mm", e.g. "2019-01-01 12:32:45-08:00".', | ||
"default": "{event.timestamp}", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
], | ||
filters={ | ||
"events": [], | ||
"actions": [], | ||
"filter_test_accounts": True, | ||
}, | ||
) |
51 changes: 51 additions & 0 deletions
51
posthog/cdp/templates/google_ads/test_template_google_ads.py
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,51 @@ | ||
from inline_snapshot import snapshot | ||
from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest | ||
from posthog.cdp.templates.google_ads.template_google_ads import ( | ||
template as template_google_ads, | ||
) | ||
|
||
|
||
class TestTemplateGoogleAds(BaseHogFunctionTemplateTest): | ||
template = template_google_ads | ||
|
||
def _inputs(self, **kwargs): | ||
inputs = { | ||
"oauth": { | ||
"access_token": "oauth-1234", | ||
}, | ||
"developerToken": "developer-token1234", | ||
"customerId": "123-123-1234", | ||
"conversionActionId": "AW-123456789", | ||
"gclid": "89y4thuergnjkd34oihroh3uhg39uwhgt9", | ||
"conversionDateTime": "2024-10-10 16:32:45+02:00", | ||
} | ||
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://googleads.googleapis.com/v17/customers/1231231234:uploadClickConversions", | ||
{ | ||
"body": { | ||
"conversions": [ | ||
{ | ||
"gclid": "89y4thuergnjkd34oihroh3uhg39uwhgt9", | ||
"conversionAction": f"customers/1231231234/conversionActions/123456789", | ||
"conversionDateTime": "2024-10-10 16:32:45+02:00", | ||
} | ||
], | ||
"partialFailure": True, | ||
"validateOnly": True, | ||
}, | ||
"method": "POST", | ||
"headers": { | ||
"Authorization": "Bearer oauth-1234", | ||
"Content-Type": "application/json", | ||
"developer-token": "developer-token1234", | ||
}, | ||
}, | ||
) | ||
) |
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,27 @@ | ||
# Generated by Django 4.2.15 on 2024-10-15 10:30 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("posthog", "0488_alter_user_is_active"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="integration", | ||
name="kind", | ||
field=models.CharField( | ||
choices=[ | ||
("slack", "Slack"), | ||
("salesforce", "Salesforce"), | ||
("hubspot", "Hubspot"), | ||
("google-pubsub", "Google Pubsub"), | ||
("google-cloud-storage", "Google Cloud Storage"), | ||
("google-ads", "Google Ads"), | ||
], | ||
max_length=20, | ||
), | ||
), | ||
] |
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