You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.jsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React, { PureComponent } from 'react';
  2. import ReactGA from '../../src';
  3. import Router from './Router';
  4. function asDisplayString(value) {
  5. if (typeof value === 'string') return `'${value}'`;
  6. return value.toString();
  7. }
  8. function renderConfigString(config, indent = '') {
  9. return [
  10. '{',
  11. ...Object.keys(config)
  12. .map((key) => {
  13. if (config[key] === undefined || config[key] === null) return '';
  14. return `\t${key}: ${asDisplayString(config[key])}`;
  15. })
  16. .filter((value) => value),
  17. '}'
  18. ].reduce((result, element, index, array) => {
  19. return `${result}${indent}${element}${
  20. index < array.length - 1 ? '\n' : ''
  21. }`;
  22. }, '');
  23. }
  24. const DEFAULT_CONFIG = {
  25. trackingId: '',
  26. debug: true,
  27. gaOptions: {
  28. cookieDomain: 'none'
  29. }
  30. };
  31. export default class App extends PureComponent {
  32. constructor(props, context) {
  33. super(props, context);
  34. this.state = {
  35. reactGaInitialised: false,
  36. configs: [DEFAULT_CONFIG]
  37. };
  38. }
  39. filteredConfigs = () => {
  40. const { configs } = this.state;
  41. return configs.filter(({ trackingId: id }) => id);
  42. };
  43. initReactGA = (event) => {
  44. event.preventDefault();
  45. if (this.filteredConfigs().length === 0) {
  46. return;
  47. }
  48. const { configs } = this.state;
  49. ReactGA.initialize(configs);
  50. // Send initial test view
  51. ReactGA.pageview('test-init-pageview');
  52. this.setState({ reactGaInitialised: true });
  53. };
  54. addConfig = () => {
  55. const { configs } = this.state;
  56. this.setState({
  57. configs: [configs, DEFAULT_CONFIG]
  58. });
  59. };
  60. updateConfig = (configIndex, key, type, event) => {
  61. const { configs } = this.state;
  62. const config = configs[configIndex];
  63. let value;
  64. if (type === 'checkbox') {
  65. value = !config[key];
  66. } else {
  67. value = event.target.value;
  68. }
  69. const newConfig = {
  70. ...config,
  71. [key]: value
  72. };
  73. this.setState({
  74. configs: [
  75. ...configs.slice(0, configIndex),
  76. newConfig,
  77. ...configs.slice(configIndex + 1)
  78. ]
  79. });
  80. };
  81. renderConfigs = () => {
  82. const configs = this.filteredConfigs();
  83. if (configs.length === 0) return '';
  84. if (configs.length === 1) {
  85. const { trackingId, ...options } = configs[0];
  86. return `'${trackingId}'${
  87. Object.values(options).filter((val) => val).length
  88. ? `, ${JSON.stringify(options)}`
  89. : ''
  90. }`;
  91. }
  92. return `[\n${configs.reduce((result, config, index, array) => {
  93. const configString = renderConfigString(config, '\t');
  94. return `${result}${result ? '\n' : ''}${configString}${
  95. index < array.length - 1 ? ',' : ''
  96. }`;
  97. }, '')}\n]`;
  98. };
  99. render() {
  100. const { configs, reactGaInitialised } = this.state;
  101. if (reactGaInitialised) {
  102. return (
  103. <div>
  104. <h4>App is Initialised. Refresh page to reset.</h4>
  105. <Router />
  106. </div>
  107. );
  108. }
  109. let initializationDebug = (
  110. <pre>
  111. ReactGA.initialize(
  112. {this.renderConfigs()}
  113. );
  114. </pre>
  115. );
  116. if (this.filteredConfigs().length === 0) {
  117. initializationDebug = <p>No Valid Configs set.</p>;
  118. }
  119. return (
  120. <form onSubmit={this.initReactGA}>
  121. <p>Input your configs below:</p>
  122. {configs.map(({ trackingId, debug }, index) => (
  123. <div key={index}>
  124. <div>
  125. TrackingID:&nbsp;
  126. <input
  127. value={trackingId}
  128. onChange={this.updateConfig.bind(
  129. this,
  130. index,
  131. 'trackingId',
  132. 'text'
  133. )}
  134. />
  135. &nbsp;Debug&nbsp;
  136. <input
  137. type="checkbox"
  138. checked={debug || false}
  139. onChange={this.updateConfig.bind(
  140. this,
  141. index,
  142. 'debug',
  143. 'checkbox'
  144. )}
  145. />
  146. </div>
  147. </div>
  148. ))}
  149. <button type="button" onClick={this.addConfig}>
  150. Add Config
  151. </button>
  152. <button type="submit" disabled={configs.length < 1}>
  153. {configs.length < 1
  154. ? 'Add Configs first'
  155. : 'Run the initialize function as below'}
  156. </button>
  157. {initializationDebug}
  158. </form>
  159. );
  160. }
  161. }