Skip to content

Adding party town as an option #742

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/fifty-spiders-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-use-intercom': minor
---

Adding party town as an option
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/> 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
Expand Down
20 changes: 18 additions & 2 deletions packages/react-use-intercom/src/initialize.ts
Original file line number Diff line number Diff line change
@@ -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') {
Expand All @@ -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') {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-use-intercom/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const IntercomProvider: React.FC<
shouldInitialize = !isSSR,
apiBase,
initializeDelay,
isPartyTown = false,
...rest
}) => {
const isBooted = React.useRef(false);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions packages/react-use-intercom/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
*/
Expand Down
Loading