-
Notifications
You must be signed in to change notification settings - Fork 159
feat: push replay and person data to intercom and crisp chat #2039
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
Open
pauldambra
wants to merge
9
commits into
main
Choose a base branch
from
feat/push-to-external-integrations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+245
−13
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d724a1c
feat: push replay and person data to intercom and crisp chat
pauldambra b700da8
import
pauldambra 2475f24
Merge branch 'main' into feat/push-to-external-integrations
pauldambra 786f21a
fix
pauldambra 1193cb6
fix
pauldambra 46f3a04
fix
pauldambra de15c13
fix
pauldambra cb2e0fc
fix
pauldambra 8fb9f33
fix
pauldambra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
42 changes: 42 additions & 0 deletions
42
packages/browser/playground/nextjs/pages/external_chat.tsx
This file contains hidden or 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,42 @@ | ||
import Script from 'next/script' | ||
|
||
export default function ExternalChat() { | ||
return ( | ||
<div> | ||
<h1>ExternalChat</h1> | ||
<p>This is a page for testing external chat widgets</p> | ||
|
||
{/* Intercom Settings Script */} | ||
<Script id="intercom-settings" strategy="beforeInteractive"> | ||
{` | ||
window.intercomSettings = { | ||
api_base: "https://api-iam.intercom.io", | ||
app_id: "cviln1h1", | ||
}; | ||
`} | ||
</Script> | ||
|
||
{/* Intercom Widget Script */} | ||
<Script id="intercom-widget" strategy="afterInteractive"> | ||
{` | ||
(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/cviln1h1';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);};if(document.readyState==='complete'){l();}else if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})(); | ||
`} | ||
</Script> | ||
|
||
{/* Crisp chat Script */} | ||
<Script id="crisp-chat" strategy="afterInteractive"> | ||
{` | ||
window.$crisp=[]; | ||
window.CRISP_WEBSITE_ID="8a81621c-0ed1-4d1f-b552-9404db8effd5"; | ||
(function(){ | ||
d = document; | ||
s = d.createElement("script"); | ||
s.src = "https://client.crisp.chat/l.js"; | ||
s.async = 1; | ||
d.getElementsByTagName("head")[0].appendChild(s); | ||
})(); | ||
`} | ||
</Script> | ||
</div> | ||
) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
58 changes: 58 additions & 0 deletions
58
packages/browser/src/entrypoints/crisp-chat-integration.ts
This file contains hidden or 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,58 @@ | ||
import { PostHog } from '../posthog-core' | ||
import { assignableWindow } from '../utils/globals' | ||
import { createLogger } from '../utils/logger' | ||
|
||
const logger = createLogger('[PostHog Crisp Chat]') | ||
|
||
const reportedSessionIds = new Set<string>() | ||
let sessionIdListenerUnsubscribe: undefined | (() => void) = undefined | ||
|
||
assignableWindow.__PosthogExtensions__ = assignableWindow.__PosthogExtensions__ || {} | ||
assignableWindow.__PosthogExtensions__.integrations = assignableWindow.__PosthogExtensions__.integrations || {} | ||
assignableWindow.__PosthogExtensions__.integrations.crispChat = { | ||
start: (posthog: PostHog) => { | ||
if (!posthog.config.integrations?.crispChat) { | ||
return | ||
} | ||
|
||
const crispChat = (assignableWindow as any).$crisp | ||
if (!crispChat) { | ||
logger.warn('Crisp Chat not found while initializing the integration') | ||
return | ||
} | ||
|
||
const updateCrispChat = () => { | ||
const replayUrl = posthog.get_session_replay_url() | ||
const personUrl = posthog.requestRouter.endpointFor( | ||
'ui', | ||
`/project/${posthog.config.token}/person/${posthog.get_distinct_id()}` | ||
) | ||
|
||
crispChat.push([ | ||
'set', | ||
'session:data', | ||
[ | ||
[ | ||
['posthogSessionURL', replayUrl], | ||
['posthogPersonURL', personUrl], | ||
], | ||
], | ||
]) | ||
} | ||
|
||
// this is called immediately if there's a session id | ||
// and then again whenever the session id changes | ||
sessionIdListenerUnsubscribe = posthog.onSessionId((sessionId) => { | ||
if (!reportedSessionIds.has(sessionId)) { | ||
updateCrispChat() | ||
reportedSessionIds.add(sessionId) | ||
} | ||
}) | ||
|
||
logger.info('integration started') | ||
}, | ||
stop: () => { | ||
sessionIdListenerUnsubscribe?.() | ||
sessionIdListenerUnsubscribe = undefined | ||
}, | ||
} |
This file contains hidden or 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,53 @@ | ||
import { PostHog } from '../posthog-core' | ||
import { assignableWindow } from '../utils/globals' | ||
import { createLogger } from '../utils/logger' | ||
|
||
const logger = createLogger('[PostHog Intercom integration]') | ||
|
||
const reportedSessionIds = new Set<string>() | ||
pauldambra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let sessionIdListenerUnsubscribe: undefined | (() => void) = undefined | ||
|
||
assignableWindow.__PosthogExtensions__ = assignableWindow.__PosthogExtensions__ || {} | ||
assignableWindow.__PosthogExtensions__.integrations = assignableWindow.__PosthogExtensions__.integrations || {} | ||
assignableWindow.__PosthogExtensions__.integrations.intercom = { | ||
start: (posthog: PostHog) => { | ||
if (!posthog.config.integrations?.intercom) { | ||
return | ||
} | ||
|
||
const intercom = (assignableWindow as any).Intercom | ||
if (!intercom) { | ||
logger.warn('Intercom not found while initializing the integration') | ||
return | ||
} | ||
|
||
const updateIntercom = () => { | ||
const replayUrl = posthog.get_session_replay_url() | ||
const personUrl = posthog.requestRouter.endpointFor( | ||
'ui', | ||
`/project/${posthog.config.token}/person/${posthog.get_distinct_id()}` | ||
) | ||
|
||
intercom('update', { | ||
latestPosthogReplayURL: replayUrl, | ||
latestPosthogPersonURL: personUrl, | ||
}) | ||
intercom('trackEvent', 'posthog:sessionInfo', { replayUrl, personUrl }) | ||
} | ||
|
||
// this is called immediately if there's a session id | ||
// and then again whenever the session id changes | ||
sessionIdListenerUnsubscribe = posthog.onSessionId((sessionId) => { | ||
if (!reportedSessionIds.has(sessionId)) { | ||
updateIntercom() | ||
reportedSessionIds.add(sessionId) | ||
} | ||
}) | ||
|
||
logger.info('integration started') | ||
}, | ||
stop: () => { | ||
sessionIdListenerUnsubscribe?.() | ||
sessionIdListenerUnsubscribe = undefined | ||
}, | ||
} |
This file contains hidden or 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,41 @@ | ||
import { PostHog } from '../posthog-core' | ||
import { ExternalIntegrationKind } from '../types' | ||
import { assignableWindow, ExternalExtensionKind } from '../utils/globals' | ||
import { createLogger } from '../utils/logger' | ||
|
||
const logger = createLogger('[PostHog ExternalIntegrations]') | ||
|
||
const MAPPED_INTEGRATIONS: Record<ExternalIntegrationKind, ExternalExtensionKind> = { | ||
intercom: 'intercom-integration', | ||
crispChat: 'crisp-chat-integration', | ||
} | ||
|
||
export class ExternalIntegrations { | ||
constructor(private readonly _instance: PostHog) {} | ||
|
||
private _loadScript(name: ExternalExtensionKind, cb: () => void): void { | ||
assignableWindow.__PosthogExtensions__?.loadExternalDependency?.(this._instance, name, (err) => { | ||
if (err) { | ||
return logger.error('failed to load script', err) | ||
} | ||
cb() | ||
}) | ||
} | ||
|
||
public startIfEnabledOrStop() { | ||
pauldambra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (const [key, value] of Object.entries(this._instance.config.integrations ?? {})) { | ||
// if the integration is enabled, and not present, then load it | ||
if (value && !assignableWindow.__PosthogExtensions__?.integrations?.[key as ExternalIntegrationKind]) { | ||
this._loadScript(MAPPED_INTEGRATIONS[key as ExternalIntegrationKind], () => { | ||
assignableWindow.__PosthogExtensions__?.integrations?.[key as ExternalIntegrationKind]?.start( | ||
this._instance | ||
) | ||
}) | ||
} | ||
// if the integration is disabled, and present, then stop it | ||
if (!value && assignableWindow.__PosthogExtensions__?.integrations?.[key as ExternalIntegrationKind]) { | ||
assignableWindow.__PosthogExtensions__?.integrations?.[key as ExternalIntegrationKind]?.stop() | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.