Skip to main content
How to Add RB2B's Script to React.js
Updated over 9 months ago

To integrate your RB2B tracking script into a React.js application and ensure it's applied across the entire site, you'd usually insert it within your top-level component, like App.js, especially if the script doesn't have to be part of the <head> section. React itself doesn't have a built-in feature for directly altering the <head> section of the document. In these cases, the useEffect hook is a practical choice for executing scripts that can operate within the body of your document.

For scenarios where you need to modify the <head> section dynamically, using a third-party library such as react-helmet is a common approach. This library allows you to manage the document's head section effectively, accommodating any scripts or metadata you wish to include or change on a page-by-page basis.

Using useEffect in Your Main App Component

This method is suitable for scripts that don't need to manipulate the document head or that can be executed from the body. This approach ensures that the script runs once when the component mounts.

// App.js or your top-level component
import React, { useEffect } from 'react';

function App() {
useEffect(() => {
// Your custom JavaScript snippet
!function () {
var reb2b = window.reb2b = window.reb2b || [];
if (reb2b.invoked) return;
reb2b.invoked = true;
reb2b.methods = ["identify", "collect"];
reb2b.factory = function (method) {
return function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(method); reb2b.push(args);
return reb2b;
};
};
for (var i = 0; i < reb2b.methods.length; i++) {
var key = reb2b.methods[i];
reb2b[key] = reb2b.factory(key);
}
reb2b.load = function (key) {
var script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
script.src = "https://s3-us-west-2.amazonaws.com/b2bjsstore/b/" + key + "/reb2b.js.gz";
var first = document.getElementsByTagName("script")[0];
first.parentNode.insertBefore(script, first);
};
reb2b.SNIPPET_VERSION = "1.0.1";
reb2b.load("YOUR-UNIQUE-ID");
}();
}, []);

return (
// Your app's component tree
<div className="App">
{/* Your components */}
</div>
);
}

export default App;

Using react-helmet for <head> Modifications

If you decide that your script must be part of the document <head>, you can use react-helmet, a third-party library, to dynamically manage the contents of the head tag.


First, install react-helmet:

npm install react-helmet

Then, you can use it in your app:

// App.js or your top-level component
import React from 'react';
import { Helmet } from 'react-helmet';

function App() {
return (
<div className="App">
<Helmet>
<script>
{`
!function () {
var reb2b = window.reb2b = window.reb2b || [];
if (reb2b.invoked) return;
reb2b.invoked = true;
reb2b.methods = ["identify", "collect"];
reb2b.factory = function (method) {
return function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(method); reb2b.push(args);
return reb2b;
};
};
for (var i = 0; i < reb2b.methods.length; i++) {
var key = reb2b.methods[i];
reb2b[key] = reb2b.factory(key);
}
reb2b.load = function (key) {
var script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
script.src = "https://s3-us-west-2.amazonaws.com/b2bjsstore/b/" + key + "/reb2b.js.gz";
var first = document.getElementsByTagName("script")[0];
first.parentNode.insertBefore(script, first);
};
reb2b.SNIPPET_VERSION = "1.0.1";
reb2b.load("YOUR-UNIQUE-ID");
}();
`}
</script>
</Helmet>
{/* Your components */}
</div>
);
}

export default App;

Make sure to replace "YOUR-UNIQUE-ID" with your actual unique ID.


Conclusion

These methods ensure that your custom JavaScript snippet is included and executed across your entire React.js application. Using useEffect is straightforward for general purposes, while react-helmet is

Did this answer your question?