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 #62 from solocommand/native-x-container-id
Browse files Browse the repository at this point in the history
NativeX story GTM/event tracking
  • Loading branch information
solocommand authored Apr 5, 2021
2 parents 1918a04 + d10b926 commit 715b33f
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 0 deletions.
110 changes: 110 additions & 0 deletions packages/marko-web-native-x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,113 @@ module.exports = gql`
`;

```
#### Event Tracking and analytics

To utilize NativeX's story analytics, you must use the event tracking [utility functions](utils/gtm-events) to send events to the NativeX GTM container.

The GTM container can be initialized by including the `<marko-web-native-x-gtm-init>` component within your `document`'s `<head>`:
```marko
<${document}>
<@head>
<marko-web-native-x-gtm-init />
</@head>
</>
```
Before sending any events, make sure to include the `<marko-web-native-x-story-track-init>` component. This component initializes the data layer with the story data that other events rely upon.
```marko
<${document}>
<@head>
<marko-web-native-x-gtm-init />
</@head>
<@page>
<marko-web-native-x-story-track-init story=story />
</@page>
</>
```

See examples of available events below. By default, including the `<marko-web-native-x-story-track-page-view>` component will populate the datalayer with the story details. To disable this behavior, send the `push=false` parameter when including the component. All other story components assume this has already happened -- to force population of the data layer, send the `push=true` parameter when including the relevant component.

##### Page View
To send a page view, include the `<marko-web-native-x-story-track-page-view>` and pass the `story` property through (from the Nx middleware):
```marko
$ const { story } = input;
<${document}>
<@head>
<marko-web-native-x-gtm-init />
</@head>
<@page>
<marko-web-native-x-story-track-page-view />
<h1>${story.title}</h1>
</@page>
</>
```

##### Outbound Links
To automatically track outbound links, include the `<marko-web-native-x-story-track-outbound-links>` component. This component requires a DOM selector in order to limit the tracking to a subset of links:
```marko
$ const { story } = input;
<${document}>
<@head>
<marko-web-native-x-gtm-init />
</@head>
<@page>
<h1>${story.title}</h1>
<div id="my-story-body">
<a href="https://google.com">This will be tracked when clicked!</a>
</div>
<marko-web-native-x-story-track-outbound-links container="#my-story-body" />
</@page>
</>
```

##### Social Sharing
If using the [marko-web-social-sharing](../marko-web-social-sharing) package, you can automatically track any share attempts by including the `<marko-web-native-x-story-track-social-share>` component in your story template:
```marko
$ const { story } = input;
<${document}>
<@head>
<marko-web-native-x-gtm-init />
</@head>
<@page>
<marko-web-native-x-story-track-social-share />
<h1>${story.title}</h1>
<marko-web-social-sharing path=story.url providers=["facebook", "email"] />
</@page>
</>
```

##### End of content
To automatically track when the user reaches the bottom of the page, include the `<marko-web-native-x-story-track-end-of-content>` component below your story. This event will fire when the element comes into view:
```marko
$ const { story } = input;
<${document}>
<@head>
<marko-web-native-x-gtm-init />
</@head>
<@page>
<h1>${story.title}</h1>
<p>...</p>
<p>...</p>
<p>...</p>
<marko-web-native-x-story-track-end-of-content />
</@page>
</>
```

##### Manual/advanced implementations
You can also send an event directly by using one of the available utility functions:
```vue
<script>
import { endOfContent } from "@parameter1/base-cms-marko-web-native-x/utils/gtm-events";
export default {
created() {
endOfContent();
}
};
</script>
```
22 changes: 22 additions & 0 deletions packages/marko-web-native-x/browser/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
extends: [
'airbnb-base',
'plugin:vue/recommended',
],
env: {
browser: true,
},
rules: {
'vue/max-attributes-per-line': ['error', {
singleline: 3,
multiline: {
max: 1,
allowFirstLine: false,
},
}],
'vue/no-v-html': 'off',
},
parserOptions: {
parser: 'babel-eslint',
},
};
10 changes: 10 additions & 0 deletions packages/marko-web-native-x/browser/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const NativeXTrackEndOfContent = () => import(/* webpackChunkName: "native-x-track-end-of-content" */ './track-end-of-content.vue');
const NativeXTrackSocialShare = () => import(/* webpackChunkName: "native-x-track-social-share" */ './track-social-share.vue');
const NativeXTrackOutboundLinks = () => import(/* webpackChunkName: "native-x-track-outbound-links" */ './track-outbound-links.vue');

export default (Browser) => {
const { EventBus } = Browser;
Browser.register('NativeXTrackEndOfContent', NativeXTrackEndOfContent);
Browser.register('NativeXTrackSocialShare', NativeXTrackSocialShare, { provide: { EventBus } });
Browser.register('NativeXTrackOutboundLinks', NativeXTrackOutboundLinks);
};
20 changes: 20 additions & 0 deletions packages/marko-web-native-x/browser/track-end-of-content.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<template>
<div id="native-x-end-of-content" />
</template>

<script>
import { endOfContent } from '../utils/gtm-events';
export default {
created() {
this.observer = new IntersectionObserver((event) => {
if (event[0].isIntersecting) {
endOfContent();
}
});
},
mounted() {
this.observer.observe(this.$el);
},
};
</script>
26 changes: 26 additions & 0 deletions packages/marko-web-native-x/browser/track-outbound-links.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<template>
<div />
</template>

<script>
import { outboundLink } from '../utils/gtm-events';
export default {
props: {
container: {
type: String,
required: true,
},
},
created() {
const elements = document.querySelectorAll(`${this.container} a[href]`);
elements.forEach((element) => {
const url = element.getAttribute('href');
element.addEventListener('click', (e) => {
e.preventDefault();
outboundLink(url);
});
});
},
};
</script>
24 changes: 24 additions & 0 deletions packages/marko-web-native-x/browser/track-social-share.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<div />
</template>

<script>
import { get } from '@parameter1/base-cms-object-path';
import { share } from '../utils/gtm-events';
export default {
inject: ['EventBus'],
props: {
eventName: {
type: String,
default: 'social-share-open',
},
},
created() {
this.EventBus.$on(this.eventName, (event) => {
const provider = get(event, 'provider.name');
share(provider);
});
},
};
</script>
18 changes: 18 additions & 0 deletions packages/marko-web-native-x/components/gtm-init.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import defaultValue from "@parameter1/base-cms-marko-core/utils/default-value";
import { get, getAsObject } from "@parameter1/base-cms-object-path";

$ const { req } = out.global;
$ const story = getAsObject(input, "story");
$ const on = defaultValue(input.on, "load");
$ const requestFrame = defaultValue(input.requestFrame, true);
$ const targetTag = defaultValue(input.targetTag, "body");

<marko-web-gtm-init
name="dataLayerNativeX"
container-id="GTM-PVHD8N5"
on=on
request-frame=requestFrame
target-tag=targetTag
start=true
push=input.push
/>
24 changes: 24 additions & 0 deletions packages/marko-web-native-x/components/marko.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,29 @@
"@uri": "string",
"@id": "string",
"@opts": "object"
},
"<marko-web-native-x-gtm-init>": {
"template": "./gtm-init.marko",
"@on": "string",
"@target-tag": "string",
"@request-frame": "boolean",
"@story": "object"
},
"<marko-web-native-x-story-track-init>": {
"template": "./story/track-init.marko",
"@story": "object"
},
"<marko-web-native-x-story-track-page-view>": {
"template": "./story/track-page-view.marko"
},
"<marko-web-native-x-story-track-end-of-content>": {
"template": "./story/track-end-of-content.marko"
},
"<marko-web-native-x-story-track-social-share>": {
"template": "./story/track-social-share.marko"
},
"<marko-web-native-x-story-track-outbound-links>": {
"template": "./story/track-outbound-links.marko",
"@container": "string"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<marko-web-browser-component name="NativeXTrackEndOfContent" props={} />
13 changes: 13 additions & 0 deletions packages/marko-web-native-x/components/story/track-init.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { get, getAsObject } from "@parameter1/base-cms-object-path";

$ const { req } = out.global;
$ const story = getAsObject(input, "story");

<marko-web-gtm-push name="dataLayerNativeX" data={
story_id: story.id,
page_path: req.path,
page_title: story.title,
publisher_id: get(story, "publisher.id"),
advertiser_id: get(story, "advertiser.id"),
preview_mode: Boolean(req.query.preview),
} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<marko-web-browser-component name="NativeXTrackOutboundLinks" props={
container: input.container,
} />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<marko-web-gtm-push name="dataLayerNativeX" data={ event: "page_load" } />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<marko-web-browser-component name="NativeXTrackSocialShare" props={} />
1 change: 1 addition & 0 deletions packages/marko-web-native-x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"@parameter1/base-cms-marko-web-deferred-script-loader": "^2.13.0",
"@parameter1/base-cms-marko-web-gtm": "^2.13.1",
"@parameter1/base-cms-object-path": "^2.5.0",
"@parameter1/base-cms-utils": "^2.4.2",
"@parameter1/base-cms-web-common": "^2.5.0",
Expand Down
20 changes: 20 additions & 0 deletions packages/marko-web-native-x/utils/gtm-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable no-undef */
export const pageLoad = () => {
const dataLayer = window.dataLayerNativeX;
if (dataLayer) dataLayer.push({ event: 'page_load' });
};

export const outboundLink = (url) => {
const dataLayer = window.dataLayerNativeX;
if (dataLayer) dataLayer.push({ event: 'outbound_click', outbound_url: url, eventTimeout: 3000 });
};

export const share = (provider) => {
const dataLayer = window.dataLayerNativeX;
if (dataLayer) dataLayer.push({ event: 'share', social_provider: provider });
};

export const endOfContent = () => {
const dataLayer = window.dataLayerNativeX;
if (dataLayer) dataLayer.push({ event: 'scroll_to_bottom' });
};

0 comments on commit 715b33f

Please sign in to comment.