Saturday, 12 January 2019

Using script reference in AEM ContextHub

While creating 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 drop down.


Create or select an audience Segment, then edit the segment. On the left side, we can see script reference component but we don't see any scripts in the drop down. 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 contexthub.segment-engine.scripts". So create a clientlibs under /app/ then add script name to js.txt. Now refresh the segment page and see if script name is displayed in the drop down.


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);

})();

No comments :

Post a Comment