Last active
February 2, 2022 14:42
-
-
Save phylliswong/bbbf9a24d7644617d0a5b9fe4a8515f2 to your computer and use it in GitHub Desktop.
FragranceX Event Emit
This file contains hidden or 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
function sendEvent(tag, url){ | |
try { | |
window.evolv.client.emit(tag); | |
} catch (e) { | |
console.log(e); | |
} | |
} | |
function pageMatch(page) { | |
if (!page) return false; | |
return new RegExp(page).test(location.pathname); | |
} | |
function processEvents(config){ | |
if (!!config.events) { | |
// Match event keys as object values | |
config.events.forEach(function(event) { | |
var matches = pageMatch(event.page); | |
if (matches) { | |
sendEvent(event.eventKey); | |
} | |
}); | |
} else { | |
// Match event keys as object keys | |
var events = Object.keys(config); | |
events.forEach(function(event){ | |
var conditions = config[event]; | |
var matches = pageMatch(conditions.page) | |
if (matches){ | |
sendEvent(event); | |
} | |
}) | |
} | |
} | |
module.exports = processEvents; |
This file contains hidden or 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
{ | |
"name": "eventEmit", | |
"main": "eventEmit.js", | |
"version": "0.1.1", | |
"description": "", | |
"license": "ISC" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment