Client Side Solutions - Cloak Or Not To Cloak

If you donโ€™t cloak your affiliate links, just add the We Can Track Tag and youโ€™re good to go. If you do cloak your URLs on the client-side, read the Cloaked Affiliate URLs section on how to solve those cases.

Cloaked Affiliate URLs

There are 2 ways of dealing with this. One way is to get the affiliate link from the API yourself, this is recommended if you have a separate redirect page with a loading message because getting the affiliate URL might take a couple seconds. You can find more information about this API here.

The other way is to use the _wct.getTrackingURL function that is in the WCT JS script, this function instantly returns a tracking URL. The tradeoff is that the tracking URL will redirect through We Can Track.

  • Insert the We Can Track Tag on the pages you want to track
  • Locate where in the Javascript you redirect the user to the affiliate URL.
  • Use this function to get a tracking URL _wct.getTrackingURL(AFFILIATE_URL, METADATA) and use that URL instead to redirect the user to. You can also send JSON metadata along in the second optional parameter.

Example code with fallback.

Scenario

Cloaked links in these examples are go/SomeProductID. These anchors have an onclick event on them, which fires a function called redirect(). The redirect function gets the href of the clicked object, uncloaks it and sets the window.location.href to the affiliate URL so that the user gets redirected to it.

<a href="go/123" onclick="redirect(event);">CTA</a>
JS Before
				
					    function redirect(event) {
      event.preventDefault();
      var href = event.currentTarget.getAttribute('href');

      // redirect user to Affiliate URL
      window.location.href = getAffiliateURL(cloakedUrl);
    }

    function getAffiliateURL(cloakedUrl) {
        // ..
        // your uncloaking process
        // ..

        return affiliateUrl;
    }
				
			
JS Adjusted for WCT
				
					    /**
     * Get We Can Track Tracking URL.
     * Fallback on Affiliate URL.
     */
    function getTrackingURL(affiliateUrl) {
        if (typeof _wct !== "undefined" && _wct.getTrackingURL !== undefined) {
            var trackingLink = _wct.getTrackingURL(affiliateUrl);
            if (trackingLink) return trackingLink;
        }
        return affiliateUrl;
    }

    function redirect(event) {
      event.preventDefault();
      var href = event.currentTarget.getAttribute('href');
      var affiliateUrl = getTrackingURL(getAffiliateURL(href));

      // redirect user to Affiliate URL
      window.location.href = getTrackingURL(affiliateUrl);
    }

    function getAffiliateURL(cloakedUrl) {
        // ..
        // your uncloaking process
        // ..

        return affiliateUrl;
    }