Skip to main content

Upgrade your SPA module to version 2

SPA module v2 removes the changes of Visual Editor experiences when the route changes. The original SPA module leaves them on the page.

This page lists the scripts and the lines that change from the original SPA module. The full scripts are in the Single Page Application (SPA) Guide.

Before you start

  • Library 7.0. Run WT.optimizeModule.prototype.wtConfigObj.libUrl in the browser console. The URL must contain 7.0. With an older library, no changes are removed.

  • A copy of your scripts. Keep the scripts that you have now. You need them if you go back.

  • No time for this? Ask your Account Manager or the Service Desk to update the tag for you.

Scripts that change

Script

Where it goes

Change

Script 1: module

Global Tag Script

4 changes

Script 2: route change detection

Global Tag Script, after Script 1

Replace in full

Script 3

Tag Trigger Script

1 new line

If your scripts have no changes of your own, replace all three in full. This is the safest way. Copy them from the Single Page Application (SPA) Guide.

Script 1: module

Change 1. Add these lines at the top of the script.

WT.optimizeModule.prototype.wtConfigObj.spaTeardown = true;WT.spa = WT.spa || {};
WT.spa.version = '2.0.0';WT.spa.teardown = function (filter) {
    try {
        if (WT.optimize && typeof WT.optimize.teardown === 'function') {
            return WT.optimize.teardown(filter);
        }
    } catch (err) {
        if (document.cookie.match(/_wt.bdebug=true/i)) console.log('WTO SPA teardown failed', err);
    }
    return null;
};

Change 2. In WT.refreshAndExecute, replace the page hide lines at the start of the function. Remove these lines:

WT.hideAndShow(document.body, WT.optimizeModule.prototype.wtConfigObj.s_pageDisplayMode, false, WT.optimizeModule.prototype.wtConfigObj.overlayColor);var PDM = WT.optimizeModule.prototype.wtConfigObj.s_pageDisplayMode;
if(["display", "visibility"].includes(PDM)){
    WT.hideAndShow(document.body, PDM, false, null);
} else if(["shift", "overlay"].includes(PDM)){
    WT.hideAndShow("body", PDM, false, null);
}

Put these lines in their place:

var PDM = WT.optimizeModule.prototype.wtConfigObj.s_pageDisplayMode;
var OVERLAY = WT.optimizeModule.prototype.wtConfigObj.overlayColor;WT.hideAndShow(document.body, PDM, false, OVERLAY);
if(["shift", "overlay"].includes(PDM)){
    WT.hideAndShow("body", PDM, false, null);
}WT.spa.teardown();

Change 3. Add these lines directly after WT.optimize._ctor();. If your script already has the two s_otsServer lines, do not add them again.

WT.optimize.g_ConfigParams.s_otsServer = "pat.webtrends-optimize.com";
WT.optimizeModule.prototype.wtConfigObj.s_otsServer = "pat.webtrends-optimize.com";
WT.optimize.g_ConfigParams.s_pageDisplayMode = PDM;
WT.optimize.g_ConfigParams.overlayColor = OVERLAY;if (WT.optimize.applied && !WT.optimize.applied.enabled) WT.optimize.applied.enable();

Change 4. Replace the last line of the function, WT.execute(WT.optimizeModule.prototype.wtConfigObj);, with these lines. They make sure that the page always shows.

var wtoRevealPage = function(){
    WT.hideAndShow(document.body, PDM, true, OVERLAY);
    var n; while ((n = document.getElementById('wt_StyleSheet')) && n.parentNode) { n.parentNode.removeChild(n); }
};
WT.addEventHandler('done', wtoRevealPage, wtoRevealPage);WT.execute(WT.optimizeModule.prototype.wtConfigObj);if (WT.spa._revealTimer) { clearTimeout(WT.spa._revealTimer); }
WT.spa._revealTimer = setTimeout(wtoRevealPage, (parseInt(WT.optimizeModule.prototype.wtConfigObj.s_pageTimeout, 10) || 3000) + 250);

Script 2: route change detection

Replace this script in full. Copy it from the Single Page Application (SPA) Guide. What is new:

  • Browser history. The script listens to pushState, replaceState, popstate and hashchange. It still looks for changes of the page content.

  • One refresh for each route change. Signals from the same route change are joined.

  • A new setting. The script reads WT.spaConfig. It does not read WT.SPA.

Script 3: Tag Trigger Script

Add this line as the first line in the try block of WT.refresh.

if (WT.spa && WT.spa.teardown) WT.spa.teardown();

Move your settings

SPA module v2 does not read WT.SPA. Remove it. If your routes use the query string or the hash, add WT.spaConfig above Script 1.

WT.spaConfig = { compare: 'path+search' };

Value of compare

A new view starts when

'pathname'

The path changes. This is the default.

'path+search'

The path or the query string changes.

'href'

Any part of the URL changes, the hash also.

Two settings of the original module have no direct replacement:

  • WT.SPA.regex. SPA module v2 runs on all pages of the site.

  • WT.SPA.method. If your site changes the view with no change of the URL, call WT.refreshAndExecute() from your code when the view changes.

After the upgrade

  • Test, then publish. Test the draft tag on your site before you publish it. Read QA Guide - Updating your tag.

  • Save your Visual Editor experiences again. Open each experience and do a quick save. Experiences that you do not save again still run, but their changes are not removed. New experiences need no extra step.

  • Remove the changes of your own code. Advanced Editor code is not removed for you. Use WT.onTeardown(). Read Available methods in the Web Client API (CAPI).

Check it

  1. Open your site and open the browser console.

  2. Run WT.spa.version. The result must be "2.0.0".

  3. Run WT.optimize.applied.enabled. The result must be true.

  4. Open a page where an experience runs. Then go to a page where it does not run, with no page load. The changes must be gone.

Go back to the original module

Put your old scripts back and restore WT.SPA. Tags that do not use the SPA module are not affected by any of this.

Next steps

Did this answer your question?