When using the ManyChat growth tool, the library provides more opportunities:
-
Functionality of using the headless checkbox widget
-
Opportunities to initialize widgets for dynamically created elements
- Control the visibility of widgets, etc.
Initialization #
The loading of the library is done by using a script. The link to this script can be found in the “Growth Tools” section (the “Settings” tab, install the JavaScript snippet). The loading is performed asynchronously, which is why you must wait for the library to load before calling methods of the library. This ismcAsyncInitthe purpose of the callback. After loading, globalMCvariables become available and provide access to functionalities.
<script src="//widget.manychat.com/YOUR_PAGE_ID.js" async="async"></script>
<script>
window.mcAsyncInit = function() {
// MC variable is available now
console.log(MC);
};
</script>
Embedding widget initialization #
To initialize the embedded widget (button or box), you need to add additional code to the page at the location where you want the widget to appear. Typically, this code looks like:
<div class="mcwidget-embed" data-widget-id="YOUR_WIDGET_ID" data-widget-payload="OPTIONAL_PAYLOAD"></div>
You can place the code in multiple locations on the same page. Instances of the widget will be initialized at each location. For each instance of the widget, you can set an optional payload - this is the data that will be saved in the “custom user fields” specified in the widget settings. To set the payload for the widget instance,data-widget-payloadset properties for the element or use the method described below. setPayload
Core methods #
|
method |
description |
|
|
Finds widget elements on the page (for example |
|
|
Returns the widget object by widget ID or the DOM element of the embedded widget instance. With this object, you can listen to events of the widget and call methods. The ID of the widget can be obtained from the code snippet in Growth Tools. |
| MC.getWidgetList() | Returns a list of all active widgets. |
Checkbox widget methods #
All methods and properties listed below apply only to the “Checkbox” widget type.
|
method |
description |
|
|
Subscribe to events
|
|
|
Unsubscribe from an event or all events. If no callback is provided, it will unsubscribe from all events. |
| .submit() |
The method to send the checkbox confirmation. Must be called after clicking the button to confirm joining. |
|
|
This property allows checking the current state of the widget. |
| .ref | The reference string used when submitting the widget, available only after submission. |
| .userRef | The user identifier used when submitting the widget, available only after submission. |
Overlay widget methods #
All methods and properties listed below apply only to “Bar”, “Slide-In”, “Modal”, and “Page Takeover” widget types.
|
method |
description |
| .open() |
Open the widget. |
|
|
Close the widget. |
| .setPayload(refPayload) | Set the reference payload for the widget. The value must be a string. Up to 200 characters. Valid characters area-z A-Z 0-9 +/=-._ |
Customer chat widget methods #
All methods and properties listed belowapply only to the “Customer Chat” widget type.
Rate limiting #
Each “Customer Chat” method is limited to 1 call every 5 seconds.
|
method |
description |
| .open(shouldOpenDialog) |
Open the widget. You can useshouldOpenDialogparameter to determine whether the dialog should also be opened. |
|
|
Close the entire widget. |
| .openDialog() | Open the widget dialog. |
| .closeDialog() | Close the widget dialog. |
| .setLoggedInGreeting(text) | Set the greeting for logged-in users. |
| .setLoggedOutGreeting(text) | Set the greeting for logged-out users. |
| .setPayload(refPayload) | Set the reference payload for the widget. The value must be a string. |
| .set(changes) | Set the greeting and/or ref payload parameters.changesis a JSON object with new values. Available PARAM names:loggedInGreeting,loggedOutGreeting,refPayload |
Embed widget methods #
All methods and properties listed below apply only to “button” and “box” widget types.
To execute methods on the widget, you need to obtain the widget instance object. You cannot get it by passingwidgetIdto the method,MC.getWidgetbecause there may be multiple widget instances on the page at the same time. Therefore, to obtain the instance object, you should pass the Widget DOM element as a parameter when callingMC.getWidget, for example
<div id="test-widget" class="mcwidget-embed" data-widget-id="420"></div>
const widgetInstanceElement = document.getElementById('test-widget')
const widget = MC.getWidget(widgetInstanceElement)
widget.setPayload('custom_payload')
|
method |
description |
| .setPayload(refPayload) | Set the reference payload for the widget. The value must be a string. Up to 200 characters. Valid characters area-z A-Z 0-9 +/=-._ |