Add Now4real to your site
Add Now4real to your website to instantly offer your visitors and users some new exciting features. They can see, in very real time, how many people are viewing the same page they have opened in their browser. They can do a group chat with them (a pagechat). They can see which pages of your website are being viewed by most people at that moment. And much more!
Luckily enough, adding Now4real to your site is as easy as pie because it’s a fully managed cloud service. There are three ways to add it, from ridiculously easy to coding with the API. Let’s see them all.
The ridiculously easy way
The easiest way to add Now4real to your site is to use the ready-made widget with its out-of-the-box configuration. You just need to add one single line of code to all the pages of your website (yes, we call it the magic line of code):
You can put this line wherever you want, even if it is usually inserted before the </body> tag at the end of the page or within the <head> section at the beginning of the page.
This will display the blue Now4real widget in the bottom-left corner of all your pages (similar to the one you see on this very page). If you want to change the position, the color, or the behavior of the widget, check out The easy way below.
There are several ways to add this line of code to the pages of your site, depending on what platform it is based on. For example, if your site is based on WordPress, we will release a plugin soon, but in the meanwhile, just follow these easy steps: 1) Install and activate a third-party plugin called Header and Footer Scripts. 2) Go to Settings > Header and Footer Scripts. 3) Paste the code copied from above into the “Scripts in footer” area and click Save settings.
Congratulations! That’s all, really. As soon as you put the code on your pages, Now4real starts working seamlessly, and you are not required to do anything else. But we suggest you register your site on the Now4real Publisher Dashboard. The Dashboard gives you full control over how Now4real works on your site and provides you with historical analytics, access to the full chat transcripts, and many other features. Skip to Register your site below to learn how to do it (again, that’s extremely easy).
If you need a bit of instruction on how to use the Now4real widget as an end user, check out the Help page.
The easy way
If you want to configure any of the widget parameters, such as colors or position, you need to add a slightly longer code snippet to all the pages of your website, instead of the single line above. Here you go:
The part you can modify is the now4real.config object, where you can put your own configuration parameters, which are defined in JSON format.
For example, the code snippet above defines two parameters:
- widget contains an object that defines the colors of the widget (blue and white, in this case, which is the same as the default configuration).
- scope tells Now4real whether to create a single pagechat or not. The two possible values are:
- ‘page’ creates a different pagechat for every page of the site. Chat messages posted by the visitors of a page can be seen only on that page and not on other pages. Furthermore, the widget bubble shows two counters: the current number of viewers of the current page and the current number of viewers of the site.
- ‘site’ (default value) creates a single pagechat shared among all the pages of the site. Chat messages posted by the visitors of a page can be seen on any other pages. The widget bubble shows only the counter of the current viewers of the site.
TIP: The “site” behavior is useful for websites and blogs with small numbers of concurrent viewers because it allows users to meet easily, even if they are on different pages. This is the reason why we chose it as the default option. On the other hand, for sites with more visitors, you might want to go for the “page” behavior.
You will probably want to change the colors and the position of the widget to adapt to the look-and-feel of your site. For example, let’s configure Now4real with a black and orange bubble aligned to the right of the page and with the site scope (the default, so we don’t need to declare it). You will need to put a string like the below in now4real.config:
The resulting code snippet becomes like this:
There are several other parameters you can use within the now4real.config object. They give you control over other aspects, like widget visibility and logging. Check them out and play.
The full list of parameters is available in the API reference documentation, under the Config object.
As explained in the previous section, putting the code above on your website is enough to see Now4real working, even in production. But we suggest you register your site on the Now4real Publisher Dashboard to take benefit from the many features available. Skip to Register your site below to learn how to do it.
The coding way
You have two options. One is to fork the default widget to build your own [we will release it on GitHub soon… stay tuned]. The other is to start from scratch with the API, as we are going to show in the tutorial below.
But first, what are some good reasons to build your own Now4real client? Here are a few:
- You can create your own widgets, from the most minimalistic to the utterly massive ones… We’d love to see your creativity!
- You can show the presence counters directly inside your pages, rather than in a widget. Imagine a news site homepage, containing the titles of the articles. Wouldn’t it be amazing to show the number of current readers in real time next to each title? The same applies to the real-time ranking of the top pages.
- The Now4real API contains a full-fledged chat API. You can display the pagechats the way you prefer and integrate them wherever you want. And you can implement your own chat front-end, leveraging the fully managed chat back-end of Now4real (the Now4real cloud).
While developing with Now4real, please consult the API reference documentation.
A few definitions
Let’s quickly introduce a few terms to make sure we are on the same page…
- Page presence: Presence of a person on a page of a website (the browser tab must be in the foreground at that moment).
- Page viewer counter: Real-time counter of people who are present on a page.
- Site viewer counter: Real-time counter of people who are present on any page of the site.
- Pagechat: A group chat that lives on a web page (it’s like a chat room dedicated to that page).
- Page chatter counter: Real-time counter of people who are chatting on a page (in a pagechat).
- Site chatter counter: Real-time counter of people who are chatting on any page of the site (in any pagechat).
- Page viewer ranking: Real-time list of pages with most viewers.
- Page chatter ranking: Real-time list of pages with most chatters.
- Page viewer heatmap: Real-time density of page viewers on the world map (at country level).
- Site viewer heatmap: Real-time density of site viewers on the world map (at country level).
The Now4real API enables you to get these and other data very much in real time from the Now4real cloud. More specifically, these are subjects you can subscribe to so that you’ll get values asynchronously pushed to your code (synchronous get method is available too).
In addition, the Now4real API contains all the building blocks to build a chat, including authentication, message delivery, typing indicators, error management, etc.
Let’s use the Now4real API to create a very simple web page that shows the page viewer counter in real time.
Create an HTML page
First, let’s create a simple page that will contain our project code. Create an empty file named tutorial.html and paste the code below.
Load the Now4real library
It is enough to load the Now4real library to see things happen. The library will immediately connect to the Now4real cloud and will automatically send a presence signal when a user has the page in the foreground.
To load the library, paste the code below before the </body> tag.
This code loads the library from the https://cdn.now4real.com/now4real.js URL, specifying that we only need the API and not the widget (see target: ‘api’ in now4real.config). It then logs a message to the browser console when the library is loaded (see now4real.onload).
Add the front end
Let’s add some basic HTML markup to display our presence counter.
Paste the code below after the <body> tag.
The real-time counter updates will be displayed inside the div element with id=”counter”. In the span element with id=”context” we will display the page context (more details below).
Get the counter
To receive real-time asynchronous updates to the counter, we’ll subscribe to the COUNTER_PAGE_VIEWERS subject.
Paste the code below after the <script> tag.
The subscribeCounter function uses the Now4real API to subscribe to the COUNTER_PAGE_VIEWERS subject while defining a callback function that is called every time a new update is pushed from the Now4real cloud. We read the update value (counterUpdate.data.value) and write it into the div element with id=”counter”.
Get the context
This part of the tutorial is not needed for reaching our goal of displaying the real-time presence counter, but it is useful to help you understand another concept of Now4real.
How does Now4real decide whether different users should be considered on the same page so that it can increase the same counter? By means of the context.
First, the site must be the same. Now4real gets the site as the window.location.host of the page. This is called the site context.
Then, the page must be the same. Now4real gets the page as the window.location.pathname of the page (with a few exceptions). This is called the page context.
By concatenating the site context and the page context, you can know the full context of the page. To display it, paste the code below after the subscribeCounter function.
The showContext function uses the Now4real API to get the site and page contexts and write them into the span element with id=”context”.
In the Let’s play section, we will explain what’s the context of pages loaded from the disk. To learn full details about contexts, please check out pageContext and siteContext in the Now4real API reference.
Putting it all together
Below is the final code of our tutorial.html page. You can also find it on GitHub.
And this is the result when you open the page in a browser loading it from the disk:
Now that the sample page is ready, we can have some fun while learning more about contexts and visibility.
You’ve saved your tutorial.html file to the disk. Open it with your favorite browser. The counter should display “1”, unless there’s somebody else in the world who is viewing a similar “tutorial.html” page (more on this later); in this case, just rename the HTML file to a unique name to make this experiment easier. Now open the same file a second time. It will appear in a second browser tab, and the counter will show “1” again because that’s the only tab in the foreground. Now, drag that tab out of the current window, so that you will have two separate browser windows displaying the tutorial page. The counter will show “2”. If you minimize one of the windows, the counter will go down to “1”. You can try with more windows and see the counter increase accordingly.
A page is considered visible if it is displayed in the foreground tab of a non-minimized window. Some browsers can even detect if the window is covered by a different application window and report it as non-visible in this case.
Now deploy the tutorial.html page to a web server, if you have one available. This allows you to open the same page from different devices. For example, open your page from both a desktop computer and a smartphone. You’ll see the counter go to “2”. Switch off the screen of your smartphone, and the counter will go down to “1”. Switch it on, and the counter will go back to “2”. If you don’t have a web server available, we put the tutorial.html page on our own server to let you play with it. You can find it at https://now4real.com/tutorial.html. Obviously, if other people are viewing this page besides you, the counter will reflect that.
Let’s go back to the tutorial.html page loaded from your local file system. You can see that the site context is called shared-public-sandbox.test. This is a special sandbox context that allows you to test your Now4real code without mounting it on an actual domain. This context originates automatically whenever you load the page from the file system or from a web server on localhost.
The nice thing is that the sandbox context is shared among all the Now4real users. As long as you load pages from disk or from a localhost server, you can meet other developers who are working in the sandbox.
On the other hand, if you load the page from a web server with a domain name, the site context will be given by the domain name. You can see this by loading tutorial.html from your web server or from ours. The site context reported on the page is no more shared-public-sandbox.test but the actual domain name.
One more step… Let’s add the standard Now4real widget to the tutorial.html page, without removing the code we developed above. It’s enough to change the target in your now4real.config object as follows:
Reload the page, and you’ll see something like this:
This makes it easy to test all the features of Now4real, including the chat, while you are developing your code.
Finally, how about the page context? As you can see, when loading the page from the disk, it is just the file name (with a leading ‘/’). So, in our tutorial, it’s /tutorial.html (or whatever name you gave to the file). If you load the page from a web server, the full path is taken. So, if you deployed the page at, say, https://www.example.com/test/tutorial.html, the site context will be www.example.com, and the page context will be /test/tutorial.html. Any query string is not considered. This means that the following URLs are all considered part of the same context:
This tutorial is extremely basic but should help you get started quickly with the Now4real API. There are tons of other things you can do with the API! You can get more counters, ranking lists, and world heatmaps. You can manage social sign up and sign in. You can create your own pagechat front end.
Please take some time to go through the Now4real API reference and explore it.
Register your site
Once you integrate Now4real in your site, as shown in the sections above, it will start working straight away. But if you register your site on the Publisher Dashboard, you will get access to more features. The Dashboard gives you full control over how Now4real behaves on your sites and provides you with historical analytics and access to the chat transcripts.
Sign up for free to the Publisher Dashboard at https://dashboard.now4real.com.
Then, you can add your sites and verify them (to demonstrate you are the owner or are in control).
From the Dashboard, you can also upgrade to the Premium plan, if you decide to unlock any of the premium features.
Easy, right? Now, you are ready to exploit Now4real to its full potential!
Check out your stats
In the Dashboard, you can see some intriguing analytics and stats regarding your websites. They are not meant to replace any traditional web analytics you may already have in place, but to bring a new and fresh perspective on your website traffic.
In the Dashboard home and the Analytics section, you can see the historical charts of concurrent viewers and concurrent chatters. This is a great way to know how popular your site is, as an alternative to the classic pageviews metric.
The Dashboard home shows another pretty important metric called the viewing time. It is the total aggregate time your users spend viewing the pages of your website in a month. As always, we only count the time when a page is actually visible and not when it is in the background or minimized.
Give Now4real a try! You will be surprised by how pleasantly spicy your website will become…