BranchMetrics / web-branch-deep-linking-attribution

The Branch Web SDK for deep linking and attribution. Once initialized, the Branch Web SDK allows you to create and share links with a banner (web only), over SMS, or your own methods by generating deep links. It also offers event tracking, access to referrals, and management of credits.
https://help.branch.io/developers-hub/docs/web-sdk-overview
MIT License
288 stars 101 forks source link

[INTENG-10487] API logging callback #745

Closed jdee closed 4 years ago

jdee commented 4 years ago

This allows a user to set a global callback function for all API requests, once each response is received, including any errors, such as timeouts. This records each attempt in the case of retries.

branch.setAPIResponseCallback(function(url, method, requestBody, err, status, responseBody) {
    console.log(url + ' ' + method + ' ' + requestBody + ' ' + err + ' ' + status + ' ' + responseBody);
});

Using this function right away requires adding setAPIResponseCallback to the JS snippet that imports the SDK in order to avoid a race asynchronously loading the SDK. See changes to onpage.js.

With some requests I get null for the status argument. Still looking into that. For jsonp requests (as opposed to XHR requests), the request is made via an async script tag. For these endpoints (/_r and /v1/deepview), no status code is available, so that argument is always undefined (not null, as originally indicated).

Also working on unit tests. May not get this finished today.

You can immediately test this locally:

  1. Run make to rebuild the dist folder.
  2. Edit example.html and add the call above to branch.setAPIResponseCallback before branch.init.
  3. In example.html, also add setAPIResponseCallback to the list of functions:
(function(b,r,a,n,c,h,_,s,d,k){if(!b[n]||!b[n]._q){for(;s<_.length;)c(h,_[s++]);d=r.createElement(a);d.async=1;d.src="dist/build.min.js";k=r.getElementsByTagName(a)[0];k.parentNode.insertBefore(d,k);b[n]=h}})(window,document,"script","branch",function(b,r){b[r]=function(){b._q.push([r,arguments])}},{_q:[],_v:1},"addListener applyCode autoAppIndex banner closeBanner closeJourney creditHistory credits data deepview deepviewCta first getCode init link logout redeem referrals removeListener sendSMS setBranchViewData setIdentity track validateCode trackCommerceEvent logEvent disableTracking getBrowserFingerprintId crossPlatformIds lastAttributedTouchData setAPIResponseCallback".split(" "), 0);
  1. Open example.html and view the Console tab.
jwangler-branch commented 4 years ago

Looks good to me. Awesome 👍