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): mapping #26655

Merged
merged 31 commits into from
Dec 12, 2024
Merged

feat(cdp): mapping #26655

merged 31 commits into from
Dec 12, 2024

Conversation

mariusandra
Copy link
Collaborator

@mariusandra mariusandra commented Dec 4, 2024

Problem

Currently you need to set up multiple copies of one destination in order to have it send slightly different payloads on different events.

Changes

Creates a new mapping system, only for site destinations for now:

2024-12-11 00 55 46

2024-12-11 00 53 34

Features

  • You must always have one mapping or the function is a noop (the UI prevents deleting the last one)
  • All mappings that match are triggered for each event.

How did you test this code?

  • Tested locally.
  • Added a test to make sure the mapping code is compiled and inserted accordingly
  • TODO: we should really test all of this E2E in a simulated browser

Copy link
Contributor

github-actions bot commented Dec 4, 2024

Size Change: +60 B (+0.01%)

Total Size: 1.11 MB

ℹ️ View Unchanged
Filename Size Change
frontend/dist/toolbar.js 1.11 MB +60 B (+0.01%)

compressed-size-action

@mariusandra mariusandra changed the title feat(cdp): match groups feat(cdp): mapping Dec 10, 2024
@mariusandra mariusandra marked this pull request as ready for review December 11, 2024 12:46
Copy link
Contributor

@benjackwhite benjackwhite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fan-freaking-tastic. Some comments but nothing explicitly blocking so ✅ - just be aware the preview JS thing has my changes needed before we actually include it

@@ -67,6 +67,7 @@ export function loadPostHogJS(): void {
capture_copied_text: true,
},
person_profiles: 'always',
__preview_remote_config: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks flags - I'm working on the proper fix atm but just beware of merging

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this line for now... I'm also hoping this is what caused alerts.cy and onboarding.cy to flake as well 🤞 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it is

return (
<>
{mappings.map((mapping, index) => (
<div key={index} className="border bg-bg-light rounded p-3 space-y-2">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More of a follow up comment - this new thing breaks the design pattern of having clear panels for different sections. I might fly by and bring some opinionated UI changes to see if we can make it a little cleaner. Definitely a comment on my work, not yours 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely feel free to step all over my "designer with 2 left hands" work (to not throw backend engineers under the bus again)

attrs["filters"]["transpiled"] = {"lang": "ts", "code": code, "stl": list(compiler.stl_functions)}
if "bytecode" in attrs["filters"]:
del attrs["filters"]["bytecode"]
attrs["mappings"] = attrs.get("mappings") or []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually thing this should default to None. Reason being that in the future mappings will essentially override the default so we need a way to indicate that someone has created a destination with no mappings - therefore a noop.

Doesn't really make a difference today but I already know it will be a future migration effort so would be nice to keep it clean now.


validate_input_and_filters(attrs, attrs["type"])

if "mappings" in attrs and attrs["mappings"] is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good currently to explicitly raise if mappings are provided for non site-destinations - again thinking about keeping things clean just in case for the future follow up.

response += "\n})"
# Wrap in IIFE = Immediately Invoked (invokable) Function Expression = to avoid polluting global scope
# Add collected STL functions above the generated code
response = "(function() {\n" + compiler.get_stl_code() + "\n" + response + "\n})"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smart


blank_site_destination: HogFunctionTemplate = HogFunctionTemplate(
status="client-side",
type="site_destination",
id="template-blank-site-destination",
name="New client-side destination",
description="Run code on your website when an event is sent to PostHog. Works only with posthog-js when opt_in_site_apps is set to true.",
description="New destination with complex event mapping. Works only with posthog-js when opt_in_site_apps is set to true.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine whilst we are testing internally but i think once we have a "real" destination using mappings we should revert this to be a simple single mapping function

@@ -255,3 +262,24 @@ def test_get_transpiled_function_with_complex_filters(self):
assert "const filterMatches = " in result
assert '__getGlobal("event") == "$pageview"' in result
assert "https://example.com" in result

def test_get_transpiled_function_with_mappings(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really would love to get proper testing working for these... I have the little node runner which is good for just seeing if there are compilation errors but I think we could likely mock out enough browser-y things to actually do proper tests via node at some point...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agreed. I'll try to think of something, but in a followup. Even node + jsdom would go a long way.

@mariusandra mariusandra merged commit bae2b6f into master Dec 12, 2024
96 checks passed
@mariusandra mariusandra deleted the hog-match-groups branch December 12, 2024 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants