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

내정보 페이지 채널톡 연동 #417

Merged
merged 2 commits into from
Nov 2, 2024
Merged
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
13 changes: 12 additions & 1 deletion apps/web/src/layout/GlobalLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import { css } from "@emotion/react";
import Hotjar from "@hotjar/browser";
import { PATHS } from "@layer/shared";
import { useEffect } from "react";
import { Outlet } from "react-router-dom";
import { Outlet, useLocation } from "react-router-dom";

import { Modal } from "@/component/common/Modal";
import { PreventExternalBrowser } from "@/helper/preventExternalBrowser.ts";
import ChannelService from "@/lib/channel-talk/service";
import { useBridge } from "@/lib/provider/bridge-provider";

const siteId = import.meta.env.VITE_HOTJAR_KEY as number;
const hotjarVersion = import.meta.env.VITE_HOTJAR_VERSION as number;

export default function GlobalLayout() {
const { safeAreaHeight } = useBridge();
const location = useLocation();

useEffect(() => {
Hotjar.init(siteId, hotjarVersion);
}, []);

useEffect(() => {
if (location.pathname.startsWith(PATHS.myInfo())) {
ChannelService.showChannelButton();
} else {
ChannelService.hideChannelButton();
}
}, [location]);

Comment on lines +23 to +30
Copy link
Member

Choose a reason for hiding this comment

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

location 값에 따라 코드로 채널톡 버튼을 숨겨주는 것도 좋은 방법이지만,
혹시 채널톡에서 제공하는 화이트리스트 관리 기능을 사용해보면 어떨까요?!

혹여나 변동 사항이 생기게 된다면 다시 소스 코드를 고치고 배포하는 과정이 필요할 것 같은데 채널톡 백오피스에서 제공하는 화이트리스트를 사용하게 되면 조금 더 과정이 편해질 것 같다는 생각이 듭니다!

스크린샷 2024-11-01 오후 5 57 57

Copy link
Member Author

@leeminhee119 leeminhee119 Nov 2, 2024

Choose a reason for hiding this comment

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

앗 요거 설정해봤는데, 채널톡이 SPA 환경에서 URL 변경되는 걸 인지하지 못해서 아래와 같이 제대로 작동하지 않더라구요!

  • /myinfo 경로로 최초 진입하면 채널톡 버튼이 뜨는데 홈으로 이동 시 사라지지 않음
  • 반대로 홈 화면 경로로 최초 진입 후 내정보로 진입 시 버튼이 뜨지 않음

그래서 사이트 최초 진입 시에 채널톡 부트시키고, 경로 이동 시 버튼만 hide/show로 설정해두었습니다.
혹시 제가 놓친게 있다면 알려주세요! 😸

Copy link
Member

Choose a reason for hiding this comment

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

앗 코드를 보니까 그러네요!
생각해보니 예전에 채널톡 화이트리스트를 적용했던 프로젝트는 Next로 구성된 프로젝트였던 기억이..

Copy link
Member Author

Choose a reason for hiding this comment

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

네네 일단 요렇게 머지하께요!

return (
<div
css={css`
Expand Down
200 changes: 200 additions & 0 deletions apps/web/src/lib/channel-talk/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
declare global {
interface Window {
ChannelIO?: IChannelIO;
ChannelIOInitialized?: boolean;
}
}

interface IChannelIO {
c?: (...args: any) => void;
q?: [methodName: string, ...args: any[]][];
(...args: any): void;
}

interface BootOption {
appearance?: string;
customLauncherSelector?: string;
hideChannelButtonOnBoot?: boolean;
hidePopup?: boolean;
language?: string;
memberHash?: string;
memberId?: string;
pluginKey: string;
profile?: Profile;
trackDefaultEvent?: boolean;
trackUtmSource?: boolean;
unsubscribe?: boolean;
unsubscribeEmail?: boolean;
unsubscribeTexting?: boolean;
zIndex?: number;
}

interface Callback {
(error: Error | null, user: CallbackUser | null): void;
}

interface CallbackUser {
alert: number;
avatarUrl: string;
id: string;
language: string;
memberId: string;
name?: string;
profile?: Profile | null;
tags?: string[] | null;
unsubscribeEmail: boolean;
unsubscribeTexting: boolean;
}

interface UpdateUserInfo {
language?: string;
profile?: Profile | null;
profileOnce?: Profile;
tags?: string[] | null;
unsubscribeEmail?: boolean;
unsubscribeTexting?: boolean;
}

interface Profile {
[key: string]: string | number | boolean | null | undefined;
}

interface FollowUpProfile {
name?: string | null;
mobileNumber?: string | null;
email?: string | null;
}

interface EventProperty {
[key: string]: string | number | boolean | null | undefined;
}

type Appearance = "light" | "dark" | "system" | null;

class ChannelService {
loadScript() {
(function () {
const w = window;
if (w.ChannelIO) {
return w.console.error("ChannelIO script included twice.");
}
const ch: IChannelIO = function () {
// eslint-disable-next-line prefer-rest-params
ch.c?.(arguments);
};
ch.q = [];
ch.c = function (args) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
ch.q?.push(args);
};
w.ChannelIO = ch;
function l() {
if (w.ChannelIOInitialized) {
return;
}
w.ChannelIOInitialized = true;
const s = document.createElement("script");
s.type = "text/javascript";
s.async = true;
s.src = "https://cdn.channel.io/plugin/ch-plugin-web.js";
const x = document.getElementsByTagName("script")[0];
if (x.parentNode) {
x.parentNode.insertBefore(s, x);
}
}
if (document.readyState === "complete") {
l();
} else {
w.addEventListener("DOMContentLoaded", l);
w.addEventListener("load", l);
}
})();
}

boot(option: BootOption, callback?: Callback) {
window.ChannelIO?.("boot", option, callback);
}

shutdown() {
window.ChannelIO?.("shutdown");
}

showMessenger() {
window.ChannelIO?.("showMessenger");
}

hideMessenger() {
window.ChannelIO?.("hideMessenger");
}

openChat(chatId?: string | number, message?: string) {
window.ChannelIO?.("openChat", chatId, message);
}

track(eventName: string, eventProperty?: EventProperty) {
window.ChannelIO?.("track", eventName, eventProperty);
}

onShowMessenger(callback: () => void) {
window.ChannelIO?.("onShowMessenger", callback);
}

onHideMessenger(callback: () => void) {
window.ChannelIO?.("onHideMessenger", callback);
}

onBadgeChanged(callback: (unread: number, alert: number) => void) {
window.ChannelIO?.("onBadgeChanged", callback);
}

onChatCreated(callback: () => void) {
window.ChannelIO?.("onChatCreated", callback);
}

onFollowUpChanged(callback: (profile: FollowUpProfile) => void) {
window.ChannelIO?.("onFollowUpChanged", callback);
}

onUrlClicked(callback: (url: string) => void) {
window.ChannelIO?.("onUrlClicked", callback);
}

clearCallbacks() {
window.ChannelIO?.("clearCallbacks");
}

updateUser(userInfo: UpdateUserInfo, callback?: Callback) {
window.ChannelIO?.("updateUser", userInfo, callback);
}

addTags(tags: string[], callback?: Callback) {
window.ChannelIO?.("addTags", tags, callback);
}

removeTags(tags: string[], callback?: Callback) {
window.ChannelIO?.("removeTags", tags, callback);
}

setPage(page: string) {
window.ChannelIO?.("setPage", page);
}

resetPage() {
window.ChannelIO?.("resetPage");
}

showChannelButton() {
window.ChannelIO?.("showChannelButton");
}

hideChannelButton() {
window.ChannelIO?.("hideChannelButton");
}

setAppearance(appearance: Appearance) {
window.ChannelIO?.("setAppearance", appearance);
}
}

export default new ChannelService();
7 changes: 6 additions & 1 deletion apps/web/src/router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { SetNickNamePage } from "@/app/login/SetNicknamePage";
import { RetrospectAnalysisPage } from "@/app/retrospect/analysis/RetrospectAnalysisPage";
import { TemplateListPage } from "@/app/retrospect/template/list/TemplateListPage";
import { RecommendDonePage } from "@/app/retrospect/template/recommend/RecommendDonePage";
import { RecommendSearch } from "@/app/retrospect/template/recommend/RecommendSearch";
import { RecommendTemplatePage } from "@/app/retrospect/template/recommend/RecommendTemplatePage";
import { RetrospectCreate } from "@/app/retrospectCreate/RetrospectCreate";
import { RetrospectCreateComplete } from "@/app/retrospectCreate/RetrospectCreateComplete";
Expand All @@ -40,7 +41,7 @@ import { RetrospectWritePage } from "@/app/write/RetrospectWritePage.tsx";
import GlobalLayout from "@/layout/GlobalLayout.tsx";
import { HomeLayout } from "@/layout/HomeLayout";
import { RequireLoginLayout } from "@/layout/RequireLoginLayout";
import { RecommendSearch } from "@/app/retrospect/template/recommend/RecommendSearch";
import ChannelService from "@/lib/channel-talk/service";

type RouteChildren = {
auth: boolean;
Expand Down Expand Up @@ -256,5 +257,9 @@ const router = createBrowserRouter([
]);

export const Routers = () => {
ChannelService.loadScript();
ChannelService.boot({
pluginKey: import.meta.env.VITE_CHANNELTALK_PLUGIN_KEY,
Copy link
Member

Choose a reason for hiding this comment

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

환경 변수 공유해주시면, 플랫폼에도 잘 등록을 해놓겠습니다 :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

넵!

});
return <RouterProvider router={router} />;
};
Loading