Skip to content

Instantly share code, notes, and snippets.

@karatechops
Created September 26, 2019 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karatechops/e5eadbffa332d6e02c0923438c42e38d to your computer and use it in GitHub Desktop.
Save karatechops/e5eadbffa332d6e02c0923438c42e38d to your computer and use it in GitHub Desktop.
Example of using Track component
import React from 'react';
import PropTypes from 'prop-types';
import { Track } from 'containers';
import { Link } from 'react-router';
export const Anchor = ({ active, url, children, track, category: customCategory, // eslint-disable-line
label: customLabel, action: customAction, ...rest }) => {
let linkNode = <div />;
let category = 'Navigation';
let label = '';
const action = 'Anchor Click';
if (url && url.indexOf('http') > -1) {
label = url;
linkNode = (
<a
href={url}
target="_blank"
rel="noopener noreferrer"
{...rest}
>
{children}
</a>);
} else if (url && url.indexOf('mailto:') > -1) {
category = 'Send Email';
label = url.replace('mailto:', '');
linkNode = (
<a
href={url}
{...rest}
>
{children}
</a>);
} else {
label = url;
linkNode = (
<Link
to={url}
{...rest}
>
{children}
</Link>
);
}
return (
<Track
category={customCategory || category}
action={customAction || action}
label={customLabel || label}
track={track}
>
{linkNode}
</Track>
);
};
Anchor.propTypes = {
active: PropTypes.bool,
url: PropTypes.string.isRequired,
children: PropTypes.node,
category: PropTypes.string,
className: PropTypes.string,
action: PropTypes.string,
label: PropTypes.string,
track: PropTypes.bool,
};
export default Anchor;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment