April 26, 2020
Estimated Post Reading Time ~

Using script reference in AEM ContextHub

While creating an audience in AEM contextHub, we see a component called Script Reference. When we drag-n-drop the component, we don't see any scripts by default in the dropdown.



Create or select an audience Segment, then edit the segment. On the left side, we can see the script reference component but we don't see any scripts in the dropdown. These scripts are defined in clientlibs located at /libs/cq/contexthub/code/kernel/segment-engine/comparison-scripts/. There is a sample script defined but not added in js.txt of "clientlibs context hub.segment-engine.scripts". So create a clientlibs under /app/ then add script name to js.txt. Now refresh the segment page and see if the script name is displayed in the dropdown.



Sample Script:
(function() {
    'use strict';

    /**
     * Sample script.
     *
     * @param {Object} val1 - sample value
     * @param {Object} val2 - sample value
     * @returns {Boolean}
     */
    var testScript = function(val1, val2) {
        /* let the SegmentEngine know when script should be re-run */
        this.dependOn(ContextHub.SegmentEngine.Property('profile/age'));
        this.dependOn(ContextHub.SegmentEngine.Property('profile/givenName'));

        /* variables */
        var name = ContextHub.get('profile/givenName');
        var age = ContextHub.get('profile/age');

        /* return result */
        return name === 'Joe' && age === 123 && val1 === 11 && val2 === 22;
    };

    /* register function */
    ContextHub.SegmentEngine.ScriptManager.register('test-script', testScript);

})();


By aem4beginner

1 comment:

  1. Not a big fan of how you block people from copying and pasting from the page.

    ReplyDelete

If you have any doubts or questions, please let us know.