Feeling stuck with Segment? Say 👋 to RudderStack.

SVG
Log in

How to send data from Node.js to WebEngage

In this article, we will explore the process of sending data from Node.js to WebEngage, a powerful customer engagement platform. Whether you are a beginner or an experienced developer, this guide will equip you with the knowledge necessary to seamlessly integrate these two technologies. Let's dive in!

Understanding the basics of Node.js and WebEngage

Node.js is an open-source, server-side runtime environment built on Chrome's V8 JavaScript engine. It allows you to run JavaScript code outside the browser, making it ideal for server-side applications. With Node.js, you can leverage the power of JavaScript to build scalable and high-performance web applications.

WebEngage, on the other hand, is a user engagement platform that helps businesses engage, retain, and convert their customers effectively. It provides a toolkit of features and tools that enable businesses to engage with their users in a personalized and targeted manner.

What is Node.js?

Node.js leverages an asynchronous, event-driven architecture, which means it can handle a large number of concurrent connections efficiently. This makes it highly suitable for building applications that require real-time communication or handle a high volume of user requests.

One of the key advantages of using Node.js is its vast ecosystem of libraries and frameworks. These libraries and frameworks provide developers with a wide range of tools and functionalities to build robust and scalable web applications. Some popular Node.js frameworks include Express.js, Koa.js, and Nest.js.

With Node.js, you can easily build APIs, web servers, real-time chat applications, streaming applications, and more. Its flexibility and performance make it a popular choice among developers for building server-side applications.

What is WebEngage?

WebEngage is a powerful toolkit that provides businesses with tools to engage with their users effectively. It offers a wide range of features and functionalities that enable businesses to deliver personalized and targeted messages to their users.

One of the key features of WebEngage is personalized messaging delivered across different channels such as SMS, Email, WhatsApp, Push Notifications, and Google Ads. With personalized messaging, businesses can create customized messages for their users based on their behavior, preferences, and demographics. This helps businesses deliver relevant and engaging content to their users, increasing user engagement and conversion rates.

WebEngage also offers push notifications, which allow businesses to send targeted messages directly to their users' devices. Push notifications are a great way to keep users informed about new offers, updates, or important events. They can be highly effective in driving user engagement and retention.

In-app messages are another powerful feature of WebEngage. With in-app messages, businesses can display targeted messages to their users while they are using their applications. These messages can be used to promote new features, offer discounts, or gather feedback from users. In-app messages are a great way to engage with users at the right time and in the right context.

By integrating Node.js with WebEngage, businesses can unlock the full potential of both technologies and deliver a seamless user experience. With Node.js, businesses can build scalable and high-performance server-side applications, while WebEngage provides the marketing automation tools and features to engage users effectively. Together, they enable businesses to create personalized and targeted user experiences that drive engagement, retention, and conversion.

Why do we need to send data from Node.js to WebEngage?

It’s pretty easy to send data from the client side of your website (the frontend) by simply adding the WebEngage SDK to your webpage html as follows:

JAVASCRIPT
<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://ssl.widgets.WebEngage.com" : "http://cdn.widgets.WebEngage.com") + "/js/WebEngage-min-v-6.0.js",
d.parentNode.insertBefore(f, d)
})
}
}(window, document, "WebEngage");
WebEngage.init('YOUR_WebEngage_LICENSE_CODE');
</script>

However, you might want to send some custom events, especially those which can only be tracked from the backend, server-side (from your Node.js code). In order to do this, you’ll need to integrate WebEngage server-side. This isn’t as easy as integration with the front end because there’s no SDK provided by WebEngage for Node.js. So, we will have to use the WebEngage REST APIs for this server-side WebEngage integration.

Setting up your Node.js environment

Before we can dive into the server-side integration of Node.js with WebEngage, let's make sure our Node.js environment is set up correctly. If you haven't already installed Node.js, follow the steps below.

Installing Node.js

To install Node.js, visit the downloads page of the official website to download the installer for your operating system. Node.js provides installers for various platforms, including Windows, macOS, and Linux.

Once downloaded, run the installer and follow the installation wizard instructions. The installer will guide you through the installation process, allowing you to customize the installation location and other settings. After a successful installation, you should be able to run Node.js commands from your terminal or command prompt.

Setting up your first Node.js server

Now that Node.js is installed, let's create a simple Node.js server to test our setup. Open your favorite text editor and create a new file called "server.js". In this file, we'll write the following code:

JAVASCRIPT
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!');});
server.listen(3000, 'localhost', () => {
console.log('Server running at http://localhost:3000/');
});

In the code above, we are using the built-in 'http' module of Node.js to create a basic HTTP server. The server listens on port 3000 and responds with a 'Hello, World!' message for every incoming request. This is a simple example to demonstrate the basic functionality of a Node.js server.

Save the file and navigate to the folder where it is located using your terminal or command prompt. Run the following command to start the server:

JAVASCRIPT
node server.js

If everything is set up correctly, you should see the following message in your terminal:

SH
Server running at http://localhost:3000/

Congratulations! You have successfully set up your first Node.js server. Now, let's move on to the next section and explore the WebEngage API.

Before we proceed, let's take a moment to understand what just happened. When you executed the 'node server.js' command, Node.js started the server and it began listening for incoming HTTP requests on port 3000. The server responded with a 'Hello, World!' message for every request it received. This is the basic foundation of a Node.js server, and from here, you can build more complex and feature-rich applications.

In the next section, we will explore the WebEngage API and see how we can integrate it with our Node.js server to enhance the functionality of our applications. Stay tuned!

Introduction to WebEngage API

To interact with WebEngage programmatically, we need to understand its API. The WebEngage API allows us to perform various operations, such as sending user data, triggering events, and more. Let's take a closer look at the WebEngage API and how we can use it in our Node.js applications.

Understanding WebEngage API

The WebEngage API provides a set of endpoints that allow developers to interact with the WebEngage platform. These endpoints enable us to send user data, trigger events, and perform various other actions. To use the WebEngage API, we need to obtain an API key from our WebEngage account, which we'll cover in the next section.

Setting up your WebEngage account

To integrate Node.js with WebEngage, you need to create a WebEngage account and obtain an API key. Visit the WebEngage website and sign up for an account. Once you have signed up, you can access your API key from the WebEngage dashboard. Make sure to keep this API key secure, as it grants access to your WebEngage account.

Integrating Node.js with WebEngage

Now that we have a basic understanding of Node.js and WebEngage, as well as a working Node.js server, let's dive into the integration process. In this section, we will create a connection between Node.js and WebEngage, and send user data from Node.js to WebEngage.

Install the necessary Node.js dependencies

We will use WebEngage REST APIs to send events. We will make an `http` request to WebEngage APIs for events. Though we can use the native `https` module available in Node.js to make the requests, to make the code cleaner and do this faster, we will use Axios the Open Source node.js package that will work as an http client for us. So the first step is to install the package in the project by running the following command:

SH
npm install axios --save

Sending data from Node.js to WebEngage

Now we can use WebEngage’s Tracking Events API

JAVASCRIPT
const axios = require('axios');
let data = {
"userId": "johndoe",
"eventName": "Added to Cart",
"eventTime": "2018-09-15T18:29:00-0800",
"eventData": {
"Product ID": 1337,
"Price": 39.80,
"Quantity": 1,
"Product": "Givenchy Pour Homme Cologne",
"Category": "Fragrance",
"Currency": "USD"
}
};
axios({
method: 'post',
url: 'https://api.WebEngage.com/v1/accounts/ACCOUNT_ID/events', // replace ACCOUNT_ID with your WebEngage account ID
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY', // replace YOUR_API_KEY with your actual API key
},
data: data
}).then((response) => {
console.log(response.data);
}).catch((error) => {
console.log(error);
});

Congratulations! You have successfully integrated Node.js with WebEngage and sent user data from your Node.js server to WebEngage. Feel free to explore the WebEngage API docs for more advanced features and customization options.

Troubleshooting common issues

As with any integration, you may encounter some common issues and errors. In this section, we'll cover a few common problems and provide solutions to help you troubleshoot effectively.

Dealing with connection errors

If you are unable to establish a connection between your Node.js server and WebEngage, make sure you have correctly used the APIs as per documentation. Double-check your API key, Account ID, and ensure that it is not misspelled or missing any characters. Additionally, check your internet connection to ensure that your server can communicate with the WebEngage platform.

Ensuring data integrity

When sending user data from Node.js to WebEngage, it is essential to ensure the integrity of the data. Validate and sanitize user input to prevent any potential security vulnerabilities. Ensure that you send accurate and up-to-date data to WebEngage, as incorrect or outdated data can lead to inaccurate analysis and ineffective user engagement.

As you continue to work with Node.js and WebEngage, you may encounter other issues specific to your use case. Don't hesitate to refer to the official documentation and seek assistance from the vast online community for further guidance.

Conclusion

Integrating Node.js with WebEngage allows you to leverage the power of both technologies and provide a personalized and engaging user experience. We covered the basics of Node.js and WebEngage, set up a Node.js environment, explored the WebEngage API, integrated Node.js with WebEngage using WebEngage REST APIs, and addressed common troubleshooting issues. Armed with this knowledge, you are now ready to take your Node.js and WebEngage integration to the next level. Happy coding! Check out RudderStack's node js to Webengage integration.

Don't want to go through the pain of direct integration? RudderStack's Node.js SDK makes it easy to send data from your Node.js app to WebEngage.