Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #34 from zarathustra323/leaders-click-emitter
Browse files Browse the repository at this point in the history
Create Leaders click event emitter
  • Loading branch information
brandonbk authored Mar 2, 2021
2 parents 875582e + 7465f37 commit 9311686
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 1 deletion.
50 changes: 50 additions & 0 deletions packages/marko-web-leaders/browser/click-emitter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<template>
<div class="marko-web-leaders-click-emitter" style="display: none;" />
</template>

<script>
import { delegate } from 'dom-utils';
export default {
data: () => ({
listener: null,
}),
created() {
this.listener = delegate(
document,
'click',
'a[data-leaders-action]',
this.emit.bind(this),
);
},
beforeDestroy() {
if (this.listener) this.listener.destroy();
},
methods: {
emit(_, link) {
const event = {
type: link.getAttribute('data-leaders-action'),
category: link.getAttribute('data-leaders-category'),
label: link.getAttribute('data-leaders-label'),
};
const payload = {
...this.parseJSON(link.getAttribute('data-leaders-payload')),
href: link.getAttribute('href'),
};
this.$emit('action', event, payload);
},
parseJSON(value) {
try {
return JSON.parse(value) || {};
} catch (e) {
return {};
}
},
},
};
</script>
2 changes: 1 addition & 1 deletion packages/marko-web-leaders/browser/gtm-tracker.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class="marko-web-leaders-gtm-tracker" />
<div class="marko-web-leaders-gtm-tracker" style="display: none;" />
</template>

<script>
Expand Down
4 changes: 4 additions & 0 deletions packages/marko-web-leaders/browser/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const LeadersProgram = () => import(/* webpackChunkName: "leaders-program" */ '@parameter1/base-cms-leaders-program');
const LeadersClickEmitter = () => import(/* webpackChunkName: "leaders-click-emitter" */ './click-emitter.vue');
const LeadersCompanyWebsiteLink = () => import(/* webpackChunkName: "leaders-company-website-link" */ './company-website-link.vue');
const LeadersCompanySocialLink = () => import(/* webpackChunkName: "leaders-company-social-link" */ './company-social-link.vue');
const LeadersGTMTracker = () => import(/* webpackChunkName: "leaders-gtm-tracker" */ './gtm-tracker.vue');
Expand All @@ -17,4 +18,7 @@ export default (Browser, { withGTM = true } = {}) => {
Browser.register('LeadersCompanySocialLink', LeadersCompanySocialLink, {
on: { action: (...args) => EventBus.$emit('leaders-action', ...args) },
});
Browser.register('LeadersClickEmitter', LeadersClickEmitter, {
on: { action: (...args) => EventBus.$emit('leaders-action', ...args) },
});
};
1 change: 1 addition & 0 deletions packages/marko-web-leaders/components/click-emitter.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<marko-web-browser-component name="LeadersClickEmitter" />
3 changes: 3 additions & 0 deletions packages/marko-web-leaders/components/marko.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"<marko-web-leaders>": {
"template": "./index.marko"
},
"<marko-web-leaders-click-emitter>": {
"template": "./click-emitter.marko"
},
"<marko-web-leaders-company-social-link>": {
"template": "./company-social-link.marko",
"@company": "object",
Expand Down
1 change: 1 addition & 0 deletions packages/marko-web-leaders/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@parameter1/base-cms-leaders-program": "^2.5.0",
"@parameter1/base-cms-marko-web-icons": "^2.0.0",
"@parameter1/base-cms-object-path": "^2.5.0",
"dom-utils": "^0.9.0",
"graphql": "^14.7.0",
"graphql-tag": "^2.11.0",
"object-path": "^0.11.5"
Expand Down
8 changes: 8 additions & 0 deletions packages/marko-web-leaders/utils/event-attrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = (params = {}) => {
const keys = ['action', 'category', 'label', 'payload'];
return keys.reduce((o, key) => {
const value = key === 'payload' ? JSON.stringify(params[key]) : params[key];
if (!value) return o;
return { ...o, [`data-leaders-${key}`]: value };
}, {});
};
16 changes: 16 additions & 0 deletions packages/marko-web-leaders/utils/event-link-attrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const leadersAttrs = require('./event-attrs');

module.exports = ({
category,
label,
companyId,
linkAttrs,
} = {}) => ({
...linkAttrs,
...leadersAttrs({
action: 'click',
category,
label,
payload: { companyId },
}),
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6733,6 +6733,11 @@ dom-serializer@^1.0.1, dom-serializer@~1.2.0:
domhandler "^4.0.0"
entities "^2.0.0"

dom-utils@^0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/dom-utils/-/dom-utils-0.9.0.tgz#e615a5af15ac4505e55ef612c72b5b5d176121f3"
integrity sha1-5hWlrxWsRQXlXvYSxytbXRdhIfM=

domain-browser@^1.1.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/domain-browser/-/domain-browser-1.2.0.tgz#3d31f50191a6749dd1375a7f522e823d42e54eda"
Expand Down

0 comments on commit 9311686

Please sign in to comment.