Skip to content

Instantly share code, notes, and snippets.

@kmclaugh
Last active March 7, 2022 15:49
Show Gist options
  • Save kmclaugh/2251a04df8b1d1dec7e848f6d30c4d42 to your computer and use it in GitHub Desktop.
Save kmclaugh/2251a04df8b1d1dec7e848f6d30c4d42 to your computer and use it in GitHub Desktop.
segment click tracking handler
import { eventObserver } from 'ui-event-observer'
const clickHandler = (event) => {
const {
target: { href, innerText, dataset },
} = event
if (dataset && dataset.action) {
const { object, action } = dataset
const eventName = `${object} ${action}`
track(eventName, {
click_url: href,
click_text: innerText,
...dataset,
})
console.log(dataset, event)
} else if (href) {
if (href.includes('/login')) {
// do something
track('Login Clicked', {
object: 'Login',
action: 'Clicked',
click_url: href,
category: 'Login',
label: href,
})
} else if (href.includes('.pdf')) {
// do something
track('PDF Clicked', {
object: 'PDF',
action: 'Clicked',
click_url: href,
category: 'PDF',
label: href,
})
} else if (href.includes('mailto:')) {
// do something
track('Mailto Clicked', {
object: 'Mailto',
action: 'Clicked',
click_url: href,
category: 'Mailto',
label: href,
})
} else if (href.includes('tel:')) {
// do something
track('Telephone Clicked', {
object: 'Telephone',
action: 'Clicked',
click_url: href,
category: 'Telephone',
label: href,
})
} else if (
!href.includes('sendowl.com') &&
!href.includes('localhost')
) {
// do something
track('Outbound Link Clicked', {
object: 'Outbound Link',
action: 'Clicked',
click_url: href,
category: 'Outbound Link',
label: href,
})
}
}
}
eventObserver.subscribe(window, 'click', clickHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment