-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hiive events via HTML attributes
- Loading branch information
Showing
2 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,58 @@ | ||
{ | ||
/** | ||
* When a user clicks on something that has a `data-nfd-click` attribute, send an event to the API endpoint. | ||
* | ||
* @param {Event} e The event object. | ||
*/ | ||
const processEvent = (e) => { | ||
// If it's a link, prevent the default behavior until the event is sent. | ||
if (e.target.tagName === 'A') { | ||
e.preventDefault(); | ||
} | ||
|
||
const target = e.target; | ||
const eventId = target.getAttribute('data-nfd-click'); | ||
const eventKey = target.getAttribute('data-nfd-event-key') || eventId; | ||
const eventCategory = target.getAttribute('data-nfd-event-category') || 'plugin'; | ||
const brand = target.getAttribute('data-nfd-brand') || window.nfdHiiveEvents.brand; | ||
const queue = target.getAttribute('data-nfd-queue') || false; | ||
|
||
const eventData = { | ||
action: eventId, | ||
category: eventCategory, | ||
queue, | ||
data: { | ||
label_key: eventKey, | ||
brand, | ||
page: window.location.href, | ||
} | ||
}; | ||
|
||
// To handle marketplace items. | ||
if (target.getAttribute('data-nfd-product-id')) { | ||
eventData.data.product_id = target.getAttribute('data-nfd-product-id'); | ||
} | ||
|
||
// Send event to the API | ||
window.wp.apiFetch({ | ||
url: window.nfdHiiveEvents.eventEndpoint, | ||
method: 'POST', | ||
data: eventData | ||
}).catch((error) => { | ||
console.error('Error sending event to API', error); | ||
}); | ||
|
||
// Send user to the link. | ||
if (e.target.tagName === 'A') { | ||
window.location.href = e.target.href; | ||
} | ||
}; | ||
|
||
window.addEventListener('load', () => { | ||
document.querySelector('#wpwrap').addEventListener('click', (e) => { | ||
if (e.target.hasAttribute('data-nfd-click')) { | ||
processEvent(e); | ||
} | ||
}); | ||
}); | ||
} |