This feature is available only with paid Kochava accounts. Contact us to learn more.
NOTE: If you are planning on using Next.js, please visit the Next.js integration support documentation.
NOTE: If migrating from a previous Kochava SDK javascript snippet, be sure to remove that integration snippet and migrate any Kochava SDK function calls as described below.
Integrating the SDK
Requirements:
- NodeJs
- NPM
- React
Supported Platforms:
- React (Web)
Data Privacy:
Integration:
The Kochava Web SDK is available for React projects as a normal npm package. It provides a class called Kochava
, which contains all necessary Kochava SDK functions and behavior.
- Install the Kochava SDK via NPM by running
npm install -D kochava@latest
in the root of your project directory (where package.json is located). For more installation options, see our NPM Installation page. - After running the command above, confirm that kochava version 3.0.1 or later was added in your package json. At this point the SDK has been installed and is ready to use.
Starting the SDK
In order for the SDK to start operating, the Kochava class must be instantiated one time with a valid configuration. However, React is a very open-ended framework full of derivatives, and so this can be accomplished many ways. Below is one example of how this can be done, but your implementation may vary depending on your architecture.
Once you have added the Kochava SDK to your project, the next step is to create and start the SDK class in code. Only your App GUID is required to start the SDK with the default settings, which is the case for typical integrations.
We recommend starting the SDK as soon as the application starts, although this can be done later if needed. Starting the tracker as early as possible will ensure it’s started before use, resulting in more accurate data/behavior.
Within your root component which calls ReactDOM.Render (usually a main or index file):
- Import the Kochava class.
- Create an instance of the Kochava class (we recommend calling it lower-case ‘kochava’).
- (Optional) Make any desired pre-start configuration calls (registerIdentityLink, disableAutoPage, useCookies, etc).
- Call startWithAppGuid using a valid Kochava App GUID.
Example — (Start Kochava in the Root Component)
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css'
import App from './App';
// Import Kochava
import { Kochava } from 'kochava';
const KochavaSetup = () => {
const kochava = Kochava.createForReact();
// Optional pre-start calls would go here
kochava.startWithAppGuid("YOUR_APP_GUID");
return kochava;
};
ReactDOM.render(
<React.StrictMode>
<App kochava={KochavaSetup()} />
</React.StrictMode>,
document.getElementById('root')
);
Replace YOUR_APP_GUID
with your Kochava App GUID. For more information on locating your App GUID, refer to our Locating your App GUID support documentation.
Optional Configuration
From here on, the SDK is integrated and ready, the following configuration calls are optional, and are only for special desired SDK behavior. The following code snippets should be placed at in the above snippet, at the comment labeled Optional pre-start calls will go here
.
Call this function with an argument of true to stop the SDK from automatically signaling a page event when the SDK starts.
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css'
import App from './App';
// Import Kochava
import { Kochava } from 'kochava';
const KochavaSetup = () => {
const kochava = Kochava.createForReact();
// Auto pages will be sent (default)
kochava.disableAutoPage(false);
// Auto pages will not be sent
kochava.disableAutoPage(true);
kochava.startWithAppGuid("YOUR_APP_GUID");
return kochava;
};
ReactDOM.render(
<React.StrictMode>
<App kochava={KochavaSetup()} />
</React.StrictMode>,
document.getElementById('root')
);
Call this function with an argument of true to drop the Cookie on the website to track a device across sub-domains.
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css'
import App from './App';
// Import Kochava
import { Kochava } from 'kochava';
const KochavaSetup = () => {
const kochava = Kochava.createForReact();
// Will not use cookies (default)
kochava.useCookies(false);
// Will use cookies
kochava.useCookies(true);
kochava.startWithAppGuid("YOUR_APP_GUID");
return kochava;
};
ReactDOM.render(
<React.StrictMode>
<App kochava={KochavaSetup()} />
</React.StrictMode>,
document.getElementById('root')
);
React Examples
Example — (Access Kochava from a Class Component):
// App.js
import logo from './logo.svg';
import './App.css';
import React from 'react';
class App extends React.Component {
render() {
const kochava = props.kochava;
// Example - access the kochava object to send a page event
kochava.sendPageEvent();
// Example - register and Identity Link
kochava.registerIdentityLink("standalone", "idLink");
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit src/App.js and save to reload.
</p>
<a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer">
Learn React
</a>
</header>
</div>
);
}
}
export default App;
Example — (Access Kochava from a Functional Component):
// App.js
import logo from './logo.svg';
import './App.css';
function App(props) {
const kochava = props.kochava;
// Example - access the kochava object to send a page event
kochava.sendPageEvent();
// Example - register and Identity Link
kochava.registerIdentityLink("standalone", "idLink");
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit src/App.js and save to reload.
</p>
<a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer">
Learn React
</a>
</header>
</div>
);
}
export default App;
Confirm the Integration
Where to Go From Here:
Now that you have completed integration you are ready to utilize the many features offered by the Kochava SDK. Continue on to Using the SDK and choose a topic.