How to install LiveSession with React or Angular application?
LiveSession provides a SDK package to install script in your favorite framework. This tool also let you work easier with LiveSesion API. We've prepared some examples how to install tracking code in React and Angular.
In both cases start with installing LiveSesion SDK
npm i @livesession/sdk
or
yarn add @livesession/sdk
React usage
We recommend two ways to integrate SDK with your app. First of all, import a package into your project
import ls from "@livesession/sdk";
Init script in file where you're rendering a whole application:
import ls from "@livesession/sdk"; ls.init("YOUR-TRACK-ID"); ls.newPageView(); ReactDOM.render(<App />, document.getElementById("root"));
If you are using something like Layout component, you can also use lifecycle method componentDidMount in it.
import ls from "@livesession/sdk"; class Layout extends Component { componentDidMount(){ ls.init("YOUR_TRACK_ID"); ls.newPageView(); } render() { return ( // your layout ); } } export default Layout;
If you have React 16.8 or higher - try with useEffect hook
import ls from "@livesession/sdk"; const Layout = () => { useEffect(() => { ls.init("YOUR_TRACK_ID"); ls.newPageView(); },[]) return ( // your layout ) } export default Layout;
Angular usage
Import SDK into your main app component
Import
OnInit
from@angular/core
Implement
OnInit
and call LiveSession init method inngOnInit
function// app.component.ts import ls from '@livesession/sdk' export class AppComponent implemets OnInit { ngOnInit(){ ls.init("YOUR_TRACK_ID"); ls.newPageView(); } }
If you want to know more informations about configuration, go to LiveSesion Developers or read instructions on LiveSesion SDK's Github page.