Skip to content

Commit 5321207

Browse files
Merge pull request #28 from Capchase/test-events
feat: implement href redirection + change button test to anchor
2 parents ca20424 + 27d4cad commit 5321207

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

segment-tracker/index.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,32 @@ function get_form_inputs(properties) {
1818
});
1919
}
2020

21+
function get_redirect(e){
22+
return e.currentTarget.getAttribute('href')
23+
}
24+
25+
2126
$(document).ready(function () {
27+
28+
// Create flags
29+
var button_event_triggered = false;
30+
2231
// capture a click on any element that has
23-
$("[data-analytics]").on("click", async function () {
32+
$("[data-analytics]").on("click", async function (e) {
33+
if (button_event_triggered) {
34+
button_event_triggered = false; // reset flag
35+
36+
const redirect_to = get_redirect(e);
37+
38+
if (redirect_to){
39+
window.location.href = redirect_to;
40+
}
41+
42+
return true; // let the event bubble away
43+
}
44+
45+
e.preventDefault();
46+
2447
// Get event name
2548
var event = $(this).attr("data-analytics");
2649

@@ -37,11 +60,15 @@ $(document).ready(function () {
3760
get_extra_attributes.call(this, properties);
3861
// Fire Segment event
3962
if ("analytics" in window) await analytics.track(event, properties);
63+
64+
button_event_triggered = true; // set flag
65+
$(this).trigger('click');
66+
4067
});
4168

4269

4370
// Add submit listener for all forms
44-
$("form").bind("submit", function (e) {
71+
$("form").on("submit", function () {
4572

4673
var properties = {
4774
// capture the URL where this event is fired

segment-tracker/test.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
</script>
1919
</head>
2020
<body>
21-
<button data-analytics="Button Clicked" data-property-some_prop="hey hey">
21+
<a data-analytics="Button Clicked" data-property-some_prop="hey hey" href="https://sandbox.capchase.com">
2222
Click me!
23-
</button>
23+
</a>
2424
<form id="formID" data-property-prop-form="added-prop" target="_blank">
2525
<label for="Email">Email:</label><br>
2626
<input type="text" id="Email" name="Email">

0 commit comments

Comments
 (0)