Skip to content

Commit 50f321b

Browse files
[WNMGDS-3344] Add Utag data object (#3559)
* Add ssr helper * Make HTML dangerous * Seems like it works * Use ref for variable storage and remove unneeded function
1 parent 3a489da commit 50f321b

File tree

3 files changed

+64
-41
lines changed

3 files changed

+64
-41
lines changed

packages/docs/gatsby-ssr.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const proc = require('process');
2+
3+
const HeadComponentsJs = (content) => (
4+
<script type="text/javascript" dangerouslySetInnerHTML={content}></script>
5+
);
6+
7+
exports.onRenderBody = ({ setPreBodyComponents }) => {
8+
const env = proc.env.NODE_ENV == 'production' ? 'prod' : 'dev';
9+
10+
const dangerousTealiumSnippet = {
11+
__html: `
12+
(function(a,b,c,d){
13+
a='https://tealium-tags.cms.gov/cms-design/${env}/utag.js';
14+
b=document;c='script';d=b.createElement(c);d.src=a;d.type='text/javascript';d.async=true;
15+
a=b.getElementsByTagName(c)[0];a.parentNode.insertBefore(d,a);
16+
})();`,
17+
};
18+
19+
setPreBodyComponents(HeadComponentsJs(dangerousTealiumSnippet));
20+
};

packages/docs/src/components/layout/Layout.tsx

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type * as React from 'react';
2+
import { useEffect, useRef } from 'react';
23
import Footer from './DocSiteFooter';
34
import SideNav from './SideNav/SideNav';
45
import PageHeader from './PageHeader';
@@ -13,6 +14,8 @@ import {
1314
TableOfContentsItem,
1415
} from '../../helpers/graphQLTypes';
1516
import { withPrefix } from 'gatsby';
17+
import { UtagContainer } from '@cmsgov/design-system';
18+
import { sendViewEvent } from '@cmsgov/design-system';
1619

1720
import '../../styles/index.scss';
1821
import { getThemeData } from './SideNav/themeVersionData';
@@ -66,12 +69,45 @@ const Layout = ({
6669
theme,
6770
tableOfContentsData,
6871
}: LayoutProps) => {
69-
const env = 'prod';
72+
// Using a ref here because this value shouldn't change after the initial render.
73+
const env = useRef('' as 'dev' | 'github-demo' | 'prod');
7074
const baseTitle = theme === 'core' ? 'CMS Design System' : getThemeData(theme).longName;
7175
const tabTitle = frontmatter?.title ? `${frontmatter.title} - ${baseTitle}` : baseTitle;
7276

7377
const pageId = slug ? `page--${slug.replace('/', '_')}` : null;
7478

79+
useEffect(() => {
80+
if (window && (window as UtagContainer)?.utag) {
81+
// We can define environment names as we wish
82+
// github-demo is a demo deployment off of a specific branch.
83+
switch (location.hostname) {
84+
case 'localhost':
85+
env.current = 'dev';
86+
break;
87+
case 'cmsgov.github.io':
88+
env.current = 'github-demo';
89+
break;
90+
case 'design.cms.gov':
91+
env.current = 'prod';
92+
break;
93+
default:
94+
env.current = 'prod';
95+
}
96+
97+
const analyticsPayload = {
98+
content_language: 'en',
99+
content_type: 'html',
100+
logged_in: 'false',
101+
page_name: tabTitle,
102+
page_type: tabTitle.includes('Page not found') ? 'true' : 'false', //If page is a 404 (error page) this is set to true, otherwise it is false
103+
site_environment: env.current, //Used to include or exclude traffic from different testing environments. Ex: test, test0, imp, production
104+
site_section: location.pathname == '/' ? 'index' : location.pathname, // Set the section to the pathname, except in the case of the index.
105+
} as any;
106+
107+
sendViewEvent(analyticsPayload);
108+
}
109+
}, []);
110+
75111
return (
76112
<div data-theme={theme} id={pageId}>
77113
<Helmet
@@ -99,8 +135,13 @@ const Layout = ({
99135
: 'The CMS Design System is a set of open source design and front-end development resources for creating Section 508 compliant, responsive, and consistent websites.'
100136
}
101137
/>
102-
<script>{`window.tealiumEnvironment = "${env}";`}</script>
103-
<script src="//tags.tiqcdn.com/utag/cmsgov/cms-design/prod/utag.sync.js"></script>
138+
<script>{`window.tealiumEnvironment = "${env.current}";`}</script>
139+
<script
140+
src={`https://tealium-tags.cms.gov/cms-design/${env.current}/utag.sync.js`}
141+
></script>
142+
<script type="text/javascript">
143+
{'window.utag_cfg_ovrd = window.utag_cfg_ovrd || {}; window.utag_cfg_ovrd.noview = true;'}
144+
</script>
104145
<link
105146
rel="stylesheet"
106147
type="text/css"

packages/docs/src/pages/404.tsx

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,10 @@
11
import { graphql } from 'gatsby';
2-
32
import Layout from '../components/layout/Layout';
43
import { MdxQuery } from '../helpers/graphQLTypes';
54
import useTheme from '../helpers/useTheme';
65
import ContentRenderer from '../components/content/ContentRenderer';
7-
import { useEffect } from 'react';
8-
import { UtagContainer } from '@cmsgov/design-system';
9-
import { sendViewEvent } from '@cmsgov/design-system';
106

117
const NotFoundPage = ({ data, location }: MdxQuery) => {
12-
useEffect(() => {
13-
if (window && (window as UtagContainer)?.utag) {
14-
let env = 'prod';
15-
16-
// We can define environment names as we wish
17-
// github-demo is a demo deployment off of a specific branch.
18-
switch (location.hostname) {
19-
case 'localhost':
20-
env = 'dev';
21-
break;
22-
case 'cmsgov.github.io':
23-
env = 'github-demo';
24-
break;
25-
case 'design.cms.gov':
26-
env = 'prod';
27-
break;
28-
default:
29-
env = 'prod';
30-
}
31-
32-
const analyticsPayload = {
33-
content_language: 'en',
34-
content_type: 'html',
35-
logged_in: 'false',
36-
page_name: 'Page not found - CMS Design System',
37-
page_type: 'true', //If page is a 404 (error page) this is set to true, otherwise it is false
38-
site_environment: env, //Used to include or exclude traffic from different testing environments. Ex: test, test0, imp, production
39-
site_section: 'Page not found - CMS Design System',
40-
} as any;
41-
42-
sendViewEvent(analyticsPayload);
43-
}
44-
}, []);
45-
468
const theme = useTheme();
479
return (
4810
<Layout frontmatter={data.mdx.frontmatter} location={location} theme={theme}>

0 commit comments

Comments
 (0)