How to append a script to body tag of all pages in a website and pass values dynamically into the script using extension

This article refers to adding a script with dynamic values inside it.

Here is an example:

Adding a script for ROI tracking with dynamic values.

Sample script structure:

<script type="text/javascript"> 

_dmTrack("product", "Product name 1"); 

_dmTrack("product", "Product name 2"); 

_dmTrack("product", "Product name 3"); 

_dmTrack("CheckOutAmount", "250"); 

</script> 

Here in the script we need to pass two values product name and total checkout amount of items in the cart, so the number of lines in the script script changes according to the number of items.

So for adding the script dynamically to the body tag of web page we can use the following code.

Extension code:

define('ROItracking',
    [
        'underscore'
        , 'jQuery'
        , 'Backbone'
        , 'SC.Configuration'
        , 'Profile.Model'
        , 'LiveOrder.Model'
    ], function (
        _
        , jQuery
        , Backbone
        , Configuration
        , ProfileModel
        , LiveOrderModel
    ) {
        'use strict';

        return {

            mountToApp: function (container) {
                //For Dot digital ROI tracking script to add in checkout pages pages of the website.
                var layout = container.getComponent('Layout');
                var profile = ProfileModel.getInstance();
                var IsloggedIN = profile.attributes.isLoggedIn;
                console.log("profile.attributes.isLoggedIn",IsloggedIN)

                if(IsloggedIN === 'T'){
                    var live = LiveOrderModel.getInstance();
                    console.log("Live", live);
                    var item2 = live.attributes.lines.length
                    console.log("item2",item2)
                    console.log("profile.attributes.isLoggedIn",profile.attributes.isLoggedIn)
                    var chekout_total= live.attributes.summary.total
                    console.log("chekout_total",chekout_total)
                    var modelArr = live.attributes.lines.models
                    var url =""
                    if (item2 > 0) {
                        //var itemdetails = [];
                        console.log("item true", item2);
                        _.each(modelArr, function (each) {
                            console.log("each",each)
                            var itemname = each.attributes.internalid
                            var product_name = each.attributes.item.attributes.storedisplayname2
                            //url +=  "_dmTrack(" + "\"product\"," + "\""+itemname +"\""+");";
                            url +=  "_dmTrack(" + "\""+"product" +"\", " + "\""+product_name +"\""+");";
                        });
                    }
                    jQuery('body').append(jQuery("<script src=\"https://r1-t.trackedlink.net/_dmpt.js\" type=\"text/javascript\">"+url+"_dmTrack(" + "\"CheckOutAmount\"," +"\""+chekout_total+"\""+ ");" + "</script>"));

                }
                else{
                    console.log("Not Logged In");
                    console.log("profile.attributes.isLoggedIn",IsloggedIN);
                }
            },

        }

    });

Leave a comment

Your email address will not be published. Required fields are marked *