diff --git a/.changeset/fifty-spiders-burn.md b/.changeset/fifty-spiders-burn.md new file mode 100644 index 00000000..9e469d4e --- /dev/null +++ b/.changeset/fifty-spiders-burn.md @@ -0,0 +1,5 @@ +--- +'react-use-intercom': minor +--- + +Adding party town as an option diff --git a/README.md b/README.md index 65616e20..ae393148 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ Place the `IntercomProvider` as high as possible in your application. This will | shouldInitialize | boolean | indicates if the Intercom should be initialized. Can be used in multistaged environment | false | true | | apiBase | string | If you need to route your Messenger requests through a different endpoint than the default. Generally speaking, this is not needed.
Format: `https://${INTERCOM_APP_ID}.intercom-messenger.com` (See: [https://github.com/devrnt/react-use-intercom/pull/96](https://github.com/devrnt/react-use-intercom/pull/96)) | false | | | initializeDelay | number | Indicates if the intercom initialization should be delayed, delay is in ms, defaults to 0. See https://github.com/devrnt/react-use-intercom/pull/236 | false | | +| isPartyTown | boolean | Indicates if the intercom initialization with party town. See https://github.com/devrnt/react-use-intercom/pull/742 | false | false | | autoBootProps | IntercomProps | Pass properties to `boot` method when `autoBoot` is `true` | false | | #### Example diff --git a/packages/react-use-intercom/src/initialize.ts b/packages/react-use-intercom/src/initialize.ts index ba454aec..9d4cc021 100644 --- a/packages/react-use-intercom/src/initialize.ts +++ b/packages/react-use-intercom/src/initialize.ts @@ -1,13 +1,25 @@ // @ts-nocheck + +interface InitializeProps { + appId: string; + timeout?: number; + isPartyTown?: boolean; +} /** * Snippet to initialize the Intercom instance * * @param appId - Intercom app id * @param [timeout=0] - Amount of milliseconds that the initialization should be delayed, defaults to 0 + * @param [isPartyTown=false] - If intercom should be initialized using party town * * @see {@link https://developers.intercom.com/installing-intercom/docs/basic-javascript} */ -const initialize = (appId: string, timeout = 0) => { + +const initialize = ({ + appId, + timeout = 0, + isPartyTown = false, +}: InitializeProps) => { var w = window; var ic = w.Intercom; if (typeof ic === 'function') { @@ -26,11 +38,15 @@ const initialize = (appId: string, timeout = 0) => { var l = function () { setTimeout(function () { var s = d.createElement('script'); - s.type = 'text/javascript'; + s.type = isPartyTown ? 'text/partytown' : 'text/javascript'; s.async = true; s.src = 'https://widget.intercom.io/widget/' + appId; var x = d.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x); + + if (isPartyTown) { + window.dispatchEvent(new CustomEvent('ptupdate')); + } }, timeout); }; if (document.readyState === 'complete') { diff --git a/packages/react-use-intercom/src/provider.tsx b/packages/react-use-intercom/src/provider.tsx index 6145ef5e..1523367d 100644 --- a/packages/react-use-intercom/src/provider.tsx +++ b/packages/react-use-intercom/src/provider.tsx @@ -28,6 +28,7 @@ export const IntercomProvider: React.FC< shouldInitialize = !isSSR, apiBase, initializeDelay, + isPartyTown = false, ...rest }) => { const isBooted = React.useRef(false); @@ -102,7 +103,7 @@ export const IntercomProvider: React.FC< React.useEffect(() => { if (!isSSR && shouldInitialize && !isInitialized.current) { - initialize(appId, initializeDelay); + initialize({ appId, timeout: initializeDelay, isPartyTown }); if (autoBoot) { boot(autoBootProps); diff --git a/packages/react-use-intercom/src/types.ts b/packages/react-use-intercom/src/types.ts index 4f6d3ce7..f5dd2f55 100644 --- a/packages/react-use-intercom/src/types.ts +++ b/packages/react-use-intercom/src/types.ts @@ -511,6 +511,10 @@ export type IntercomProviderProps = { * @remarks If not set delay is set to 0ms * */ initializeDelay?: number; + /** + * If intercom should be initialized using party town + */ + isPartyTown?: boolean; /** * Pass properties to `boot` method when `autoBoot` is `true` */