Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new component for google analytics 4 #793

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/idyll-components/src/ga4-analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';

class Ga4Analytics extends React.PureComponent {
componentDidMount() {
try {
const url =
'https://www.googletagmanager.com/gtag/js?id=' + this.props.google;

(function(w, d, o, u) {
const ga4ScriptNode = d.createElement('script');
const firstONode = d.getElementsByTagName(o)[0];
ga4ScriptNode.async = 1;
ga4ScriptNode.src = u;
firstONode.parentNode.insertBefore(ga4ScriptNode, firstONode);
})(window, document, 'link', url);

window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());

gtag('config', this.props.google);
} catch (error) {
console.log('Could not mount google analytics 4: ' + error);
}
}

render() {
return null;
}
}

Ga4Analytics._idyll = {
name: 'Ga4Analytics',
tagType: 'closed',
props: [
{
name: 'google',
type: 'string',
example: '"G-XXXXXXX"',
defaultValue: 'none',
description: 'Component to add google analytics 4.'
}
]
};

export default Ga4Analytics;
1 change: 1 addition & 0 deletions packages/idyll-components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ export { default as TextContainer } from './text-container';
export { default as TextInput } from './text-input';
export { default as Tweet } from './tweet';
export { default as Youtube } from './youtube';
export { default as Ga4Analytics } from './ga4-analytics';
9 changes: 8 additions & 1 deletion packages/idyll-components/test/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const {
H6,
Desmos,
Cite,
References
References,
Ga4Analytics
} = require('../dist/cjs/index.js');

describe('Sanity Check', () => {
Expand Down Expand Up @@ -181,6 +182,9 @@ describe('Sanity Check', () => {
it('<Preload />', () => {
expect(() => shallow(<Preload />)).not.toThrow();
});
it('<Ga4Analytics />', () => {
expect(() => shallow(<Ga4Analytics />)).not.toThrow();
});
});
});

Expand Down Expand Up @@ -323,6 +327,9 @@ describe('Sanity Check', () => {
it('<Preload />', () => {
expect(() => mount(<Preload />)).not.toThrow();
});
it('<Ga4Analytics />', () => {
expect(() => mount(<Ga4Analytics />)).not.toThrow();
});
});
});
});
9 changes: 9 additions & 0 deletions packages/idyll-docs/idyll-components/contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,15 @@ const components = [
liveExample: false
}
},
{
Ga4Analytics: {
thumbnail: 'analytics.png',
component: COMPONENTS.Ga4Analytics,
description:
'This component uses Google Analytics 4 code on your page. Universal Analytics properties will stop collecting data starting July 1, 2023. Google recommends to use Google Analytics 4 property instead.',
liveExample: false
}
},
{
Meta: {
thumbnail: 'meta.png',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Ga4Analytics google:"G-XXXXXXXXX" /]
Loading