|
| 1 | +import * as React from "react" |
| 2 | +import {StoreContext} from "./store-context" |
| 3 | +import {emptyEvents, eventDescription, eventLine, eventName, eventSnippet, eventTimestamp, gaConsoleStyle} from "@/components/ga4/EnhancedEcommerce/ga-console.module.css"; |
| 4 | +import Dialog from '@material-ui/core/Dialog'; |
| 5 | +import Tabs from '@material-ui/core/Tabs'; |
| 6 | +import Tab from '@material-ui/core/Tab'; |
| 7 | +import DialogActions from '@material-ui/core/DialogActions'; |
| 8 | +import DialogContent from '@material-ui/core/DialogContent'; |
| 9 | +import DialogContentText from '@material-ui/core/DialogContentText'; |
| 10 | +import DialogTitle from '@material-ui/core/DialogTitle'; |
| 11 | +import Button from '@material-ui/core/Button'; |
| 12 | +import TextField from '@material-ui/core/TextField'; |
| 13 | +import {Link} from "gatsby"; |
| 14 | +import {Box, Typography} from "@material-ui/core"; |
| 15 | + |
| 16 | +function TabPanel(props) { |
| 17 | + const {children, value, index, ...other} = props; |
| 18 | + |
| 19 | + return ( |
| 20 | + <div |
| 21 | + role="tabpanel" |
| 22 | + hidden={value !== index} |
| 23 | + id={`simple-tabpanel-${index}`} |
| 24 | + {...other} |
| 25 | + > |
| 26 | + {value === index && ( |
| 27 | + <Box p={3}> |
| 28 | + <Typography>{children}</Typography> |
| 29 | + </Box> |
| 30 | + )} |
| 31 | + </div> |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +export function GaConsole({className}) { |
| 36 | + const {events} = React.useContext(StoreContext) |
| 37 | + const [open, setOpen] = React.useState(false); |
| 38 | + |
| 39 | + const [selectedEvent, setSelectedEvent] = React.useState({ |
| 40 | + key: 0, |
| 41 | + timestamp: '', |
| 42 | + name: '', |
| 43 | + description: '', |
| 44 | + snippet: '' |
| 45 | + }); |
| 46 | + |
| 47 | + const [value, setValue] = React.useState(0); |
| 48 | + |
| 49 | + const handleClickOpen = (eventKey) => () => { |
| 50 | + setSelectedEvent(events[events.length - eventKey - 1]) |
| 51 | + setOpen(true); |
| 52 | + }; |
| 53 | + const handleClose = () => { |
| 54 | + setOpen(false); |
| 55 | + }; |
| 56 | + const handleChange = (event, newValue) => { |
| 57 | + setValue(newValue); |
| 58 | + }; |
| 59 | + |
| 60 | + return ( |
| 61 | + <div className={[gaConsoleStyle, className].join(" ")}> |
| 62 | + {events.length ? events.map((event) => ( |
| 63 | + <div key={event.key} className={eventLine}> |
| 64 | + <div className={eventTimestamp}>{event.timestamp}</div> |
| 65 | + <div className={eventName}> |
| 66 | + <Link |
| 67 | + to={`https://developers.google.com/gtagjs/reference/ga4-events#${event.name}`} |
| 68 | + aria-label={`GA4 event reference documentation`} |
| 69 | + target='_blank' |
| 70 | + > |
| 71 | + {event.name} |
| 72 | + </Link></div> |
| 73 | + <div |
| 74 | + className={eventDescription}>{event.description}</div> |
| 75 | + <div onClick={handleClickOpen(event.key)} |
| 76 | + className={eventSnippet}>{event.snippet}</div> |
| 77 | + </div> |
| 78 | + )) : <div className={emptyEvents}>Start interacting with the store |
| 79 | + to see Google Analytics eCommerce events here.</div>} |
| 80 | + |
| 81 | + <Dialog |
| 82 | + open={open} |
| 83 | + onClose={handleClose} |
| 84 | + aria-labelledby="scroll-dialog-title" |
| 85 | + aria-describedby="scroll-dialog-description" |
| 86 | + > |
| 87 | + <DialogTitle id="scroll-dialog-title">Google Analytics eCommerce |
| 88 | + event details</DialogTitle> |
| 89 | + <DialogContent> |
| 90 | + <DialogContentText |
| 91 | + tabIndex={-1} |
| 92 | + > |
| 93 | + <p>{selectedEvent?.timestamp} |
| 94 | + <Link |
| 95 | + to={`https://developers.google.com/gtagjs/reference/ga4-events#${selectedEvent?.name}`} |
| 96 | + aria-label={`GA4 event reference documentation`} |
| 97 | + target='_blank' |
| 98 | + > |
| 99 | + {selectedEvent?.name} |
| 100 | + </Link> {selectedEvent?.description}</p> |
| 101 | + <Tabs value={value} onChange={handleChange}> |
| 102 | + <Tab label="gtag.js Code"/> |
| 103 | + <Tab label="Google Tag Manager Code" disabled/> |
| 104 | + |
| 105 | + </Tabs> |
| 106 | + <TabPanel index={0}> |
| 107 | + Item One |
| 108 | + </TabPanel> |
| 109 | + <TextField |
| 110 | + multiline |
| 111 | + defaultValue={selectedEvent?.snippet} |
| 112 | + variant="filled" |
| 113 | + fullWidth={true} |
| 114 | + InputProps={{ |
| 115 | + readOnly: true, |
| 116 | + }} |
| 117 | + /> |
| 118 | + </DialogContentText> |
| 119 | + </DialogContent> |
| 120 | + <DialogActions> |
| 121 | + <Button color="primary"> |
| 122 | + Copy |
| 123 | + </Button> |
| 124 | + <Button onClick={handleClose} color="primary"> |
| 125 | + Close |
| 126 | + </Button> |
| 127 | + </DialogActions> |
| 128 | + </Dialog> |
| 129 | + </div> |
| 130 | + ) |
| 131 | +} |
0 commit comments