score:1

Accepted answer
  • you should create an account to begin with. you can start with a 14 day free trial. this will enable you to get an account id called licensecode which needs to be used to initialise the web sdk script mentioned in the question.
  • follow the documentation to setup your account.
  • for a reactjs based frontend, you should add the sdk integration script inside your index.html file and into the head section.

<head>
  ..
  ..
  ..
  <script id="_webengage_script_tag" type="text/javascript">
    var webengage;
    ! function(w, e, b, n, g) {
      function o(e, t) {
        e[t[t.length - 1]] = function() {
          r.__queue.push([t.join("."), arguments])
        }
      }
      var i, s, r = w[b],
        z = " ",
        l = "init options track screen onready".split(z),
        a = "feedback survey notification".split(z),
        c = "options render clear abort".split(z),
        p = "open close submit complete view click".split(z),
        u = "identify login logout setattribute".split(z);
      if (!r || !r.__v) {
        for (w[b] = r = {
            __queue: [],
            __v: "6.0",
            user: {}
          }, i = 0; i < l.length; i++) o(r, [l[i]]);
        for (i = 0; i < a.length; i++) {
          for (r[a[i]] = {}, s = 0; s < c.length; s++) o(r[a[i]], [a[i], c[s]]);
          for (s = 0; s < p.length; s++) o(r[a[i]], [a[i], "on" + p[s]])
        }
        for (i = 0; i < u.length; i++) o(r.user, ["user", u[i]]);
        settimeout(function() {
          var f = e.createelement("script"),
            d = e.getelementbyid("_webengage_script_tag");
          f.type = "text/javascript",
            f.async = !0,
            f.src = ("https:" == e.location.protocol ? "https://widgets.in.webengage.com" : "http://widgets.in.webengage.com") + "/js/webengage-min-v-6.0.js",
            d.parentnode.insertbefore(f, d);
        })
      }
    }(window, document, "webengage");

    webengage.init('your_webengage_license_code');
  </script>
  ..
  ..
  ..
</head>

  • now you can access the webengage object inside any of your components and further make use of the various sdk methods mentioned in the documentation.

import react, {
  component
} from "react";

class app extends component {
  componentdidmount() {
    webengage.track('customevent', {
      'firstname': 'alex'
    }); // version 1
    
    window.webengage.track('customevent', {
      'firstname': 'alex'
    }); // version 2
  }

  render() {
    return (
    <div classname = "app" >
      <header classname = "app-header" >
        <img src={logo} classname = "app-logo" alt = "logo" />
        <a classname = "app-link" href = "https://reactjs.org" target = "_blank" rel = "noopener noreferrer" >learn react 
        </a> 
      </header> 
    </div>
    );
  }
}

reactdom.render( <app/> , document.getelementbyid("root"));

  • if you are using some syntax linter like (eslint) in your project, using the first version of code will throw errors. please refer to this detailed explanation for more on referencing unimported libraries.

  • thus, you can try using the second version with the window keyword.

  • further, if you are building a single page application, do checkout the spa version of the webengage's websdk.

hope these steps help you integrate without hassle.


Related Query

More Query from same tag