Skip to content

Commit

Permalink
Add docType and docId to dataLayer for dataLoaded event
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Feb 28, 2023
1 parent f9e6a16 commit 32eb241
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,18 @@ Storybook will be available under /storybook/index.html after build.

This project supports Google Tag Manager. You can prepare the following setup in `.env` file:
- `PUBLIC_GTM_ID`: Google Tag Manager Container ID (`GTM-XXXXXXX`)
- `PUBLIC_GA_TRACKING_ID`: Google analytics trakcing ID (`UA-XXXXXXXX-X`). `rumors-site` will *not*
load Google analytics itself; instead, it will push `GA_TRACKING_ID` to `dataLayer`, and it is your
responsibility to pick it up as a Data Layer Variable and setup Google Analytics in Google Tag Manager.

The application will fire the following custom events:
The application will fire the following custom events in GTM `dataLayer`:

- `routeChangeStart` - when next-router starts route change
- `routeChangeComplete` - when next-router finish route change
- `dataLoaded` - when article / reply is loaded in article & reply page

Also, it will push the following custom variable to `dataLayer`;

- `GA_TRACKING_ID` - see `PUBLIC_GA_TRACKING_ID`
- `CURRENT_USER` - Current user object, set by `useCurrentUser`.
- `docType` - Set when `dataLoaded` event fires. Can be either `article` or `reply`.
- `docId` - Set when `dataLoaded` event fires. The ID of loaded content.

Lastly, in Google Tag Manager we use `data-ga` property to track clicks.
If user clicks a decendant of an React element with `data-ga` property,
Expand Down
6 changes: 5 additions & 1 deletion pages/article/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@ function ArticlePage() {

const article = data?.GetArticle;

usePushToDataLayer(!!article, { event: 'dataLoaded' });
usePushToDataLayer(!!article, {
event: 'dataLoaded',
docType: 'article',
docId: query.id,
});

if (loading && !article) {
return (
Expand Down
6 changes: 5 additions & 1 deletion pages/reply/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ function ReplyPage() {
}, [currentUser]);

const reply = data?.GetReply;
usePushToDataLayer(!!reply, { event: 'dataLoaded' });
usePushToDataLayer(!!reply, {
event: 'dataLoaded',
docType: 'article',
docId: query.id,
});

if (loading) {
return (
Expand Down

0 comments on commit 32eb241

Please sign in to comment.