{"_id":"56b548b75f1cf00d00cc4773","parentDoc":null,"user":"56839cf74aecbd0d00a4659e","category":{"_id":"569742bd0b09a41900b2446c","__v":9,"pages":["5697455824490c3700170a69","569747128400d52d00dd56a4","5697476b8560a60d00e2c262","569747a4c8ded91700307b92","5697489c59a6692d003fad96","56a1500244f3d80d00a2c388","56a2c51906150b0d002ad264","56b548b75f1cf00d00cc4773","56b54ae37719bb1900143093"],"project":"5668fab608f90021008e882f","version":"5668fab608f90021008e8832","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-01-14T06:39:57.496Z","from_sync":false,"order":6,"slug":"tuning-liftigniter-javascript","title":"Rendering,Tracking and A/B Testing Widgets (JavaScript)"},"githubsync":"","version":{"_id":"5668fab608f90021008e8832","__v":19,"project":"5668fab608f90021008e882f","createdAt":"2015-12-10T04:08:22.769Z","releaseDate":"2015-12-10T04:08:22.769Z","categories":["5668fab708f90021008e8833","569740f124490c3700170a64","569742b58560a60d00e2c25d","569742bd0b09a41900b2446c","569742cd69393517000c82b3","569742f459a6692d003fad8f","569743020b09a41900b2446d","5697430b69393517000c82b5","56a17776470ae00d00c30642","56a2c48a831e2a0d0069b1ad","56b535757bccae0d00e9a1cd","56e1ff6aa49fdc0e005746b5","57e1c88115bf6522002a5e4e","57fa65275ba65a17008b988f","57fbeea34002550e004c032e","58474584889b6c2d00fb86e9","58475dcc64157f0f002f1907","587e7b5158666c2700965d4e","58a349fc30852819007ba083"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.18.0","version":"1.18"},"project":"5668fab608f90021008e882f","__v":4,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-02-06T01:13:27.822Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":8,"body":"Overwriting an existing widget with our recommendations is useful in the following contexts:\n<ul>\n<li>You want to rewrite a subset of the recommendations in a widget area that you have already designed.</li>\n<li>You want to run an A/B test against your own internal recommendations.</li>\n<li>You want to maintain a fallback set of recommendations to show if the user is unable to fetch our recommendations (due to browser issues on the user’s side, network connectivity issues, or server timeouts on our side).</li>\n</ul>\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"1. Illustration: rewriting some items with recommendations from one widget\"\n}\n[/block]\nAssume you already have the following recommendation area on your page:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<div id=\\\"li-recommendation-unit\\\">\\n  <div class='recommended_item'>\\n    <a class='headline' href='//url-1'>Title 1</a>\\n  </div>\\n\\n  <div class='recommended_item'>\\n    <a class='headline' href='//url-2'>Title 1</a>\\n  </div>\\n\\n  <div class='recommended_item'>\\n    <a class='headline' href='//url-3'>Title 3</a>\\n  </div>\\n\\n  <div class='recommended_item'>\\n    <a class='headline' href='//url-4'>Title 4</a>\\n  </div>\\n</div>\",\n      \"language\": \"html\"\n    }\n  ]\n}\n[/block]\nSuppose you want to overwrite the 2nd and 4th `recommended_item` with LiftIgniter recommendations. You can do that as follows:\n\n***Step 1*** Tag the items you want to replace (e.g. by adding `li-widget-item` to the class):\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<div id=\\\"li-recommendation-unit\\\">\\n  <div class='recommended_item'>\\n    <a class='headline' href='//url-1'>Title 1</a>\\n  </div>\\n\\n  <div class='recommended_item li-widget-item'>\\n    <a class='headline' href='//url-2'>Title 1</a>\\n  </div>\\n\\n  <div class='recommended_item'>\\n    <a class='headline' href='//url-3'>Title 3</a>\\n  </div>\\n\\n  <div class='recommended_item li-widget-item'>\\n    <a class='headline' href='//url-4'>Title 4</a>\\n  </div>\\n</div>\",\n      \"language\": \"html\"\n    }\n  ]\n}\n[/block]\n***Step 2*** Add a mustache template matching the tagged units to your page:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<script type=\\\"application/mustache\\\" id=\\\"recommended-item-template\\\">\\n  <div class='recommended_item'>\\n    <a class='headline' href='{{url}}'>{{title}}</a>\\n  </div>\\n</script>\",\n      \"language\": \"html\"\n    }\n  ]\n}\n[/block]\n***Step 3*** Render recommendations with appropriate callback\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Renders the recommendations and overwrites area marked with 'li-widget-item'.\\nvar overwrite_callback = function(resp) {\\n    var els = document.querySelectorAll(\\n      '#li-recommendation-unit > div.li-widget-item');\\n    var template = document.querySelector(\\n      '#recommended-item-template').innerHTML;\\n    for (var i = 0;\\n         i < els.length && i < resp.items.length;\\n         ++i) {\\n         els[i].innerHTML = $p('render', template, resp.items[i]);\\n         // Basically Mustache.render(template, resp.items[i]);\\n    }\\n   $p('track', {\\n       elements: document.querySelectorAll('#li-recommendation-unit > div.li-widget-item'\\n), // track LI recommendations\\n       name: 'default-widget',\\n       source: 'LI',\\n       opts: {}\\t\\t // Optional parameters to track with each event.\\n                         // If the corresponding 'register' call had optional\\n                         // fields, exactly those fields MUST be present here.\\n\\t\\t                }\\n\\t\\t       );\\n\\n}\\n// Register call to fetch recommendations.\\n// Apply the overwrite callback.\\n$p('register', {\\n                  max: 2,\\n                  widget: 'default-widget',\\n                  callback: overwrite_callback // You may wish to wrap this inside jQuery or another listener to make sure that the widget you intend to overwrite has actually loaded\\n               }\\n);\\n// Executes the registered call.\\n$p('fetch');\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"danger\",\n  \"title\": \"Load order issues\",\n  \"body\": \"Keep in mind that if your recommendations that you intend to overwrite have not loaded when the callback is executed, LiftIgniter's recommendations will not be able to replace them. You can remedy the problem by wrapping the execution inside a jQuery. For more, see our documentation on [load order](doc:load-order).\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"You can populate an area of your site with recommendations mixed and matched from different widget calls.\",\n  \"title\": \"Rewrite items with recommendations from multiple widget calls\"\n}\n[/block]","excerpt":"","slug":"overwriting-widgets","type":"basic","title":"Overwriting Widgets"}

Overwriting Widgets


Overwriting an existing widget with our recommendations is useful in the following contexts: <ul> <li>You want to rewrite a subset of the recommendations in a widget area that you have already designed.</li> <li>You want to run an A/B test against your own internal recommendations.</li> <li>You want to maintain a fallback set of recommendations to show if the user is unable to fetch our recommendations (due to browser issues on the user’s side, network connectivity issues, or server timeouts on our side).</li> </ul> [block:api-header] { "type": "basic", "title": "1. Illustration: rewriting some items with recommendations from one widget" } [/block] Assume you already have the following recommendation area on your page: [block:code] { "codes": [ { "code": "<div id=\"li-recommendation-unit\">\n <div class='recommended_item'>\n <a class='headline' href='//url-1'>Title 1</a>\n </div>\n\n <div class='recommended_item'>\n <a class='headline' href='//url-2'>Title 1</a>\n </div>\n\n <div class='recommended_item'>\n <a class='headline' href='//url-3'>Title 3</a>\n </div>\n\n <div class='recommended_item'>\n <a class='headline' href='//url-4'>Title 4</a>\n </div>\n</div>", "language": "html" } ] } [/block] Suppose you want to overwrite the 2nd and 4th `recommended_item` with LiftIgniter recommendations. You can do that as follows: ***Step 1*** Tag the items you want to replace (e.g. by adding `li-widget-item` to the class): [block:code] { "codes": [ { "code": "<div id=\"li-recommendation-unit\">\n <div class='recommended_item'>\n <a class='headline' href='//url-1'>Title 1</a>\n </div>\n\n <div class='recommended_item li-widget-item'>\n <a class='headline' href='//url-2'>Title 1</a>\n </div>\n\n <div class='recommended_item'>\n <a class='headline' href='//url-3'>Title 3</a>\n </div>\n\n <div class='recommended_item li-widget-item'>\n <a class='headline' href='//url-4'>Title 4</a>\n </div>\n</div>", "language": "html" } ] } [/block] ***Step 2*** Add a mustache template matching the tagged units to your page: [block:code] { "codes": [ { "code": "<script type=\"application/mustache\" id=\"recommended-item-template\">\n <div class='recommended_item'>\n <a class='headline' href='{{url}}'>{{title}}</a>\n </div>\n</script>", "language": "html" } ] } [/block] ***Step 3*** Render recommendations with appropriate callback [block:code] { "codes": [ { "code": "// Renders the recommendations and overwrites area marked with 'li-widget-item'.\nvar overwrite_callback = function(resp) {\n var els = document.querySelectorAll(\n '#li-recommendation-unit > div.li-widget-item');\n var template = document.querySelector(\n '#recommended-item-template').innerHTML;\n for (var i = 0;\n i < els.length && i < resp.items.length;\n ++i) {\n els[i].innerHTML = $p('render', template, resp.items[i]);\n // Basically Mustache.render(template, resp.items[i]);\n }\n $p('track', {\n elements: document.querySelectorAll('#li-recommendation-unit > div.li-widget-item'\n), // track LI recommendations\n name: 'default-widget',\n source: 'LI',\n opts: {}\t\t // Optional parameters to track with each event.\n // If the corresponding 'register' call had optional\n // fields, exactly those fields MUST be present here.\n\t\t }\n\t\t );\n\n}\n// Register call to fetch recommendations.\n// Apply the overwrite callback.\n$p('register', {\n max: 2,\n widget: 'default-widget',\n callback: overwrite_callback // You may wish to wrap this inside jQuery or another listener to make sure that the widget you intend to overwrite has actually loaded\n }\n);\n// Executes the registered call.\n$p('fetch');", "language": "javascript" } ] } [/block] [block:callout] { "type": "danger", "title": "Load order issues", "body": "Keep in mind that if your recommendations that you intend to overwrite have not loaded when the callback is executed, LiftIgniter's recommendations will not be able to replace them. You can remedy the problem by wrapping the execution inside a jQuery. For more, see our documentation on [load order](doc:load-order)." } [/block] [block:callout] { "type": "info", "body": "You can populate an area of your site with recommendations mixed and matched from different widget calls.", "title": "Rewrite items with recommendations from multiple widget calls" } [/block]