NAV
javascript

.getWidgetId()

SnapEngage.getWidgetId();

Sometimes it will be helpful to know the ID of the currently-active widget, especially if you use .setWidgetId() to change the active widget on the fly.

No Parameters

Returns

// An example widget ID is returned from the call above
'1z1z1z1z1-y2y2-x3x3-w4w4-v5v5v5v5v5v'
Type Description
String Returns a String that is the full widget ID of the currently active widget. (Alphanumeric plus dashes.)

An example return value of is inside the code example.

Example getWidgetId() Usage

Send a system message to the agent based on the widget ID
SnapEngage.setCallback('StartChat', function(email, msg, type) {
    var success = false;
    var tid;

    function sendWidgetName() {
        var currWidget = SnapEngage.getWidgetId();

        // For each different widget ID, send a message to the agent with that widget's name
        switch (currWidget) {
            case 'xxxxx-xxxx-xxxx-xxxx' :
                success = SnapEngage.sendTextToChat('A Widget\'s Name');
                break;
            case 'yyyyyy-yyyy-yyyy-yyyyy' :
                success = SnapEngage.sendTextToChat('Other Widget\'s Name');
                break;
            default :
                success = SnapEngage.sendTextToChat('Error fetching Widget Name');
                break;
        }
    }

    // Call the function above
    sendWidgetName();

    if (!success) {
        clearTimeout(tid);

        // Try again every two seconds until success.
        tid = setTimeout(sendWidgetName, 2000);
    }
});

Send a message to the agent that changes for each different widget ID. This code sample uses getWidgetId() along with the StartChat JavaScript event and sendTextToChat().