Skip to main content

Tagging guide for NextJS applications

O
Written by Optimize Team

NextJS presents some small challenges compared to CMS implementations for installing the WTO tag. This brief article describes an easy approach to installing the WTO tag on NextJS applications.

Note: This document references a project which can be found at the following link on Github:

The environment

For this article, we have created an app using:

The application settings used were:

  • Tailwindcss

  • App Router

  • /src folder

When running npm run dev you'll find an output similar to:

Test page before changes are made

This is the default nextjs welcome page:

Implementation steps

Finding your tagging guide

On that page, you'll find your full tag script - copy it.

Edit your NextJS app

Find your layout.js script:

If you don't have a <head> tag, create one:

Paste in the tag copied from the WTO UI:

Modify the tag to look like this:

Note:

  • The tag changes from opening and closing tags to one self-closing tag.

  • We wrap the contents of the tag in a dangerouslySetInnerHTML attribute.

How to QA

  1. Go to your web page

  2. Right click and view-source

  3. Look for the WTO script, and find the opening script tag - we want to make sure it's a plain script tag, with no additional attributes like async/defer added:

  4. Make sure there are no new console errors

  5. Look in the Network tab, and filter by "webtrends". We are looking for

    1. wt.js

    2. wt_lib.js


    As long as these appear, we know everything will be fine with the rest of the settings being controlled in the WTO UI.

    Note: If you see a wt.js call with a 302, this is likely because you are on a http:// local page - this will not affect users when you go live on a https:// page.

Frequently asked questions

  • Q: Does this allow for feature-flagging?
    A: No, feature-flags are typically rendered using our server-side testing REST APIs. This guide is strictly for traditional client-side AB Testing.

  • Q: Are there any considerations for hooking into page/state changes I need to think about as a website developer?
    A: No, we hook into these changes in our tag, there is no additional work required on the website-end.

Did this answer your question?