Search code examples
next.jsanalyticsscript-tagmoengage

MoEngage on NextJS


I have a problem to implement MoEngage in Next.js.

Here's the MoEngage documentation for Reactjs, MoEngage ReactJS

But the problem is, I can't find index.html in NextJS. I already try to put in _documents.tsx, under Head component.

import {Html, Head} from 'next/document'
<Html>
   <Head>
     <script
        dangerouslySetInnerHTML={{
          __html: `
          (function(i, s, o, g, r, a, m, n) {
    i.moengage_object = r;
    t = {};
    q = function(f) {
      return function() {
        (i.moengage_q = i.moengage_q || []).push({ f: f, a: arguments });
      };
    };
    (f = [
      'track_event',
      'add_user_attribute',
      'add_first_name',
      'add_last_name',
      'add_email',
      'add_mobile',
      'add_user_name',
      'add_gender',
      'add_birthday',
      'destroy_session',
      'add_unique_user_id',
      'moe_events',
      'call_web_push',
      'track',
      'location_type_attribute',
    ]),
      (h = { onsite: ['getData'] });
    for (k in f) {
      t[f[k]] = q(f[k]);
    }
    a = s.createElement(o);
    m = s.getElementsByTagName(o)[0];
    a.async = 1;
    a.src = g;
    m.parentNode.insertBefore(a, m);
    i.moe =
      i.moe ||
      function() {
        n = arguments[0];
        return t;
      };
    a.onload = function() {
      if (n) {
        i[r] = moe(n);
      }
    };
  })(
    window,
    document,
    'script',
    'https://cdn.moengage.com/webpush/moe_webSdk.min.latest.js',
    'Moengage'
  );

  Moengage = moe({
    app_id: ${moeID},
    debug_logs: ${moeDebug},
    swPath: '/service-worker.js',
  });
      `,
        }}
      />
   </Head>
</Html>

It doesn't work, Does anyone know how to implement this script tag on NextJS? I try this method just like I add Google Analytics, it works for Google Analytics, but not for MoEngage.

Thank you.


Solution

  • If you aren't finding index.js then I'm assuming there must be _app.js file in your code base._app.js Allows us to write code that will impact ALL of our pages - so it is the perfect place for us to create the Script components

    First things first!!

    If you are using next.js 11/12, the <Script> tag from 'next/script' should be placed in your _app.js inside the return block And also above the <Head> tag. Something like this,

    import Script from 'next/script';
    import Head from 'next/head';
    
    // ...
    
    // Script tag belongs ABOVE the <Head> tag inside your _app.js file's return block.
    
    <Script
            strategy="afterInteractive"
            dangerouslySetInnerHTML={{
              __html: `(function(i, s, o, g, r, a, m, n) {
                i.moengage_object = r;
                t = {};
                q = function(f) {
                  return function() {
                    (i.moengage_q = i.moengage_q || []).push({ f: f, a: arguments });
                  };
                };
                (f = [
                  "track_event",
                  "Web_Start_your_application",
                  "Web_Signup",
                  "Web_Want_BRE1_limitincrease_click",
                  "Web_Happy_with_BRE1limit_click",
                  "Web_Complete_Profile",
                  "Web_On_bank_statement",
                  "Web_Bank statement click_perfios",
                  "Web_Bank statement click_manual",
                  "Web_Suspend_bank_statement_perfios",
                  "Web_Suspend_bank_statement_manual",
                  "Web_Suspend_salaryslip",
                  "Web_Smart_Repay_Click",
                  "Web_downloadapp",
                  "destroy_session",
                  "add_unique_user_id",
                  "moe_events",
                  "call_web_push",
                  "track",
                  "location_type_attribute"
                ]),
                  (h = { onsite: ["getData"] });
                for (k in f) {
                  t[f[k]] = q(f[k]);
                }
                a = s.createElement(o);
                m = s.getElementsByTagName(o)[0];
                a.async = 1;
                a.src = g;
                m.parentNode.insertBefore(a, m);
                i.moe =
                  i.moe ||
                  function() {
                    n = arguments[0];
                    return t;
                  };
                a.onload = function() {
                  if (n) {
                    i[r] = moe(n);
                  }
                };
              })(
                window,
                document,
                "script",
                "https://cdn.moengage.com/webpush/moe_webSdk.min.latest.js",
                "Moengage"
              );
    
              Moengage = moe({
                app_id: {YOUR_APP_ID},
                debug_logs: 0
              });`
            }}
      ></Script>
          
     // If you have other scripts...     
     
    <Head>
    //.....
    </Head>