Skip to content
  • There are no suggestions because the search field is empty.

Installing Convert Experiences on a Vue.js app.

How to install the Convert tracking code in a Vue.js single page app, with or without the tracking-vue component

 THIS ARTICLE WILL HELP YOU:


How Convert Works With Vue.js

In Convert Experiences, you install the tracking code once, in the <head> of the HTML page that loads your Vue.js app. You don't need to add Convert code to your Vue components.

Vue.js apps are single page apps: they change the URL and redraw parts of the page without loading a new page. The Convert tracking script has SPA optimizations built in, and they're on by default in every Web project. When a visitor moves to another route, the script:

  • checks your experiences' locations, audiences and goals against the new URL;
  • removes the elements and styles that variations added on the previous route, then applies the variations that match the new route;
  • applies a change again if your app redraws the element the change was made on.

SPA Optimizations explains each of these and shows where to turn them off.

📒Note: If your project shows Use Legacy Script under Configuration > Environments & Tracking Code, it still runs the legacy tracking script, which is deprecated. Follow Migrate from the Legacy Tracking Script to the New Tracking Script (Step-by-Step) first.

Install the Tracking Code in Your Vue.js App

  1. Open your project and click the Configuration tab.
  2. In the left menu, click Environments & Tracking Code.
  3. On the row of the environment you want, for example your production environment, open the row's menu and click Install Code Snippet.

    Row menu of the Production environment under Environments & Tracking Code, with Install Code Snippet, Edit this Environment and Delete this Environment

  4. The Code Snippet window opens on the Basic snippet tab. Copy the code.

    The Code Snippet window on the Basic snippet tab, with the tracking code to add in the head section of your pages

  5. Paste it into the <head> of the HTML file that loads your app, just after </title>. In a project built with Vite, that file is index.html in the project root.
  6. Deploy your app.

The window gives the same instruction: Add the following project code in the <head> section, just after </title>, on all your experiences and goal pages.

In a single page app, every route loads from that one HTML file, so adding the code there once covers all your pages.

If your Vue.js app is rendered on the server, for example with Nuxt, add the code to the <head> of the HTML your server sends for every page.

⚠️ Important: Keep the tracking code synchronous: don't add async or defer to it, and don't load it later from your own code. From the <head>, the script covers the page as soon as it runs and shows it once your experiences are applied, so visitors don't see the original page first. If the script loads later, visitors can see the original page for a moment before the variation. This is called flicker.

For more on where the code goes and the difference between the basic and advanced snippet, see How to Install the Main Tag / Convert Tracking Code / JavaScript.

Remove Old Mounted-Hook Code

Earlier versions of this article told you to add code to the mounted hook of your Vue components. That code read window.convert.currentData.experiments and pushed executeExperiment to _conv_q for each experiment. If you added it, remove it. Search your components for executeExperiment and currentData.experiments to find it.

The tracking script already runs your experiences again when the URL changes, so this code repeats work the script does on its own.

If an experience should start at a moment rather than on a URL, for example when a modal opens or a step of a form appears, use a location trigger. See Use a location trigger in Running Experiments on Single Page Apps. To start one experience from your own code, see Run one experience by hand in the same article.

Use the @convertcom/tracking-vue Component

⚠️ Important: Convert labels @convertcom/tracking-vue experimental. It may cause flicker if it isn't used correctly. Convert strongly recommends installing the tracking code in the <head> of your pages instead, as described in Install the tracking code in your Vue.js app.

@convertcom/tracking-vue is an npm package with a Vue component that loads the Convert tracking script from inside your app. When the component mounts, it covers the page and loads the script. The page shows once Convert has applied your experiences, or when the timeout runs out, whichever comes first.

The component needs Vue 3.

  1. Install the package:
    npm install @convertcom/tracking-vue

    Or, with Yarn:

    yarn add @convertcom/tracking-vue
  2. Find your account ID and project ID. They're the two numbers in the file name at the end of the script address in your tracking code. In 1234567-7654321.js, 1234567 is the account ID and 7654321 is the project ID. To see your tracking code, follow steps 1 to 4 of Install the tracking code in your Vue.js app.
  3. Add the component at the top of your root component, usually App.vue, so it stays on the page for the whole visit:
    <script setup>
    import ConvertTrackingScript from '@convertcom/tracking-vue';
    </script>

    <template>
    <ConvertTrackingScript accountId="1234567" projectId="7654321" />
    <!-- the rest of your app -->
    </template>
  4. Optionally, set how long the page can stay covered while the script loads, in milliseconds:
    <ConvertTrackingScript accountId="1234567" projectId="7654321" :timeout="1500" />

The component has three props:

PropRequiredWhat it does
accountIdYesYour Convert account ID, as text.
projectIdYesYour Convert project ID, as text.
timeoutNoThe longest time, in milliseconds, that the page stays covered while the script loads. The default is 2500 (2.5 seconds). Write it with a colon, :timeout="1500", so Vue passes it as a number.

Before you use the component, keep in mind:

  • Use either the tracking code in the <head> or the component, not both.
  • The component loads the script after your app has mounted, which is later than the tracking code in the <head>. If the script hasn't applied your experiences when the timeout runs out, the page shows anyway, and visitors can see the original page before the variation.
  • The component loads your project's tracking script without an environment. Without an environment, every active experience can run, whatever environment it's assigned to. If you use environments, for example to keep experiences you're still testing off your live site, install that environment's code snippet in the <head> instead. See The Environments Feature.
  • The component always loads the script from Convert's own domain. It doesn't use a custom domain (CNAME) you set up for the tracking script. If you use one, install the code snippet, which uses your custom domain. See Custom Domains (CNAME) for the Convert Tracking Script.
  • Keep it in a component that stays on the page, such as App.vue. When the component unmounts, it removes the script tag it added. If it mounts again, it adds the script again.

Check That Convert Is Running

After you deploy, open your site and check that the tracking script loads and your experiences run. See Check if Convert Experiences Is Installed Correctly and Tracking Script Installation Verification.

If a variation doesn't show after the visitor moves to another route, or stays on the page after they move on, see Single Page Application (SPA) Troubleshooting Guide.