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: RemoteConfig logic (decide replacement) #26348

Merged
merged 52 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
f679a2a
Started adding remote config
benjackwhite Nov 21, 2024
c36bba5
Added generator and tests
benjackwhite Nov 22, 2024
cc61230
Fixes
benjackwhite Nov 22, 2024
bcd6577
Update query snapshots
github-actions[bot] Nov 22, 2024
9ae9f75
Update query snapshots
github-actions[bot] Nov 22, 2024
46d8eba
Fixes
benjackwhite Nov 28, 2024
901c40f
Merge branch 'feat/serverless-decide' of github.com:PostHog/posthog i…
benjackwhite Nov 28, 2024
d8e679f
Merge branch 'master' into feat/serverless-decide
benjackwhite Nov 28, 2024
c3308f8
Fixes
benjackwhite Nov 28, 2024
ce490ad
Fixes
benjackwhite Nov 28, 2024
13ff981
Fix up
benjackwhite Nov 28, 2024
12d6ef5
Added array configs to config page
benjackwhite Nov 28, 2024
ca61624
Update UI snapshots for `chromium` (1)
github-actions[bot] Nov 28, 2024
9131733
fix
benjackwhite Nov 29, 2024
1de9759
Fixes
benjackwhite Nov 29, 2024
b65bf07
fix
benjackwhite Nov 29, 2024
942f9d7
Fix up to use a celery task for updates
benjackwhite Nov 29, 2024
3c65713
Fix
benjackwhite Nov 29, 2024
406e016
Added syncing to S3
benjackwhite Nov 29, 2024
babacf3
Fix tests
benjackwhite Dec 2, 2024
8e64e35
Fixes
benjackwhite Dec 2, 2024
3c552cc
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 2, 2024
f21a315
Fixes
benjackwhite Dec 2, 2024
17fe62b
Fix
benjackwhite Dec 2, 2024
3fecfdc
Fix
benjackwhite Dec 2, 2024
016c85a
Fixes
benjackwhite Dec 2, 2024
48efbda
Update UI snapshots for `chromium` (1)
github-actions[bot] Dec 2, 2024
6f7e5cb
Fixes
benjackwhite Dec 2, 2024
90e877f
Fix
benjackwhite Dec 2, 2024
8be131c
Fixes
benjackwhite Dec 2, 2024
10f598f
Remove S3 stuff
benjackwhite Dec 2, 2024
d27534e
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 2, 2024
d5892d7
Fixes
benjackwhite Dec 2, 2024
015cd12
Fix array js loading
benjackwhite Dec 2, 2024
d46bc65
Update query snapshots
github-actions[bot] Dec 2, 2024
ebd444a
Update query snapshots
github-actions[bot] Dec 2, 2024
01c9fad
Update query snapshots
github-actions[bot] Dec 2, 2024
86b84bd
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 3, 2024
d913c84
Fixed up tests
benjackwhite Dec 3, 2024
0f0e0f2
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 3, 2024
30ac3ea
Update query snapshots
github-actions[bot] Dec 3, 2024
bf32a66
fix: Match config to old format (#26600)
benjackwhite Dec 4, 2024
27c69cb
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 4, 2024
749f55d
fix
benjackwhite Dec 4, 2024
a5344c3
Fix
benjackwhite Dec 4, 2024
aafc1cc
Update UI snapshots for `chromium` (1)
github-actions[bot] Dec 4, 2024
1ccaf2b
Update query snapshots
github-actions[bot] Dec 4, 2024
6beebaf
Update query snapshots
github-actions[bot] Dec 4, 2024
fa4b039
Fix
benjackwhite Dec 4, 2024
771648e
Merge branch 'feat/serverless-decide' of github.com:PostHog/posthog i…
benjackwhite Dec 4, 2024
3ae8b46
Added array gzipping
benjackwhite Dec 5, 2024
f0dee04
Merge branch 'master' into feat/serverless-decide
benjackwhite Dec 5, 2024
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# serializer version: 1
# name: ClickhouseTestFunnelExperimentResults.test_experiment_flow_with_event_results
'''
/* user_id:0 celery:posthog.tasks.tasks.sync_insight_caching_state */
/* celery:posthog.tasks.tasks.sync_insight_caching_state */
SELECT team_id,
date_diff('second', max(timestamp), now()) AS age
FROM events
Expand Down Expand Up @@ -108,7 +108,7 @@
# ---
# name: ClickhouseTestFunnelExperimentResults.test_experiment_flow_with_event_results_and_events_out_of_time_range_timezones
'''
/* user_id:0 celery:posthog.tasks.tasks.sync_insight_caching_state */
/* celery:posthog.tasks.tasks.sync_insight_caching_state */
SELECT team_id,
date_diff('second', max(timestamp), now()) AS age
FROM events
Expand Down Expand Up @@ -215,7 +215,7 @@
# ---
# name: ClickhouseTestFunnelExperimentResults.test_experiment_flow_with_event_results_for_three_test_variants
'''
/* user_id:0 celery:posthog.tasks.tasks.sync_insight_caching_state */
/* celery:posthog.tasks.tasks.sync_insight_caching_state */
SELECT team_id,
date_diff('second', max(timestamp), now()) AS age
FROM events
Expand Down Expand Up @@ -322,7 +322,7 @@
# ---
# name: ClickhouseTestFunnelExperimentResults.test_experiment_flow_with_event_results_with_hogql_aggregation
'''
/* user_id:0 celery:posthog.tasks.tasks.sync_insight_caching_state */
/* celery:posthog.tasks.tasks.sync_insight_caching_state */
SELECT team_id,
date_diff('second', max(timestamp), now()) AS age
FROM events
Expand Down Expand Up @@ -429,7 +429,7 @@
# ---
# name: ClickhouseTestTrendExperimentResults.test_experiment_flow_with_event_results
'''
/* user_id:0 celery:posthog.tasks.tasks.sync_insight_caching_state */
/* celery:posthog.tasks.tasks.sync_insight_caching_state */
SELECT team_id,
date_diff('second', max(timestamp), now()) AS age
FROM events
Expand Down Expand Up @@ -592,7 +592,7 @@
# ---
# name: ClickhouseTestTrendExperimentResults.test_experiment_flow_with_event_results_for_three_test_variants
'''
/* user_id:0 celery:posthog.tasks.tasks.sync_insight_caching_state */
/* celery:posthog.tasks.tasks.sync_insight_caching_state */
SELECT team_id,
date_diff('second', max(timestamp), now()) AS age
FROM events
Expand Down Expand Up @@ -694,7 +694,7 @@
# ---
# name: ClickhouseTestTrendExperimentResults.test_experiment_flow_with_event_results_out_of_timerange_timezone
'''
/* user_id:0 celery:posthog.tasks.tasks.sync_insight_caching_state */
/* celery:posthog.tasks.tasks.sync_insight_caching_state */
SELECT team_id,
date_diff('second', max(timestamp), now()) AS age
FROM events
Expand Down Expand Up @@ -849,7 +849,7 @@
# ---
# name: ClickhouseTestTrendExperimentResults.test_experiment_flow_with_event_results_with_hogql_filter
'''
/* user_id:0 celery:posthog.tasks.tasks.sync_insight_caching_state */
/* celery:posthog.tasks.tasks.sync_insight_caching_state */
SELECT team_id,
date_diff('second', max(timestamp), now()) AS age
FROM events
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 16 additions & 7 deletions frontend/src/lib/components/JSSnippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { apiHostOrigin } from 'lib/utils/apiHost'
import posthog from 'posthog-js'
import { teamLogic } from 'scenes/teamLogic'

export function snippetFunctions(): string {
function snippetFunctions(arrayJs = '/static/array.js'): string {
const methods: string[] = []
const posthogPrototype = Object.getPrototypeOf(posthog)
for (const key of Object.getOwnPropertyNames(posthogPrototype)) {
Expand All @@ -20,23 +20,24 @@ export function snippetFunctions(): string {
}
const snippetMethods = methods.join(' ')

return `!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.crossOrigin="anonymous",p.async=!0,p.src=s.api_host.replace(".i.posthog.com","-assets.i.posthog.com")+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="${snippetMethods}".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);`
return `!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.crossOrigin="anonymous",p.async=!0,p.src=s.api_host.replace(".i.posthog.com","-assets.i.posthog.com")+"${arrayJs}",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="${snippetMethods}".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);`
}

export function useJsSnippet(indent = 0): string {
export function useJsSnippet(indent = 0, arrayJs?: string): string {
const { currentTeam } = useValues(teamLogic)
const { featureFlags } = useValues(featureFlagLogic)
const isPersonProfilesDisabled = featureFlags[FEATURE_FLAGS.PERSONLESS_EVENTS_NOT_SUPPORTED]

return [
'<script>',
` ${snippetFunctions()}`,
` posthog.init('${currentTeam?.api_token}',{api_host:'${apiHostOrigin()}', ${
` ${snippetFunctions(arrayJs)}`,
` posthog.init('${currentTeam?.api_token}', {
api_host:'${apiHostOrigin()}'${
isPersonProfilesDisabled
? ``
: `person_profiles: 'identified_only' // or 'always' to create profiles for anonymous users as well`
: `,\n person_profiles: 'identified_only' // or 'always' to create profiles for anonymous users as well`
}
})`,
})`,
'</script>',
]
.map((x) => ' '.repeat(indent) + x)
Expand All @@ -48,3 +49,11 @@ export function JSSnippet(): JSX.Element {

return <CodeSnippet language={Language.HTML}>{snippet}</CodeSnippet>
}

export function JSSnippetV2(): JSX.Element {
const { currentTeam } = useValues(teamLogic)

const snippet = useJsSnippet(0, `/array/${currentTeam?.api_token}/array.js`)

return <CodeSnippet language={Language.HTML}>{snippet}</CodeSnippet>
}
1 change: 1 addition & 0 deletions frontend/src/lib/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export const FEATURE_FLAGS = {
CUSTOM_CSS_THEMES: 'custom-css-themes', // owner: @daibhin
EXPERIMENTS_MULTIPLE_METRICS: 'experiments-multiple-metrics', // owner: @jurajmajerik #team-experiments
WEB_ANALYTICS_WARN_CUSTOM_EVENT_NO_SESSION: 'web-analytics-warn-custom-event-no-session', // owner: @robbie-c #team-web-analytics
REMOTE_CONFIG: 'remote-config', // owner: @benjackwhite
TWO_FACTOR_UI: 'two-factor-ui', // owner: @zach
SITE_DESTINATIONS: 'site-destinations', // owner: @mariusandra #team-cdp
SITE_APP_FUNCTIONS: 'site-app-functions', // owner: @mariusandra #team-cdp
Expand Down
24 changes: 22 additions & 2 deletions frontend/src/scenes/settings/environment/TeamSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { urls } from '@posthog/apps-common'
import { LemonButton, LemonDialog, LemonInput, LemonLabel, LemonSkeleton } from '@posthog/lemon-ui'
import { LemonButton, LemonDialog, LemonInput, LemonLabel, LemonSkeleton, LemonTag } from '@posthog/lemon-ui'
import { useActions, useValues } from 'kea'
import { AuthorizedUrlList } from 'lib/components/AuthorizedUrlList/AuthorizedUrlList'
import { AuthorizedUrlListType } from 'lib/components/AuthorizedUrlList/authorizedUrlListLogic'
import { CodeSnippet } from 'lib/components/CodeSnippet'
import { FlaggedFeature } from 'lib/components/FlaggedFeature'
import { JSBookmarklet } from 'lib/components/JSBookmarklet'
import { JSSnippet } from 'lib/components/JSSnippet'
import { JSSnippet, JSSnippetV2 } from 'lib/components/JSSnippet'
import { getPublicSupportSnippet } from 'lib/components/Support/supportLogic'
import { FEATURE_FLAGS } from 'lib/constants'
import { IconRefresh } from 'lib/lemon-ui/icons'
Expand Down Expand Up @@ -66,6 +67,25 @@ export function WebSnippet(): JSX.Element {
) : (
<JSSnippet />
)}

<FlaggedFeature flag="remote-config">
<h3 className="mt-4 flex items-center gap-2">
Web Snippet V2 <LemonTag type="warning">Experimental</LemonTag>
</h3>
<p>
The V2 version of the snippet is more advanced and includes your project config automatically along
with the PostHog JS code. This generally leads to faster load times and fewer calls needed before
the SDK is fully functional.
</p>
{currentTeamLoading && !currentTeam ? (
<div className="space-y-4">
<LemonSkeleton className="w-1/2 h-4" />
<LemonSkeleton repeat={3} />
</div>
) : (
<JSSnippetV2 />
)}
</FlaggedFeature>
</>
)
}
Expand Down
5 changes: 1 addition & 4 deletions posthog/api/decide.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,7 @@ def get_decide(request: HttpRequest):
if random() < settings.NEW_CAPTURE_ENDPOINTS_SAMPLING_RATE:
response["__preview_ingestion_endpoints"] = True

if (
settings.ELEMENT_CHAIN_AS_STRING_EXCLUDED_TEAMS
and str(team.id) not in settings.ELEMENT_CHAIN_AS_STRING_EXCLUDED_TEAMS
):
if str(team.id) not in (settings.ELEMENT_CHAIN_AS_STRING_EXCLUDED_TEAMS or []):
response["elementsChainAsString"] = True

response["sessionRecording"] = _session_recording_config_response(request, team, token)
Expand Down
38 changes: 38 additions & 0 deletions posthog/api/remote_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.http import JsonResponse, Http404, HttpResponse
from rest_framework.views import APIView
from posthog.models.remote_config import RemoteConfig


class BaseRemoteConfigAPIView(APIView):
"""
Base class for RemoteConfig API views.
"""

authentication_classes = []
permission_classes = []

def get_object(self, token: str) -> RemoteConfig:
try:
return RemoteConfig.objects.get(team__api_token=token)
except RemoteConfig.DoesNotExist:
raise Http404()


class RemoteConfigAPIView(BaseRemoteConfigAPIView):
def get(self, request, token: str, *args, **kwargs):
resource = self.get_object(token)
return JsonResponse(resource.config)


class RemoteConfigJSAPIView(BaseRemoteConfigAPIView):
def get(self, request, token: str, *args, **kwargs):
resource = self.get_object(token)
script_content = resource.build_js_config()
return HttpResponse(script_content, content_type="application/javascript")


class RemoteConfigArrayJSAPIView(BaseRemoteConfigAPIView):
def get(self, request, token: str, *args, **kwargs):
resource = self.get_object(token)
script_content = resource.build_array_js_config()
return HttpResponse(script_content, content_type="application/javascript")
3 changes: 1 addition & 2 deletions posthog/api/site_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json

from django.http import HttpRequest, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework import status
Expand All @@ -8,8 +7,8 @@

from posthog.exceptions import generate_exception_response
from posthog.logging.timing import timed
from posthog.plugins.site import get_transpiled_site_source, get_site_config_from_schema
from posthog.models.hog_functions.hog_function import HogFunction
from posthog.plugins.site import get_site_config_from_schema, get_transpiled_site_source


@csrf_exempt
Expand Down
Loading
Loading