If integrating GrowSurf globally throughout your SPA, there are no special instructions required. Simply install your campaign-specific GrowSurf Universal Code onto your site and you can begin using the GrowSurf JavaScript Web API.
Due to the asynchronous behavior of SPAs, if the GrowSurf Universal Code has already been added you may need to re-initialize GrowSurf at times. For example, if embeddable elements are not being displayed, call growsurf.init().
Check out the growsurf-samples
GitHub repo for sample code.
Check out the GitHub sample code for React here.
To initialize GrowSurf asynchronously within your SPA, simply include the following code within your React components componentDidMount()
lifecycle event.
App.jscomponentDidMount() {const script = document.createElement('script');script.src = 'https://app.growsurf.com/growsurf.js?v=2.0.0';script.setAttribute('grsf-campaign', 'jaoh4t');script.async = true;document.head.appendChild(script);}
This snippet of code will only add the GrowSurf Universal Code when a visitor is brought to a page within your site that includes the component with this code.
Once the script has been added successfully and initialized, you will then have the ability to add any GrowSurf embeddable element(s) you would like within your components JSX.
App.jsrender() {return (<div><div className='App'>REACT + GROWSURF</div><div data-grsf-block-form></div></div>)}
Check out the GitHub sample code for Vue here.
If you experience issues with embeddable elements rendering on the page (depending on how URL routes are handled in your SPA, this behavior will typically be seen when you refresh the webpage with the absolute route) you will need to call growsurf.init()
to re-initialize GrowSurf to make the embeddable elements render.