Download Shopper Information in SFRA

When you implement an SFRA-based storefront, consider providing your shoppers with a mechanism to download their data. The plugin_datadownload cartridge provides a sample implementation of this capability.

In the sample implementation, registered users see a Download My Data button on the My Account page. When a shopper clicks this button, SFRA downloads a JSON file to the shopper's browser. This file contains the following information:

  • Profile
  • Address
  • Payment instruments
  • Orders
  • Wish lists
  • Gift registries
  • Shopping lists

The JSON file illustrates the type of information to provide to your shoppers. Your business can provide different data in a different format. To use the sample implementation, upload the plugin_datadownload cartridge into your instance and add the cartridge to your cartridge path.

Sample JSON File

This sample file shows the type of information you can provide to the shopper.

{
  "profile": {
    "birthday": "1988-10-21T00:00:00.000Z",
    "companyName": "",
    "customerNo": "D00000001",
    "email": "[email protected]",
    "fax": "",
    "firstName": "Test1",
    "gender": "Female",
    "jobTitle": "",
    "lastLoginTime": "2018-02-14T20:07:31.074Z",
    "lastName": "Doe",
    "lastVisitTime": "2018-02-14T20:07:31.074Z",
    "phoneBusiness": "",
    "phoneHome": "",
    "phoneMobile": "",
    "preferredLocale": "",
    "previousLoginTime": "2015-05-18T20:43:17.000Z",
    "previousVisitTime": "2015-05-18T20:43:17.000Z",
    "salutation": "",
    "secondName": "",
    "suffix": "",
    "taxID": null,
    "taxIDMasked": null,
    "taxIDType": null,
    "title": "",
    "male": false,
    "female": true,
    "nextBirthday": "2018-10-21T00:00:00.000Z"
  },
  "addressbook": [
    {
      "address1": "104 Presidential Way",
      "address2": null,
      "city": "Woburn",
      "companyName": null,
      "countryCode": "us",
      "firstName": "Test1",
      "fullName": "Test1 User1",
      "id": "Home",
      "jobTitle": null,
      "lastName": "User1",
      "phone": "781-555-1212",
      "postalCode": "01801",
      "postBox": null,
      "salutation": null,
      "secondName": null,
      "stateCode": "MA",
      "suffix": null,
      "suite": null,
      "title": null
    },
    {
      "address1": "91 Middlesex Tpke",
      "address2": null,
      "city": "Burlington",
      "companyName": null,
      "countryCode": "us",
      "firstName": "Jane",
      "fullName": "Jane Doe",
      "id": "Work",
      "jobTitle": null,
      "lastName": "Doe",
      "phone": "781-555-1212",
      "postalCode": "01803",
      "postBox": null,
      "salutation": null,
      "secondName": null,
      "stateCode": "MA",
      "suffix": null,
      "suite": null,
      "title": null
    }
  ],
  "wallet": [],
  "orders": [],
  "productList": {
    "whishlists": [],
    "giftregistries": [],
    "shoppinglists": []
  },
  "thirdpartydata": {}
}

Implementation Details

The sample implementation provides a Download my data button in the accountDashboard.isml template.

<div class="row mb-3 clearfix hidden-sm-down">
	<div class="col">
		<div>
			<a href="${URLUtils.url('Account-DataDownload')}" class="btn btn-outline-primary pull-right"
			   role="button" aria-pressed="true">
				${Resource.msg('button.download.data', 'datadownload', null)}
			</a>
		</div>
	</div>
</div>

When the shopper clicks the button, the Account-DataDownload route is called. This route is implemented in Account.js.

'use strict';

var server = require('server');
server.extend(module.superModule);

var userLoggedIn = require('*/cartridge/scripts/middleware/userLoggedIn');

server.get('DataDownload',
    server.middleware.https,
    userLoggedIn.validateLoggedIn,
    function (req, res, next) {
        var customer = req.currentCustomer.raw;
        var dataDownloadHelper = require('~/cartridge/scripts/dataDownloadHelper');
        var site = require('dw/system/Site');

        var fileName = site.current.name
            + '_' + customer.profile.firstName
            + '_' + customer.profile.lastName
            + '.json';

        var profileData = dataDownloadHelper.getProfileData(customer);

        res.setHttpHeader(res.base.CONTENT_DISPOSITION, 'attachment; filename="' + fileName + '"');
        res.setContentType('application/octet-stream');
        res.print(profileData);

        next();
    }
);

module.exports = server.exports();

This route relies on the helper script dataDownloadHelper.js, which provides several helper functions, such as getWallet(profile), getWishlists(customer, ProductListMgr), and so on. The helper script exports getProfileData , which calls the helper functions, constructs the JSON string, and returns the result to the shopper's browser.


exports.getProfileData = function (profile) {
    var ProductListMgr = require('dw/customer/ProductListMgr');
    var downloadJSONObj = {};

    downloadJSONObj.profile = getProfile(profile);
    downloadJSONObj.addressbook = getAddressBook(profile);
    downloadJSONObj.wallet = getWallet(profile);
    downloadJSONObj.orders = getOrders(profile);
    downloadJSONObj.productList = {
        whishlists: getWishLists(ProductListMgr),
        giftregistries: getGiftregistries(ProductListMgr),
        shoppinglists: getShoppingLists(ProductListMgr)
    };

    downloadJSONObj.thirdpartydata = {};
    return JSON.stringify(downloadJSONObj, null, 2);
};