Built by

RSC-Labs

Category

Other

Version

0.1.3

Last updated

Sep 14, 2024, 10:59:34 AM3 days ago

Medusa Marketing

Note: This plugin will be migrated to Medusa v2, when v2 will reach production readiness.

What is it?

Medusa Marketing is a plugin which provides a framework for sending emails based on the user's actions.

How to install?

  1. Install the package with Copy to clipboardyarn add @rsc-labs/medusa-marketing or Copy to clipboardnpm i @rsc-labs/medusa-marketing.
  2. In Copy to clipboardmedusa-config.js, add the plugin to the Copy to clipboardplugins array and set Copy to clipboardenableUI
const plugins = [
// ... other plugins
{
resolve: `@rsc-labs/medusa-marketing`,
options: {
enableUI: true
}
}
]
  1. Run migrations, e.g. Copy to clipboardnpx medusa migrations run (see: https://docs.medusajs.com/development/entities/migrations/overview) as plugin uses new tables.

Getting started

You can run a Medusa after plugin installation, but without email configuration you won't be able to send emails.

Before running a Medusa

Firstly, you need to configure email transport. You need to modify Copy to clipboardmedusa-config.js and under plugin section add the email transport configuration. The example looks like this:
{
resolve: "@rsc-labs/medusa-marketing",
options: {
enableUI: true,
},
email_transports: [
{
name: 'smtp',
configuration: {
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: "user@gmail.com",
pass: "app-password"
}
}
}
]
}
In the above example, we have configured SMTP transport. Firstly you need to provide a unique name (e.g. Copy to clipboardsmtp) and configuration assigned to this name. Name is then used on the frontend to choose the transport. Under the hood, plugin uses https://github.com/forwardemail/email-templates, which means it accepts every configuration which is supported by https://nodemailer.com (see: https://github.com/forwardemail/email-templates?tab=readme-ov-file#basic) .The example of SMTP follows example here: https://nodemailer.com/about/#example.
In a plugin, you may have many email transports, but today we support only Copy to clipboardsmtp.

Templates

To create an email content, plugin uses templates. At this moment, plugin supports two kind of templates - Copy to clipboardhtml and Copy to clipboardpug. For simplicitly, we provide basic examples under Copy to clipboard/assets/email-templates.
If you would like to create your own templates, create a new directory somewhere and set this directory as Copy to clipboardtemplateDir parameter in Copy to clipboardmedusa-config.js. It needs to be relative to Copy to clipboard/dist folder. For example, if you create directory called Copy to clipboardmy-email-templates on the same level as Copy to clipboard/dist then you need to configure it like:
templateDir: `../my-email-templates`
If you do not set it, plugin uses default templates.
Your directory needs to follow below structure (see example here: https://raw.githubusercontent.com/RSC-Labs/medusa-marketing/main/src/assets):
.
└── my-email-templates
└── html
└── <template-name>
└──html.html
└── pug
└── <template-name>
├── html.pug
└── subject.pug
Under Copy to clipboardmy-email-templates you need to indicate the kind of template (Copy to clipboardhtml or Copy to clipboardpug). Under kind of template you need to put your template name (e.g. my-super-template-name). Under Copy to clipboard<template-name> create proper files (Copy to clipboardhtml.html for Copy to clipboardhtml kind and two files - Copy to clipboardhtml.pug and Copy to clipboardsubject.pug for Copy to clipboardpug).

After configuration

Finally we configured our plugin! Now, we can run Medusa application and play around with the frontend :)
After running Medusa, you will see new option on the sidebar named Copy to clipboardMarketing. This will lead you to such view:
This is a view of configured emails and useful statistics. Let's now describe it shortly:
  • Copy to clipboardWelcome and Copy to clipboardBack in stock cards indicates two actions which can be configured. There can be more in the future. You can notice there following information:
    • status of action - first time you see Copy to clipboardUndefined, but when you will configure it, then you will see Copy to clipboardEnabled or Copy to clipboardDisabled
    • emails processed - this stat describes how many emails have been processed under this action
    • subscriptions - this stat describes how many subscribtions this action has
  • Copy to clipboardRecent activity card shows the processed or failed emails.

Processed and failed emails

The plugin provides a framework for configuring actions, which are transformed to emails. However, the plugin itself does not send emails - it uses different email transports (e.g. SMTP) to send messages. Plugin is not able to tell if email has been delivered to the user, because it is under transport responsibility. In short, it looks like this:
Plugin -> Email transport -> User
The only thing which we can check is Copy to clipboardPlugin -> Email transport relation. Because of that, Copy to clipboardProcessed status means that it has been succesfully moved from Copy to clipboardPlugin to Copy to clipboardEmail transport. Copy to clipboardFailed status means that there was an issue by moving the message to Copy to clipboardEmail transport - it can be a misconfiguration or a bug.

Settings

To configure action, you need to firstly go to Copy to clipboardSettings. As an example, let's use Copy to clipboardBack in stock.
Let's go through every setting:
  • Copy to clipboardSee trigger and parameters
    • click on the icon and you will see what is a exact trigger and what parameters you can use in your templates.
  • Copy to clipboardEnable....
    • simply - enable or disable sending messages
  • Copy to clipboardEmail subject
    • subject of an email, which will be send
  • Copy to clipboardChoose template for email
    • if you properly configured a plugin (see Before running a Medusa), you shall be able to choose a predefined template.
After choosing a template, you will see an email preview. Preview is based on the last entities from database (e.g. if template has parameters related to product, then preview takes the last product from database as an example).
Now, you can click save and if you enabled the action, then messages will be send automatically to clients.

Subscriptions

Some actions (like Copy to clipboardback in stock) requires subscribers. In other words - customer needs subscribe for the action to get an email. We expose APIs which can be used in your Storefront. For more info please check: Store API

Q&A

Why it is so complicated?

We try to keep plugin as much flexible as possible - that's why you need to configure your transport and create your own templates. We do not try to implement our own SendGrid portal - it will be to much. We just want to provide a bridge between Medusa and sending emails.
What is more, we want to keep plugin vendor-agnostic, so that's why we support very low level configuration of email transport. However, we might consider supporting Brevo or SendGrid in the futurea as out of the box transports.

Proposals, bugs, improvements

If you have an idea, what could be the next highest priority functionality, do not hesistate raise issue here: Github issues

License

MIT

Build your own plugins

Develop your own plugins with our API to speed up your processes.

Make your plugin available via npm for it to be shared in our Plugin Library with the broader Medusa community.