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

Additional Delivery Preference SDK changes #293

Draft
wants to merge 17 commits into
base: develop
Choose a base branch
from
Draft
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
395 changes: 312 additions & 83 deletions clevertap.js

Large diffs are not rendered by default.

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.

2 changes: 1 addition & 1 deletion src/clevertap.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ export default class CleverTap {
data = this.#request.addSystemDataToObject(data, undefined)
data.cpg = currLocation

data[CAMP_COOKIE_NAME] = getCampaignObjForLc()
data[CAMP_COOKIE_NAME] = getCampaignObjForLc(this.#session)
let pageLoadUrl = this.#account.dataPostURL
this.#request.addFlags(data)
// send dsync flag when page = 1
Expand Down
2 changes: 1 addition & 1 deletion src/modules/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export default class RequestManager {
this.#addToLocalEventMap(data.evtName)
data = this.addSystemDataToObject(data, undefined)
this.addFlags(data)
data[CAMP_COOKIE_NAME] = getCampaignObjForLc()
data[CAMP_COOKIE_NAME] = getCampaignObjForLc(this.#session)
const compressedData = compressData(JSON.stringify(data), this.#logger)
let pageLoadUrl = this.#account.dataPostURL
pageLoadUrl = addToURL(pageLoadUrl, 'type', EVT_PUSH)
Expand Down
2 changes: 1 addition & 1 deletion src/util/campaignRender/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const appendScriptForCustomEvent = (targetingMsgJson, html) => {

export const staleDataUpdate = (staledata, campType) => {
const campObj = getCampaignObject()
const globalObj = campObj[campType].global
const globalObj = (campObj && Object.keys(campObj).length > 0) ? campObj[campType].global : {}
if (globalObj != null && campType) {
for (const idx in staledata) {
if (staledata.hasOwnProperty(idx)) {
Expand Down
165 changes: 124 additions & 41 deletions src/util/clevertap.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ export const getCampaignObject = () => {
return finalcampObj
}

export const saveCampaignObject = (campaignObj) => {
export const saveCampaignObject = (campaignObj, sessionId) => {
if (StorageManager._isLocalStorageSupported()) {
const newObj = { ...getCampaignObject(), ...campaignObj }
const campObj = JSON.stringify(newObj)
StorageManager.save(CAMP_COOKIE_NAME, encodeURIComponent(campObj))
// Update the CAMP_COOKIE_G to be in sync with CAMP_COOKIE_NAME
setCampaignObjectForGuid()
setCampaignObjectForGuid(sessionId)
}
}

// set Campaign Object against the guid, with daily count and total count details
export const setCampaignObjectForGuid = () => {
export const setCampaignObjectForGuid = (sessionId, newOcData = [], wpTc = false) => {
if (StorageManager._isLocalStorageSupported()) {
let guid = StorageManager.read(GCOOKIE_NAME)
if (isValueValid(guid)) {
Expand All @@ -84,35 +84,86 @@ export const setCampaignObjectForGuid = () => {
if (guid && StorageManager._isLocalStorageSupported()) {
var finalCampObj = {}
var campObj = getCampaignObject()
const campWPObj = (guid in guidCampObj && Object.keys(guidCampObj[guid]).length && guidCampObj[guid].wp) ? guidCampObj[guid].wp : {}
const today = getToday()
if (newOcData.length > 0) {
newOcData.forEach((campaignId) => {
if (!campWPObj[campaignId] || (campWPObj[campaignId].ts === undefined && campWPObj[campaignId].oc === undefined)) {
campWPObj[campaignId] = { ts: [], oc: 0 }
}
campWPObj[campaignId].oc += 1 // Increment oc count
})
finalCampObj.wp = campWPObj
guidCampObj[guid] = finalCampObj
StorageManager.save(CAMP_COOKIE_G, encodeURIComponent(JSON.stringify(guidCampObj)))
return
}
if (wpTc) {
campWPObj.wp_tc = campWPObj.wp_tc || {}
Object.keys(campWPObj.wp_tc).forEach(previousDay => {
if (previousDay !== today) {
// Remove stale session ID
delete campWPObj.wp_tc[previousDay]
}
})
campWPObj.wp_tc[today] = (campWPObj.wp_tc[today] || 0) + 1
finalCampObj.wp = campWPObj
guidCampObj[guid] = finalCampObj
StorageManager.save(CAMP_COOKIE_G, encodeURIComponent(JSON.stringify(guidCampObj)))
return
}

Object.keys(campObj).forEach(key => {
const campKeyObj = (guid in guidCampObj && Object.keys(guidCampObj[guid]).length && guidCampObj[guid][key]) ? guidCampObj[guid][key] : {}
const globalObj = campObj[key].global
const today = getToday()
const dailyObj = campObj[key][today]
if (typeof globalObj !== 'undefined') {
const campaignIdArray = Object.keys(globalObj)
for (const index in campaignIdArray) {
let resultObj = []
if (campaignIdArray.hasOwnProperty(index)) {
let dailyC = 0
let totalC = 0
const campaignId = campaignIdArray[index]
if (campaignId === 'tc') {
continue
}
if (typeof dailyObj !== 'undefined' && typeof dailyObj[campaignId] !== 'undefined') {
dailyC = dailyObj[campaignId]
if (key === 'wp') {
Object.keys(globalObj || {}).forEach(campaignId => {
if (campaignId === 'wp_sc') {
if (campObj.wp?.global?.wp_sc?.[sessionId]) {
campKeyObj.wp_sc = campKeyObj.wp_sc || {}
Object.keys(campKeyObj.wp_sc).forEach(existingSessionId => {
if (existingSessionId !== sessionId) {
// Remove stale session ID
delete campKeyObj.wp_sc[existingSessionId]
}
})
campKeyObj.wp_sc[sessionId] = campObj.wp?.global?.wp_sc?.[sessionId] // Increment session count
}
if (typeof globalObj !== 'undefined' && typeof globalObj[campaignId] !== 'undefined') {
totalC = globalObj[campaignId]
}
resultObj = [campaignId, dailyC, totalC]
campKeyObj[campaignId] = resultObj
}
}
if (campaignId === 'tc' || campaignId === 'wp_tc' || campaignId === 'wp_sc') return
const campaignData = globalObj[campaignId]

// Initialize or update ts (timestamps) and oc (occurrence count) for each campaign
if (!campKeyObj[campaignId] || (campKeyObj[campaignId].ts === undefined && campKeyObj[campaignId].oc === undefined)) {
campKeyObj[campaignId] = { ts: [], oc: 0 }
}
// Add timestamps for the specific campaign in `global`
if (campKeyObj[campaignId].ts) {
campKeyObj[campaignId].ts = Array.from(
new Set([
...campKeyObj[campaignId].ts,
...(Array.isArray(campaignData.ts) ? campaignData.ts : [])
])
)
}
})
} else {
// Handle wi (web inbox) campaigns without new changes
Object.keys(globalObj || {}).forEach(campaignId => {
if (campaignId === 'tc') return // Skip total count (tc)

const dailyC = campObj[key]?.[today]?.[campaignId] || 0
const totalC = globalObj?.[campaignId] || 0
const resultObj = [campaignId, dailyC, totalC]

campKeyObj[campaignId] = resultObj
})
}
finalCampObj = { ...finalCampObj, [key]: campKeyObj }

finalCampObj[key] = campKeyObj
})

// Save updated campaign object back to storage
guidCampObj[guid] = finalCampObj
StorageManager.save(CAMP_COOKIE_G, encodeURIComponent(JSON.stringify(guidCampObj)))
}
Expand All @@ -122,24 +173,26 @@ export const setCampaignObjectForGuid = () => {
}
}
}
export const getCampaignObjForLc = () => {
// before preparing data to send to LC , check if the entry for the guid is already there in CAMP_COOKIE_G
export const getCampaignObjForLc = (session) => {
const sessionID = session?.scookieObj?.s
// Retrieve the GUID from storage
const guid = JSON.parse(decodeURIComponent(StorageManager.read(GCOOKIE_NAME)))

let campObj = {}
if (StorageManager._isLocalStorageSupported()) {
let resultObj = {}
campObj = getCampaignObject()
const storageValue = StorageManager.read(CAMP_COOKIE_G)
const campObj = getCampaignObject() // Fetch current campaign object from local storage (CAMP_COOKIE_NAME)
const storageValue = StorageManager.read(CAMP_COOKIE_G) // Fetch from CAMP_COOKIE_G (wzrk_camp_G)
const decodedValue = storageValue ? decodeURIComponent(storageValue) : null
const parsedValue = decodedValue ? JSON.parse(decodedValue) : null

const resultObjWP = (!!guid &&
storageValue !== undefined && storageValue !== null &&
parsedValue && parsedValue[guid] && parsedValue[guid].wp)
? Object.values(parsedValue[guid].wp)
: []
// Retrieve wp (web popup) data from wzrk_camp_G
const wpData = (!!guid &&
storageValue !== undefined && storageValue !== null &&
parsedValue && parsedValue[guid] && parsedValue[guid].wp)
? parsedValue[guid].wp
: {}

// Retrieve wi (web inbox) data from wzrk_camp_G using the old method
const resultObjWI = (!!guid &&
storageValue !== undefined && storageValue !== null &&
parsedValue && parsedValue[guid] && parsedValue[guid].wi)
Expand All @@ -149,18 +202,47 @@ export const getCampaignObjForLc = () => {
const today = getToday()
let todayCwp = 0
let todayCwi = 0
if (campObj.wp && campObj.wp[today] && campObj.wp[today].tc !== 'undefined') {
todayCwp = campObj.wp[today].tc

// Get today's web popup total count (wp_tc) from wzrk_camp_G
if (wpData.wp_tc && wpData.wp_tc[today] !== undefined) {
todayCwp = wpData.wp_tc[today]
}

// Get today's web inbox total count from wzrk_camp (not touched, old method)
if (campObj.wi && campObj.wi[today] && campObj.wi[today].tc !== 'undefined') {
todayCwi = campObj.wi[today].tc
}

// Prepare the occurrence count (oc) and frequency count (fc) for web popups from wzrk_camp_G
const oc = {}
const fc = {}

if (wpData) {
Object.keys(wpData).forEach(campaignId => {
if (wpData[campaignId].oc) {
oc[campaignId] = wpData[campaignId].oc // Add occurrence count for each campaign
}
if (wpData[campaignId].ts) {
fc[campaignId] = wpData[campaignId].ts // Add frequency count (timestamps) for each campaign
}
})
}

// Retrieve wp_sc (session count) from wzrk_camp_G
const wpSc = wpData.wp_sc?.[sessionID] || 0

// Prepare the result object with the required fields for the backend
resultObj = {
wmp: todayCwp,
wimp: todayCwi,
tlc: resultObjWP,
witlc: resultObjWI

wmp: todayCwp, // Web popup total count for the day (wp_tc)
wimp: todayCwi, // Web inbox total count for the day (witlc)
witlc: resultObjWI, // Unchanged: Old way of processing web inbox lifetime count
wsc: wpSc, // Web popup session count (wp_sc)
woc: oc, // Occurrence count for campaigns
wfc: fc // Frequency count (timestamps) for campaigns

}

return resultObj
}
}
Expand Down Expand Up @@ -421,6 +503,7 @@ export const closeIframe = (campaignId, divIdIgnored, currentSessionId) => {
campaignObj[currentSessionId] = sessionCampaignObj
}
sessionCampaignObj[campaignId] = 'dnd'

saveCampaignObject(campaignObj)
}
}
Expand Down
Loading
Loading