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

Preview web custom html #358

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 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
39 changes: 34 additions & 5 deletions clevertap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion clevertap.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion clevertap.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/clevertap.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { Variable } from './modules/variables/variable'
import VariableStore from './modules/variables/variableStore'
import { addAntiFlicker, handleActionMode } from './modules/visualBuilder/pageBuilder'
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add the details about the changes in PR Template.
Changelog and version bump is missing

Copy link
Contributor

@singhkunal2050 singhkunal2050 Feb 20, 2025

Choose a reason for hiding this comment

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

Also please squash the commits into 2-3 meaningful commits to avoid rebasing issues for fellow collaborators.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will squash the commits and add change log and version bump before merging the PR to develop as I don't know when to merge this

import { setServerKey } from './modules/webPushPrompt/prompt'
import { checkCustomHtmlNativeDisplayPreview } from './util/campaignRender/nativeDisplay'

export default class CleverTap {
#logger
Expand Down Expand Up @@ -92,7 +93,7 @@ export default class CleverTap {
constructor (clevertap = {}) {
this.#onloadcalled = 0
this._isPersonalisationActive = this._isPersonalisationActive.bind(this)
this.raiseNotificationClicked = () => { }
this.raiseNotificationClicked = () => {}
this.#logger = new Logger(logLevels.INFO)
this.#account = new Account(clevertap.account?.[0], clevertap.region || clevertap.account?.[1], clevertap.targetDomain || clevertap.account?.[2], clevertap.token || clevertap.account?.[3])
this.#device = new DeviceManager({ logger: this.#logger })
Expand Down Expand Up @@ -641,6 +642,7 @@ export default class CleverTap {
this.#logger.debug('CT Initialized with Account ID: ' + this.#account.id)
}
handleActionMode(this.#logger, this.#account.id)
checkCustomHtmlNativeDisplayPreview(this.#logger)
this.#session.cookieName = SCOOKIE_PREFIX + '_' + this.#account.id

if (region) {
Expand Down
33 changes: 29 additions & 4 deletions src/util/campaignRender/nativeDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ export const handleKVpairCampaign = (targetingMsgJson) => {
document.dispatchEvent(kvPairsEvent)
}

export const renderCustomHtml = (targetingMsgJson, logger) => {
export const renderCustomHtml = (targetingMsgJson) => {
const { display, wzrk_id: wzrkId, wzrk_pivot: wzrkPivot } = targetingMsgJson || {}

const divId = display.divId || {}
const { divId } = display || {}
const details = display.details[0]
const html = details.html

if (!divId || !html) {
logger.error('No div Id or no html found')
console.error('No div Id or no html found')
return
}

Expand All @@ -75,7 +75,7 @@ export const renderCustomHtml = (targetingMsgJson, logger) => {
retryElement.outerHTML = html
clearInterval(intervalId)
} else if (++count >= 20) {
logger.log(`No element present on DOM with divId '${divId}'.`)
console.log(`No element present on DOM with divId '${divId}'.`)
clearInterval(intervalId)
}
}, 500)
Expand All @@ -98,3 +98,28 @@ export const handleJson = (targetingMsgJson) => {
const jsonEvent = new CustomEvent('CT_web_native_display_json', { detail: inaObj })
document.dispatchEvent(jsonEvent)
}

export const checkCustomHtmlNativeDisplayPreview = (logger) => {
const searchParams = new URLSearchParams(window.location.search)
const ctType = searchParams.get('ctActionMode')
if (ctType) {
const parentWindow = window.opener
switch (ctType) {
case 'ctCustomHtmlPreview':
if (parentWindow) {
parentWindow.postMessage('ready', '*')
window.addEventListener('message', (event) => {
const eventData = JSON.parse(event.data)
const inAppNotifs = eventData.inapp_notifs
const msgContent = inAppNotifs[0].msgContent
if (eventData && msgContent.templateType === 'custom-html' && msgContent.type === 5) {
renderCustomHtml(inAppNotifs[0])
}
}, false)
}
break
default:
break
}
}
}