-
Notifications
You must be signed in to change notification settings - Fork 6
add trackLead and trackSale method
#62
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b28376b
add trackLead and trackSale method
devkiran 8a9adce
Update client-conversion-tracking.js
devkiran 61a0a82
process the queued events
devkiran 7928ab4
add to react package
devkiran 151723d
Update client-conversion-tracking.js
devkiran 905b11a
Update conversion-tracking.html
devkiran 2979be6
Update client-conversion-tracking.js
devkiran cb69fc1
Update client-conversion-tracking.js
devkiran 5ab2d80
rename to conversion-tracking
steven-tey 638befd
update build script to build all 8 variants
steven-tey 4ed4f83
Update package.json
steven-tey 15affd7
fallback to dub_id cookie for clickId
steven-tey 6e2e771
update examples with new syntax
steven-tey 5dac70b
bump version to 0.0.30
steven-tey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,66 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <meta name="description" content="Your website description" /> | ||
| <title>Client Conversion Tracking</title> | ||
| </head> | ||
| <body> | ||
| <script> | ||
| !(function (w, da) { | ||
| w[da] = | ||
| w[da] || | ||
| function () { | ||
| (w[da].q = w[da].q || []).push(arguments); | ||
| }; | ||
|
|
||
| ['trackClick', 'trackLead', 'trackSale'].forEach(function (m) { | ||
| w[da][m] = function () { | ||
| w[da](m, ...arguments); | ||
| }; | ||
| }); | ||
| })(window, 'dubAnalytics'); | ||
| </script> | ||
|
|
||
| <script> | ||
| // Add a 3-second delay before loading the script to test queue functionality | ||
| setTimeout(() => { | ||
| const script = document.createElement('script'); | ||
| script.defer = true; | ||
| script.src = './analytics/script.conversion-tracking.js'; | ||
| script.setAttribute('data-api-host', 'http://api.localhost:8888'); | ||
| script.setAttribute( | ||
| 'data-publishable-key', | ||
| 'dub_pk_BgyBCEJCPCGN3RN7oieLVHRs', | ||
| ); | ||
| document.head.appendChild(script); | ||
| }, 3000); | ||
| </script> | ||
|
|
||
| <header> | ||
| <h1>Client Conversion Tracking</h1> | ||
| <button | ||
| onclick="dubAnalytics.trackLead({ | ||
| eventName: 'Account created', | ||
| customerExternalId: '1234567890', | ||
| customerName: 'John Doe', | ||
| customerEmail: 'john.doe@example.com', | ||
| })" | ||
| > | ||
| Track Lead | ||
| </button> | ||
|
|
||
| <button | ||
| onclick="dubAnalytics.trackSale({ | ||
| eventName: 'Purchase completed', | ||
| customerExternalId: 'CXvG5QOLi8QKBA2jYmDh', | ||
| invoiceId: Math.random().toString(36).substring(2, 15), | ||
| amount: 5000, // defaults to usd cents, use `currency` prop to specify a different currency | ||
| })" | ||
| > | ||
| Track Sale | ||
| </button> | ||
| </header> | ||
| </body> | ||
| </html> |
This file contains hidden or 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,39 @@ | ||
| 'use client'; | ||
|
|
||
| import { useAnalytics } from '@dub/analytics/react'; | ||
|
|
||
| export function ConversionTrackingPageClient() { | ||
| const { trackLead, trackSale } = useAnalytics(); | ||
|
|
||
| const handleTrackLead = () => { | ||
| trackLead({ | ||
| eventName: 'Account created', | ||
| customerExternalId: '1234567890', | ||
| }); | ||
| }; | ||
|
|
||
| const handleTrackSale = () => { | ||
| trackSale({ | ||
| eventName: 'Purchase completed', | ||
| customerExternalId: 'CXvG5QOLi8QKBA2jYmDh', | ||
| amount: 5000, // defaults to usd cents, use `currency` prop to specify a different currency | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex gap-4 p-4"> | ||
| <button | ||
| onClick={handleTrackLead} | ||
| className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors" | ||
| > | ||
| Track Lead | ||
| </button> | ||
| <button | ||
| onClick={handleTrackSale} | ||
| className="px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600 transition-colors" | ||
| > | ||
| Track Sale | ||
| </button> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or 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,5 @@ | ||
| import { ConversionTrackingPageClient } from './page-client'; | ||
|
|
||
| export default function ConversionTrackingPage() { | ||
| return <ConversionTrackingPageClient />; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,104 @@ | ||
| const initConversionTracking = () => { | ||
| const { | ||
| a: API_HOST, | ||
| k: PUBLISHABLE_KEY, | ||
| c: cookieManager, | ||
| i: DUB_ID_VAR, | ||
| } = window._dubAnalytics || {}; | ||
|
|
||
| if (!API_HOST) { | ||
| console.warn('[dubAnalytics] Missing API_HOST'); | ||
| return; | ||
| } | ||
|
|
||
| if (!PUBLISHABLE_KEY) { | ||
| console.warn('[dubAnalytics] Missing PUBLISHABLE_KEY'); | ||
| return; | ||
| } | ||
|
|
||
| // Track lead conversion | ||
| const trackLead = async (input) => { | ||
| const clickId = cookieManager?.get(DUB_ID_VAR); | ||
|
|
||
| const requestBody = { | ||
| ...(clickId && { clickId }), | ||
| ...input, | ||
| }; | ||
|
|
||
| const response = await fetch(`${API_HOST}/track/lead/client`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${PUBLISHABLE_KEY}`, | ||
| }, | ||
| body: JSON.stringify(requestBody), | ||
| }); | ||
|
|
||
| const result = await response.json(); | ||
|
|
||
| if (!response.ok) { | ||
| console.error('[dubAnalytics] trackLead failed', result.error); | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| // Track sale conversion | ||
| const trackSale = async (input) => { | ||
| const response = await fetch(`${API_HOST}/track/sale/client`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${PUBLISHABLE_KEY}`, | ||
| }, | ||
| body: JSON.stringify(input), | ||
| }); | ||
|
|
||
| const result = await response.json(); | ||
|
|
||
| if (!response.ok) { | ||
| console.error('[dubAnalytics] trackSale failed', result.error); | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| // Add methods to the global dubAnalytics object for direct calls | ||
| if (window.dubAnalytics) { | ||
| window.dubAnalytics.trackLead = function (...args) { | ||
| trackLead(...args); | ||
| }; | ||
|
|
||
| window.dubAnalytics.trackSale = function (...args) { | ||
| trackSale(...args); | ||
| }; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // Process any existing queued conversion events | ||
| if (window._dubAnalytics && window._dubAnalytics.qm) { | ||
| const queueManager = window._dubAnalytics.qm; | ||
| const existingQueue = queueManager.queue || []; | ||
|
|
||
| const remainingQueue = existingQueue.filter(([method, ...args]) => { | ||
| if (method === 'trackLead') { | ||
| trackLead(...args); | ||
| return false; | ||
| } else if (method === 'trackSale') { | ||
| trackSale(...args); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }); | ||
|
|
||
| // Update the queue with remaining items | ||
| queueManager.queue = remainingQueue; | ||
| } | ||
| }; | ||
|
|
||
| // Run when base script is ready | ||
| if (window._dubAnalytics) { | ||
| initConversionTracking(); | ||
| } else { | ||
| window.addEventListener('load', initConversionTracking); | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.