Skip to content

Commit

Permalink
feat(router): page tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
aliwesome committed Dec 13, 2024
1 parent f4b88fc commit 9e59688
Show file tree
Hide file tree
Showing 4 changed files with 4,064 additions and 174 deletions.
3 changes: 2 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module.exports = async function (moduleOptions) {
enabled: true,
debug: false,
accountId: null,
// Add other Drip-specific options
trackPages: false,
respectDoNotTrack: true
}

// Merge options
Expand Down
59 changes: 54 additions & 5 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,82 @@ export default function (ctx, inject) {
// Parse options from module configuration
const options = JSON.parse('<%= JSON.stringify(options) %>')

// Drip snippet
const initDrip = () => {
if (typeof window._dcq === 'undefined') {
window._dcq = window._dcq || []
window._dcat = window._dcat || []
window._dcq.push(['setAccount', options.accountId])

const dc = document.createElement('script')
dc.type = 'text/javascript'
dc.async = true
dc.src = `https://tag.getdrip.com/${options.accountId}.js`
const s = document.getElementsByTagName('script')[0]
s.parentNode.insertBefore(dc, s)
}
}

// Create module API
const moduleAPI = {
// Initialize Drip
init() {
if (options.debug) {
console.log('Debug: Initializing Drip')
}
// Drip initialization code
if (!options.enabled) {
if (options.debug) {
console.log('Debug: Drip is disabled')
}
return
}
initDrip()
},

// Track event method
track(event, properties = {}) {
if (options.debug) {
console.log('Debug: Tracking event', event, properties)
}
// Drip tracking implementation
if (!window._dcq) {
console.warn('Drip is not initialized')
return
}
window._dcq.push(['track', event, properties])
},

// Identify user method
identify(userId, traits = {}) {
identify(email, properties = {}) {
if (options.debug) {
console.log('Debug: Identifying user', userId, traits)
console.log('Debug: Identifying user', email, properties)
}
// Drip identify implementation
if (!window._dcq) {
console.warn('Drip is not initialized')
return
}
window._dcq.push(['identify', { email, ...properties }])
}
}

// Inject module
inject('drip', moduleAPI)

// Auto-initialize if enabled
if (options.enabled && process.client) {
// Wait for Vue to be ready
ctx.app.router.onReady(() => {
moduleAPI.init()
})

// Track page views if enabled
if (options.trackPages) {
ctx.app.router.afterEach((to) => {
moduleAPI.track('Viewed Page', {
path: to.fullPath,
url: window.location.origin + to.fullPath,
title: document.title
})
})
}
}
}
Loading

0 comments on commit 9e59688

Please sign in to comment.