Skip to content

Commit

Permalink
feat(GoogleAnalytics): Added gtag events for button clicks
Browse files Browse the repository at this point in the history
  • Loading branch information
timothystewart6 committed Oct 9, 2021
1 parent bece421 commit 962742c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/analytics/google.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function trackEvent(action, category, label, value) {
window.gtag('event', action, {
event_category: category,
event_label: label,
value: value,
});
}
10 changes: 10 additions & 0 deletions src/components/Button/Button.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import React, { memo } from 'react';
import { string } from 'prop-types';
import { trackEvent } from '../../analytics/google';
import { runtimeConfig } from '../../config';

function Button(props) {
const { name, href, displayName, logo } = props;

const handleClick = () => {
if (runtimeConfig?.GA_TRACKING_ID) {
trackEvent('click', 'social', name, 1);
}
};

return (
<>
<a
className={`button button-${name}`}
href={href}
target="_blank"
rel="noopener noreferrer"
onClick={handleClick}
>
{logo && (
<img className="icon" src={logo} alt={`${displayName} logo`} />
Expand All @@ -29,4 +38,5 @@ Button.propType = {
srcSet: string,
alt: string.isRequired,
href: string,
name: string,
};

0 comments on commit 962742c

Please sign in to comment.