Add an Embedded Script Extension Using the Wix CLI

This feature is in Developer Preview and is subject to change.

An embedded script is an app extension that injects an HTML code fragment into the DOM of your users' sites. Unlike other extensions, embedded scripts are not fully configured during app installation, and require an additional setup step to embed the code fragment.

Follow the instructions below to:

  1. Create an embedded script extension for a Wix CLI app.
  2. Prepare your app for production.
  3. Build and deploy your app.

Once this task is complete, your app will have an embedded script extension that injects its HTML code fragment into the DOM of every page of your users' sites.

Before you begin

Step 1 | Create the extension

In the terminal:

  1. Navigate to your project repo.

  2. Run the following command and follow the prompts to create an embedded script extension:

    Copy
    1

    During the creation process you will be prompted to select a script type and a placement. For this guide, you can make any selection. For more information, see the script type and placement options.

Upon completion, the extension files will be created in your app directory with the following structure:

Copy
1
  • embedded.html - This file contains the HTML code fragment for your embedded script.
  • embedded.json - This file contains information about your embedded script.
  • params.dev.json - This file contains values for dynamic parameters to use during testing.

Note: This step has covered the basics of setting up an embedded script extension. For more information about how you can customize your embedded script, read Embedded Script Extension Files and Code

Step 2 | Prepare your app for production

To finish setting up your embedded script, either you or the site owner must call the embedScript() endpoint to embed your script and update the values of the dynamic parameters in each app instance.

If your app has a dashboard page, you have the option to shift responsibility for this last configuration step onto site owners.

Note: If an app has a dashboard page and an embedded script extension, site owners will automatically be directed to the app's dashboard page after installing the app.

Site owners can call embedScript() from the Wix React SDK.

This API call is also used to specify the value of any dynamic parameters. For more information about using dynamic parameters see Using dynamic parameters in your HTML code.

To use embedScript() in your app's dashboard page:

  1. Open the page.tsx file in your app’s src/dashboard/pages folder.

  2. Add the following import statements at the top of your page:

    Copy
    1
  3. Inside the Index method, add the following code:

    Copy
    1
  4. Add the embedScript() call somewhere in your code.

    For example, add a call to action with instructions to click a button to complete setup for your app. Then, when the site owner clicks the button, they will call the endpoint.

    If your app contains only one embedded script, the endpoint should be called in the following format:

    Copy
    1

    If your app contains more than one embedded script, you must also pass a componentId using the id value defined in your embedded.json file. In this situation, your call should be in the following format:

    Copy
    1

    Warning: If your app only has 1 embedded script, don't pass the componentId in the request body. This action could break your app in production. The componentId is only relevant for apps with more than 1 embedded script.

Ensure that parameters contains all the dynamic parameters in your embedded script. Otherwise, you will get an error and your code will not be embedded.

Although we are setting it up now, the Embed Script call will not work until you have created an app version, as documented below.

For a practical example of this configuration, see our Mixpanel Analytics template.

Embedding a script as an app developer

You can also call the Embed Script endpoint from your server once the app is installed on a user's site. This requires an access token obtained through the OAuth process.

Step 3 | Build and deploy your app

Now that your app is ready for production, you can build your app and create a version in the Wix Dev Center.

  1. Run the following command to build your app:

    Copy
    1
  2. Run the following command and follow the prompts to create an app version:

    Copy
    1

An app version allows you to publish an app to the Wix App Market or install it on a site with a direct install URL.

For more information about building and deploying your app, see Build and Deploy an App with the CLI.

Summary

By following these instructions, you have:

  1. Created and configured an embedded script extension for your application.
  2. Provided a method to embed your extension’s HTML code fragment on your users' sites. When site owners download your app and complete the setup process, your code will be injected into the DOM of every page on their site.
  3. Built your app and created a version on the Wix Dev Center.

See also

Was this helpful?
Yes
No