Sending signals to different endpoints based on condition

Hi,

I want to send signals to a different endpoint if a condition is met. I followed this and was able to achieve this but on the second signal the base64 encoded data in “cx” param is not going at all. Any suggestion ?

@ANOOP_LATH, how did you implement your contexts tracking? Can you share your code (with sensitive data masked if any)?

Here is the code snippet…hope this helps… I have created a new namespace called “cf1”

u.send = function(a, b) {
            if (u.ev[a] || u.ev.all !== undefined) {
                var c, d, e, f, i;
                u.data = {
                    "base_url": "",
                    "snowplow_object": "snowplow",
                    "customURL": "",
                    "referrerURL": "",
                    "cookieTimeOut": "",
                    "enableAT": "false",
                    "minimumVisitLength": 30,
                    "heartBeat": 10,
                    "enableET": "false",
                    "preservePageView": "false",
                    "trk_appId": "",
                    "trk_platform": "",
                    "trk_send": "",
                    "trk_namespace": "cf",
                    "trk_endpoint": "",
                    "trk_cookieDomain": "",
                    "trk_cookieName": "",
                    "trk_respectDoNotTrack": "",
                    "trk_userFingerprint": "",
                    "trk_userFingerprintSeed": 123412414,
                    "trk_pageUnloadTimer": 0,
                    "trk_forceSecure": "",
                    "trk_sendAsPost": "",
                    "trk_bufferSize": "",
                    "trk_maxPostBytes": "",
                    "trk_storageStrategy": "",
                    "trk_cookieLifetime": "",
                    "trk_contextVendor": "",
                    "enableLCT": "false",
                    "ltr_blacklist": "",
                    "ltr_whitelist": "",
                    "ltr_filter": "",
                    "ltr_pseudoClicks": "false",
                    "ltr_clickContent": "",
                    "ltr_url": "",
                    "ltr_elementId": "",
                    "ltr_elementClasses": "",
                    "ltr_elementTarget": "",
                    "ltr_elementClickContent": "",
                    "ltr_schema": "",
                    "ltr_contextObj": "",
                    "ltr_context": {},
                    "adtracking": "",
                    "ad_impressionId": "",
                    "ad_costModel": "",
                    "ad_cost": "",
                    "ad_targetUrl": "",
                    "ad_bannerId": "",
                    "ad_zoneId": "",
                    "ad_advertiserId": "",
                    "ad_campaignId": "",
                    "ad_clickId": "",
                    "ad_conversionId": "",
                    "ad_category": "",
                    "ad_action": "",
                    "ad_property": "",
                    "ad_initialValue": "",
                    "ad_schema": "",
                    "ad_context": {},
                    "ad_contextObj": "",
                    "cc_schema": "",
                    "cc_context": {},
                    "cc_contextObj": "",
                    "sot_action": "",
                    "sot_network": "",
                    "sot_target": "",
                    "sot_schema": "",
                    "sot_context": {},
                    "sot_contextObj": "",
                    "enableFormTracking": false,
                    "cartTracking": false,
                    "cart_sku": "",
                    "cart_name": "",
                    "cart_category": "",
                    "cart_price": "",
                    "cart_quantity": "",
                    "cart_currency": "",
                    "cart_schema": "",
                    "cart_context": {},
                    "cart_contextObj": "",
                    "sst_terms": "",
                    "sst_filters": {},
                    "sst_totalResults": "",
                    "sst_pageResults": "",
                    "sst_schema": "",
                    "sst_context": {},
                    "sst_contextObj": "",
                    "order_id": "",
                    "order_total": "",
                    "order_shipping": "",
                    "order_tax": "",
                    "order_store": "",
                    "order_currency": "",
                    "customer_id": "",
                    "customer_city": "",
                    "customer_state": "",
                    "customer_country": "",
                    "product_name": [],
                    "product_sku": [],
                    "product_category": [],
                    "product_quantity": [],
                    "product_unit_price": [],
                    "ecm_schema": "",
                    "ecm_context": {},
                    "ecm_contextObj": "",
                    "stctCat": "",
                    "stctActn": "",
                    "stctLabel": "",
                    "stctProp": "",
                    "stctVal": "",
                    "stc_schema": "",
                    "stc_context": {},
                    "stc_contextObj": "",
                    "unstctDataSchema": "",
                    "unstctData": {},
                    "unstctDataObj": {},
                    "unstc_schema": "",
                    "unstc_context": {},
                    "unstc_contextObj": ""
                };
                for (c = 0; c < u.extend.length; c++) {
                    try {
                        d = u.extend[c](a, b);
                        if (d == false)
                            return
                    } catch (e) {}
                }
                ;for (d in utag.loader.GV(u.map)) {
                    if (b[d] !== undefined && b[d] !== "") {
                        e = u.map[d].split(",");
                        for (f = 0; f < e.length; f++) {
                            u.map_func(e[f].split("."), u.data, b[d]);
                        }
                    }
                }
                u.data.order_id = u.data.order_id || b._corder || "";
                u.data.order_total = u.data.order_total || b._ctotal || "";
                u.data.order_shipping = u.data.order_shipping || b._cship || "";
                u.data.order_tax = u.data.order_tax || b._ctax || "";
                u.data.order_store = u.data.order_store || b._cstore || "";
                u.data.order_currency = u.data.order_currency || b._ccurrency || "";
                u.data.customer_city = u.data.customer_city || b._ccity || "";
                u.data.customer_state = u.data.customer_state || b._cstate || "";
                u.data.customer_country = u.data.customer_country || b._ccountry || "";
                if (u.data.product_name.length === 0 && b._cprodname !== undefined) {
                    u.data.product_name = b._cprodname.slice(0);
                }
                if (u.data.product_sku.length === 0 && b._csku !== undefined) {
                    u.data.product_sku = b._csku.slice(0);
                }
                if (u.data.product_category.length === 0 && b._ccat !== undefined) {
                    u.data.product_category = b._ccat.slice(0);
                }
                if (u.data.product_quantity.length === 0 && b._cquan !== undefined) {
                    u.data.product_quantity = b._cquan.slice(0);
                }
                if (u.data.product_unit_price.length === 0 && b._cprice !== undefined) {
                    u.data.product_unit_price = b._cprice.slice(0);
                }
                u.loader_cb = function() {
                    var sendToTracker = "";
                    if (typeof u.data.trk_send === "string" && u.data.trk_send.length > 0) {
                        u.data.trk_send = [u.data.trk_send];
                    }
                    if (u.data.trk_send.length > 0) {
                        sendToTracker = ":" + u.data.trk_send.join(';');
                    }
                    if (!u.trackers[u.data.trk_namespace]) {
                        u.trackers[u.data.trk_namespace] = 1;
                        var config = {};
                        if (u.data.trk_appId) {
                            config.appId = u.data.trk_appId;
                        }
                        if (u.data.trk_platform) {
                            config.platform = u.data.trk_platform;
                        }
                        if (u.data.trk_cookieName) {
                            config.cookieName = u.data.trk_cookieName;
                        }
                        if (u.data.trk_cookieDomain) {
                            config.cookieDomain = u.data.trk_cookieDomain;
                        }
                        if (u.data.trk_encodeBase64) {
                            config.encodeBase64 = u.data.trk_encodeBase64;
                        }
                        if (u.data.trk_respectDoNotTrack) {
                            config.respectDoNotTrack = (u.data.trk_respectDoNotTrack === "true" || u.data.trk_respectDoNotTrack === true);
                        }
                        if (u.data.trk_userFingerprint) {
                            config.userFingerprint = u.data.trk_userFingerprint;
                            if (u.data.trk_userFingerprintSeed) {
                                config.userFingerprintSeed = parseInt(u.data.trk_userFingerprintSeed);
                            }
                        }
                        if (u.data.trk_pageUnloadTimer) {
                            config.pageUnloadTimer = parseInt(u.data.trk_pageUnloadTimer);
                        }
                        if (u.data.trk_forceSecure) {
                            config.forceSecureTracker = (u.data.trk_forceSecure === "true" || u.data.trk_forceSecure === true);
                        }
                        if (u.data.trk_sendAsPost) {
                            config.post = (u.data.trk_sendAsPost === "true" || u.data.trk_sendAsPost === true);
                        }
                        if (u.data.trk_bufferSize) {
                            config.bufferSize = parseInt(u.data.trk_bufferSize);
                        }
                        if (u.data.trk_maxPostBytes) {
                            config.maxPostBytes = parseInt(u.data.trk_maxPostBytes);
                        }
                        if (u.data.cookieTimeOut) {
                            config.sessionCookieTimeout = parseInt(u.data.cookieTimeOut);
                        }
                        if (u.data.trk_storageStrategy) {
                            config.stateStorageStrategy = u.data.trk_storageStrategy;
                        }
                        if (u.data.trk_cookieLifetime) {
                            config.cookieLifetime = parseInt(u.data.cookieLifetime);
                        }
                        if (u.data.trk_contextVendor) {
                            config.contexts = u.createVendorObject(u.data.trk_contextVendor);
                        }
                        if (u.data.trk_endpoint == '') {
                            u.data.trk_endpoint = b.snowplow_endpoint;
                        }
                        if (u.data.trk_endpoint_recom == '') {
                            u.data.trk_endpoint_recom = b.snowplow_recom_endpoint;
                        }
                        window[u.data.snowplow_object]('newTracker', u.data.trk_namespace, u.data.trk_endpoint, config);
                        window[u.data.snowplow_object]('newTracker', 'cf1', u.data.trk_endpoint_recom, config);
                        if (u.data.enableAT === "true" || u.data.enableAT === true) {
                            window[u.data.snowplow_object]('enableActivityTracking', parseInt(u.data.minimumVisitLength), parseInt(u.data.heartBeat));
                        }
                        if (u.data.enableET === "true" || u.data.enableET === true) {
                            window[u.data.snowplow_object]('enableErrorTracking');
                        }
                        if (u.data.preservePageView === "true" || u.data.preservePageView === true) {
                            window[u.data.snowplow_object]('preservePageViewId');
                        }
                    }
                    if (u.data.enableLCT === "true" || u.data.enableLCT === true) {
                        var config = {};
                        if (u.data.ltr_blacklist) {
                            config.blacklist = u.data.ltr_blacklist;
                        }
                        if (u.data.ltr_whitelist) {
                            config.whitelist = u.data.ltr_whitelist;
                        }
                        if (u.data.ltr_filter) {
                            if (typeof u.data.ltr_filter === "string") {
                                config.filter = window[u.data.ltr_filter];
                            } else {
                                config.filter = u.data.ltr_filter;
                            }
                        }
                        window[u.data.snowplow_object]('enableLinkClickTracking', config, (u.data.ltr_pseudoClicks === "true" || u.data.ltr_pseudoClicks === true), u.data.ltr_clickContent, u.createContext(u.data.ltr_contextObj, u.data.ltr_schema, u.data.ltr_context));
                        window[u.data.snowplow_object]('refreshLinkClickTracking');
                    }
                    u.data.customer_id = u.data.customer_id || b._ccustid || "";
                    if (u.data.customer_id) {
                        window[u.data.snowplow_object]('setUserId', u.data.customer_id);
                    }
                    if (u.data.customURL) {
                        window[u.data.snowplow_object]('setCustomUrl', u.data.customURL);
                    }
                    if (u.data.referrerURL) {
                        window[u.data.snowplow_object]('setReferrerUrl', u.data.referrerURL);
                    }
                    if ('view' === a || u.data.cc_contextObj) {
                        if (typeof b.call_to_actiontype != 'undefined' && b.call_to_actiontype == 'certona product click') {
                            window[u.data.snowplow_object]('trackPageView:cf1', u.data.pageTitle, u.createContext(u.data.cc_contextObj, u.data.cc_schema, u.data.cc_context));
                        } else {
                            window[u.data.snowplow_object]('trackPageView:cf', u.data.pageTitle, u.createContext(u.data.cc_contextObj, u.data.cc_schema, u.data.cc_context));
                        }
                        u.data.cc_contextObj = {}
                        if (u.data.adtracking === "impression") {
                            window[u.data.snowplow_object]('trackAdImpression' + sendToTracker, u.data.ad_impressionId, u.data.ad_costModel, parseFloat(u.data.ad_cost), u.data.ad_targetUrl, u.data.ad_bannerId, u.data.ad_zoneId, u.data.ad_advertiserId, u.data.ad_campaignId, u.createContext(u.data.ad_contextObj, u.data.ad_schema, u.data.ad_context));
                        } else if (u.data.adtracking === "conversion") {
                            window[u.data.snowplow_object]('trackAdConversion' + sendToTracker, u.data.ad_conversionId, u.data.ad_costModel, parseFloat(u.data.ad_cost), u.data.ad_category, u.data.ad_action, u.data.ad_property, parseFloat(u.data.ad_initialValue), u.data.ad_advertiserId, u.data.ad_campaignId, u.createContext(u.data.ad_contextObj, u.data.ad_schema, u.data.ad_context));
                        }
                    } else if ('link' === a) {
                        if (u.data.adtracking === "click") {
                            window[u.data.snowplow_object]('trackAdClick' + sendToTracker, u.data.ad_targetUrl, u.data.ad_clickId, u.data.ad_costModel, parseFloat(u.data.ad_cost), u.data.ad_bannerId, u.data.ad_zoneId, u.data.ad_impressionId, u.data.ad_advertiserId, u.data.ad_campaignId, u.createContext(u.data.ad_contextObj, u.data.ad_schema, u.data.ad_context));
                        }
                        if (u.data.ltr_url) {
                            window[u.data.snowplow_object]('trackLinkClick' + sendToTracker, u.data.ltr_url, u.data.ltr_elementId, u.data.ltr_elementClasses, u.data.ltr_elementTarget, u.data.ltr_elementClickContent, u.createContext(u.data.ltr_contextObj, u.data.ltr_schema, u.data.ltr_context));
                        }
                    }
                    if (u.data.unstctDataSchema || !u.isEmpty(u.data.unstctDataObj)) {
                        window[u.data.snowplow_object]('trackUnstructEvent', u.createContext(u.data.unstctDataObj, u.data.unstctDataSchema, u.data.unstctData), u.createContext(u.data.unstc_contextObj, u.data.unstc_schema, u.data.unstc_context));
                    }
                    if (u.data.stctActn) {
                        window[u.data.snowplow_object]('trackStructEvent', u.data.stctCat, u.data.stctActn, u.data.stctLabel, u.data.stctProp, u.data.stctVal, u.createContext(u.data.stc_contextObj, u.data.stc_schema, u.data.stc_context));
                    }
                    if (u.data.enableFormTracking === "true" || u.data.enableFormTracking === true) {
                        var config = {};
                        if (u.data.ftr_ctmConfig) {
                            config = u.data.ftr_ctmConfig;
                        }
                        if (u.data.ftr_formBlacklist || u.data.ftr_formWhitelist || u.data.ftr_filterForm) {
                            config.forms = {};
                        }
                        if (u.data.ftr_fieldBlacklist || u.data.ftr_fieldWhitelist || u.data.ftr_filterField) {
                            config.fields = {};
                        }
                        if (u.data.ftr_formBlacklist) {
                            config.forms.blacklist = u.data.ftr_formBlacklist;
                        }
                        if (u.data.ftr_formWhitelist) {
                            config.forms.whitelist = u.data.ftr_formWhitelist;
                        }
                        if (u.data.ftr_fieldBlacklist) {
                            config.fields.blacklist = u.data.ftr_fieldBlacklist;
                        }
                        if (u.data.ftr_fieldWhitelist) {
                            config.fields.whitelist = u.data.ftr_fieldWhitelist;
                        }
                        if (u.data.ftr_filterForm) {
                            if (typeof u.data.ftr_filterForm === "string") {
                                config.forms.filter = window[u.data.ftr_filterForm];
                            } else {
                                config.forms.filter = u.data.ftr_filterForm;
                            }
                        }
                        if (u.data.ftr_filterField) {
                            if (typeof u.data.ftr_filterField === "string") {
                                config.fields.filter = window[u.data.ftr_filterField];
                            } else {
                                config.fields.filter = u.data.ftr_filterField;
                            }
                        }
                        window[u.data.snowplow_object]('enableFormTracking', config);
                    }
                    if (u.data.sot_action) {
                        window[u.data.snowplow_object]('trackSocialInteraction', u.data.sot_action, u.data.sot_network, u.data.sot_target, u.createContext(u.data.sot_contextObj, u.data.sot_schema, u.data.sot_context));
                    }
                    if (u.data.sst_terms) {
                        window[u.data.snowplow_object]('trackSiteSearch', u.data.sst_terms, u.data.sst_filters, u.data.sst_totalResults, u.data.sst_pageResults, u.createContext(u.data.sst_contextObj, u.data.sst_schema, u.data.sst_context));
                    }
                    if (u.data.cartTracking === "cart_add") {
                        window[u.data.snowplow_object]('trackAddToCart', u.data.cart_sku, u.data.cart_name, u.data.cart_category, u.data.cart_price, u.data.cart_quantity, u.data.cart_currency, u.createContext(u.data.cart_contextObj, u.data.cart_schema, u.data.cart_context));
                    }
                    if (u.data.cartTracking === "cart_remove") {
                        window[u.data.snowplow_object]('trackRemoveFromCart', u.data.cart_sku, u.data.cart_name, u.data.cart_category, u.data.cart_price, u.data.cart_quantity, u.data.cart_currency, u.createContext(u.data.cart_contextObj, u.data.cart_schema, u.data.cart_context));
                    }
                }
                ;
                if (!u.initialised) {
                    u.initialised = true;
                    window.GlobalSnowplowNamespace.push(u.data.snowplow_object);
                    window[u.data.snowplow_object] = function() {
                        (window[u.data.snowplow_object].q = window[u.data.snowplow_object].q || []).push(arguments)
                    }
                    ;
                    window[u.data.snowplow_object].q = window[u.data.snowplow_object].q || [];
                    u.loader({
                        "type": "script",
                        "src": u.data.base_url,
                        "cb": u.loader_cb,
                        "loc": "script",
                        "id": 'utag_15'
                    });
                } else {
                    u.loader_cb();
                }
            }

Hey @ANOOP_LATH - I think there’s a simpler way to go about it.

Instead of wrapping tracker creation and event construction in conditions, just define two trackers, each with a different endpoint. Then, you just wrap your track statements in conditions.

Here’s an example of multiple trackers per page, and here’s an example of specifying which tracker should send an event. If you don’t specify a tracker namespace in the track method, both trackers will send the event.

1 Like

I have done that only. And 2 requests are being triggered successfully. But my problem is that in cf1 namespace the “cx” parameter is not going. Also, when I check the localstorage I see that the item with cx namespace is blank while the cx1 param is not blank after the request is sent.

I also found out that it is not sending the cx param only on first request…after navigating to another page it is sending the cx param properly. Any thoughts?

@ANOOP_LATH, it looks like you are overcomplicating things. It fact it looks like you are inventing your own Javascript tracker by manually setting various properties and hacking into the code (well, it’s hard to follow what you actually trying to do).

Here’s a simple example of how it’s meant to be used based on the wiki here https://github.com/snowplow/snowplow/wiki/1-General-parameters-for-the-Javascript-tracker#25-managing-multiple-trackers but with the addition of contexts to the page view event.

snowplow("newTracker", "cf1", <COLLECTOR_1>, {
  appId: "my_app"
});

snowplow("newTracker", "cf2", <COLLECTOR_2>, {
  appId: "my_app"
});

window.snowplow('enableActivityTracking', 10, 10);
window.snowplow(
  'trackPageView:cf1', null,

  // Context 1
  [{
    schema: 'iglu:com.acme/static_context/jsonschema/1-0-0',
    data: {
      data1: "here"
    }
  }]
);
window.snowplow(
  'trackPageView:cf2', null,

  // Context 2
  [{
    schema: 'iglu:com.acme/static_context/jsonschema/1-0-0',
    data: {
      data1: "there"
    }
  }]
);

In this example, the pageview event is sent to 2 different collectors (endpoints). One with a context called static_context and value “here” sent to cf1 and the other is sent to the second endpoint, cf2, but with the value “there”.

1 Like