Countly Documentation

Countly Resources

Here you'll find comprehensive guides to help you start working with Countly as quickly as possible.

Extending or modifying modules

All previous pages show you how to add new functionality to Countly. But what if you want to modify existing behavior? The best way to do it is to modify the modules directly.

For example, there is a mail.js module in countly/api/parts/mgmt. What if we want to change the template email that Countly sends by default? We can allow our plugin to modify this module through plugin manager. So any instance or process that requires this module would go through our modifications before.

So how can we achieve that.

First we need to modify mail.js module it self.
Well actually it is already modified to be extended, but not all modules are so if you need to extend some module that is not extended, here's how you can do it:

//require plugin manager
var plugins = require('../../../plugins/pluginManager.js');

//extend module with module name "mail" and module object module
plugins.extendModule("mail", mail);

//and only then pass it to exports
module.exports = mail;

Extending other modules

Currently mail.js module already is modified like that and can be extended, if you need to modify any other module these are the changes you need to do. You can also submit a PR with changes to our repo, so your modifications would work on all Countly installations

So that's how we make object go through plugin manager. But what happens underneath, how can we actually modify it?

Underneath plugin manager goes through all the plugins and checks if they have mail module in extend folder of the plugin.

So to extend mail.js you need to have folder extend with mail.js inside it in your plugin.

With your standard plugin structure, it would look like this

│   install.js
│   package.json
│   tests.js
│   uninstall.js
│
├───api
│       api.js
│
├───extend
│       mail.js
│
└───frontend
    │   app.js
    │
    └───public
        ├───images
        │   └───empty
        │           image1.png
        │           image2.png
        │
        ├───javascripts
        │       countly.models.js
        │       countly.views.js
        │
        ├───localization
        │       empty.properties
        │
        ├───stylesheets
        │       main.css
        │
        └───templates
                tempalte2.html
                template1.html

The contents of this mail.js should export function to call, which receives mail object and modifies it. For example it could look like this:

module.exports = function(mail){
    mail.sendPasswordResetInfo = function (member, prid) {
        var message = {
            to: member.email,
            subject: 'Let's reset your password ok',
            html:'Hello,<br/><br/>'+
                'So '+ member.full_name + ', did you forget your password?'
        };
    
        mail.sendMail(message);
    };
}

This way we changed the email that will be sent to members who requested password change, because every process (api or frontend) requiring this module, before exporting project, it will go through our modifications in plugin manager.

But as said previously, it can be applied also to other modules.

Extending or modifying modules