-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from getamis/ASL-4736/custom-track-settings
[ASL-4736] custom track settings
- Loading branch information
Showing
7 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const { gtag } = window as any; | ||
|
||
function getClientId(tagId: string): Promise<string> { | ||
return new Promise(resolve => { | ||
if (!gtag) resolve(''); | ||
|
||
gtag('get', tagId, 'client_id', (clientId: string) => { | ||
resolve(clientId); | ||
}); | ||
}); | ||
} | ||
|
||
function getSessionId(tagId: string): Promise<string> { | ||
return new Promise(resolve => { | ||
if (!gtag) resolve(''); | ||
gtag('get', tagId, 'session_id', (sessionId: string) => { | ||
resolve(sessionId); | ||
}); | ||
}); | ||
} | ||
|
||
let cachedGaTrackSettings: string[] = []; | ||
export async function initGaTrack(measurementIds: string[]): Promise<void> { | ||
if (!gtag && measurementIds.length > 0) { | ||
console.warn('window.gtag not found, you should set up gtag in current web to use this function'); | ||
return; | ||
} | ||
cachedGaTrackSettings = ( | ||
await Promise.all( | ||
measurementIds.map(async measurementId => { | ||
const clientId = await getClientId(measurementId); | ||
const sessionId = await getSessionId(measurementId); | ||
if (!clientId) { | ||
console.error('clientId not found'); | ||
return ''; | ||
} | ||
if (!sessionId) { | ||
console.error('sessionId not found'); | ||
return ''; | ||
} | ||
return `${measurementId},${clientId},${sessionId}`; | ||
}), | ||
) | ||
).filter(item => !!item); | ||
} | ||
|
||
export function addTrackSettingsToUrl(url: string): string { | ||
const paymentUrlObject = new URL(url); | ||
const params = new URLSearchParams(paymentUrlObject.search); | ||
|
||
// https://url?track_ga=setting1&track_ga=setting2 | ||
cachedGaTrackSettings.forEach(each => { | ||
params.append('track_ga', each); | ||
}); | ||
|
||
paymentUrlObject.search = params.toString(); | ||
return paymentUrlObject.href; | ||
} |