|
89 | 89 | } |
90 | 90 |
|
91 | 91 | currentRef = currentUrl; |
92 | | - currentUrl = normalize(new URL(url, location.href).toString()); |
| 92 | + currentUrl = normalize(url); |
93 | 93 |
|
94 | 94 | if (currentUrl !== currentRef && autoPageview) { |
95 | 95 | setTimeout(track, delayDuration); |
|
100 | 100 | const hook = (_this, method, callback) => { |
101 | 101 | const orig = _this[method]; |
102 | 102 | return (...args) => { |
| 103 | + const result = orig.apply(_this, args); |
103 | 104 | callback.apply(null, args); |
104 | | - return orig.apply(_this, args); |
| 105 | + return result; |
105 | 106 | }; |
106 | 107 | }; |
107 | 108 |
|
|
123 | 124 | return track(eventName, eventData); |
124 | 125 | } |
125 | 126 | }; |
126 | | - const onClick = async e => { |
| 127 | + const onClick = e => { |
127 | 128 | const el = e.target; |
128 | | - const parentElement = el.closest('a,button'); |
129 | | - if (!parentElement) return trackElement(el); |
| 129 | + const eventEl = el.closest(`[${eventNameAttribute}]`); |
| 130 | + if (!eventEl) return; |
130 | 131 |
|
131 | | - const { href, target } = parentElement; |
132 | | - if (!parentElement.getAttribute(eventNameAttribute)) return; |
133 | | - |
134 | | - if (parentElement.tagName === 'BUTTON') { |
135 | | - return trackElement(parentElement); |
136 | | - } |
137 | | - if (parentElement.tagName === 'A' && href) { |
| 132 | + if (eventEl.tagName === 'A' && eventEl.href) { |
| 133 | + const { href, target } = eventEl; |
138 | 134 | const external = |
139 | 135 | target === '_blank' || |
140 | 136 | e.ctrlKey || |
141 | 137 | e.shiftKey || |
142 | 138 | e.metaKey || |
143 | 139 | (e.button && e.button === 1); |
144 | 140 | if (!external) e.preventDefault(); |
145 | | - return trackElement(parentElement).then(() => { |
| 141 | + return trackElement(eventEl).then(() => { |
146 | 142 | if (!external) { |
147 | 143 | (target === '_top' ? top.location : location).href = href; |
148 | 144 | } |
149 | 145 | }); |
150 | 146 | } |
| 147 | + |
| 148 | + return trackElement(eventEl); |
151 | 149 | }; |
152 | 150 | document.addEventListener('click', onClick, true); |
153 | 151 | }; |
|
0 commit comments