| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- import React, { PureComponent } from 'react';
- import ReactGA from '../../src';
-
- import Router from './Router';
-
- function asDisplayString(value) {
- if (typeof value === 'string') return `'${value}'`;
- return value.toString();
- }
-
- function renderConfigString(config, indent = '') {
- return [
- '{',
- ...Object.keys(config)
- .map((key) => {
- if (config[key] === undefined || config[key] === null) return '';
- return `\t${key}: ${asDisplayString(config[key])}`;
- })
- .filter((value) => value),
- '}'
- ].reduce((result, element, index, array) => {
- return `${result}${indent}${element}${
- index < array.length - 1 ? '\n' : ''
- }`;
- }, '');
- }
-
- const DEFAULT_CONFIG = {
- trackingId: '',
- debug: true,
- gaOptions: {
- cookieDomain: 'none'
- }
- };
-
- export default class App extends PureComponent {
- constructor(props, context) {
- super(props, context);
-
- this.state = {
- reactGaInitialised: false,
- configs: [DEFAULT_CONFIG]
- };
- }
-
- filteredConfigs = () => {
- const { configs } = this.state;
- return configs.filter(({ trackingId: id }) => id);
- };
-
- initReactGA = (event) => {
- event.preventDefault();
- if (this.filteredConfigs().length === 0) {
- return;
- }
- const { configs } = this.state;
- ReactGA.initialize(configs);
- // Send initial test view
- ReactGA.pageview('test-init-pageview');
- this.setState({ reactGaInitialised: true });
- };
-
- addConfig = () => {
- const { configs } = this.state;
- this.setState({
- configs: [configs, DEFAULT_CONFIG]
- });
- };
-
- updateConfig = (configIndex, key, type, event) => {
- const { configs } = this.state;
- const config = configs[configIndex];
- let value;
- if (type === 'checkbox') {
- value = !config[key];
- } else {
- value = event.target.value;
- }
- const newConfig = {
- ...config,
- [key]: value
- };
- this.setState({
- configs: [
- ...configs.slice(0, configIndex),
- newConfig,
- ...configs.slice(configIndex + 1)
- ]
- });
- };
-
- renderConfigs = () => {
- const configs = this.filteredConfigs();
- if (configs.length === 0) return '';
- if (configs.length === 1) {
- const { trackingId, ...options } = configs[0];
- return `'${trackingId}'${
- Object.values(options).filter((val) => val).length
- ? `, ${JSON.stringify(options)}`
- : ''
- }`;
- }
- return `[\n${configs.reduce((result, config, index, array) => {
- const configString = renderConfigString(config, '\t');
- return `${result}${result ? '\n' : ''}${configString}${
- index < array.length - 1 ? ',' : ''
- }`;
- }, '')}\n]`;
- };
-
- render() {
- const { configs, reactGaInitialised } = this.state;
- if (reactGaInitialised) {
- return (
- <div>
- <h4>App is Initialised. Refresh page to reset.</h4>
- <Router />
- </div>
- );
- }
- let initializationDebug = (
- <pre>
- ReactGA.initialize(
- {this.renderConfigs()}
- );
- </pre>
- );
- if (this.filteredConfigs().length === 0) {
- initializationDebug = <p>No Valid Configs set.</p>;
- }
- return (
- <form onSubmit={this.initReactGA}>
- <p>Input your configs below:</p>
- {configs.map(({ trackingId, debug }, index) => (
- <div key={index}>
- <div>
- TrackingID:
- <input
- value={trackingId}
- onChange={this.updateConfig.bind(
- this,
- index,
- 'trackingId',
- 'text'
- )}
- />
- Debug
- <input
- type="checkbox"
- checked={debug || false}
- onChange={this.updateConfig.bind(
- this,
- index,
- 'debug',
- 'checkbox'
- )}
- />
- </div>
- </div>
- ))}
- <button type="button" onClick={this.addConfig}>
- Add Config
- </button>
- <button type="submit" disabled={configs.length < 1}>
- {configs.length < 1
- ? 'Add Configs first'
- : 'Run the initialize function as below'}
- </button>
- {initializationDebug}
- </form>
- );
- }
- }
|