You're viewing **Apigee Edge** documentation.  
[Go to the
**Apigee X** documentation](http://cloud.google.com/apigee/docs/api-platform/publish/portal/custom-scripts). info


> [!NOTE]
> **Note:** The integrated portal is available as an Apigee-managed solution for Apigee Edge for Public Cloud customers only. If you are an Apigee Edge for Private Cloud customer, refer to the customer-managed [Drupal portal solution](https://docs.apigee.com/api-platform/publish/drupal/open-source-drupal).

<br />

> [!WARNING]
> **Warning**: Custom scripts are supported for integrating third-party services such as web metrics or feedback tools. Avoid using custom scripts to manipulate the DOM to override or otherwise change built-in behaviors of the portal.

To add custom JavaScript code or HTML content before the `<body>` tag on each page in your portal:

1. Select **Publish \> Portals** and select your portal.
2. Click **Settings** on the landing page. Alternatively, you can select **Settings** in the drop-down in the top navigation bar.
3. Click the **Custom Scripts** tab.
4. In the **Custom Scripts** section, enter the custom JavaScript code in the text box. You can include multiple scripts.

   > [!WARNING]
   > **Warning** : Make sure to enclose your custom code in `<script>` and `</script>` tags. Invalid JavaScript code has the potential to impact the correct display of content or introduce security vulnerabilities across your entire site.

5. Click **Save**.

The following sections provide examples of custom scripts:

- [Execute a custom script during and onLoad or onUnload JavaScript event](https://docs.apigee.com/api-platform/publish/portal/custom-scripts#javascript-event)

- [Add a cookie consent popup](https://docs.apigee.com/api-platform/publish/portal/custom-scripts#cookie-consent)

See also [Configure analytics tracking](https://docs.apigee.com/api-platform/publish/portal/api-portal-analytics).

## Execute a custom script during an onLoad or onUnload JavaScript event

Define custom scripts to be executed when each page in your portal:

- Loads into the DOM using the `onLoad` JavaScript event.
- Is navigated away from using the `onUnload` JavaScript event.

Your custom function must be defined as part of the `portal.pageEventListeners` in the global namespace (declared on the `window` variable).

Both the `onLoad` and `onUnload` events receive as their first parameters the current path of the page (`/quickstart`, for example). The `onUnload` function receives as its second parameter the return value from the `onLoad` call enabling context to be passed between the two events. Use `onUnload` to clean up the event listeners that are no longer required and perform other clean up activities.

For example:

    <script>
    window.portal = {};
    window.portal.pageEventListeners = {
      onLoad: (path) => {
        if (path === '/quickstart') {
          // Change text content of first <p> element to something
          // else. (DOM must be loaded when onLoad is called)
          document.getElementsByTagName('p')[0].textContent =
              'Welcome to the quick start! Be sure to send us your feedback.';
          // print a custom message to the console every second while user is on
          // quickstart page.
          const interval =
              window.setInterval(() => console.log('Hello'), 1000);
          return interval;
        }
        return undefined;
      },
      onUnload: (path, contextReturnedFromOnLoad) => {
        if (contextReturnedFromOnLoad != null) {
          // Stop printing custom message to console every second.
          window.clearInterval(contextReturnedFromOnLoad)

        }
      },
    };
    </script>

## Add a cookie consent popup

Custom scripts may be used to implement a cookie consent solution. There are a number of popular open source options implemented in
JavaScript; select one that meets your specific compliance requirements.

For example, the following script uses the [Cookie Info Script](https://cookieinfoscript.com/).

    <script type="text/javascript" id="cookieinfo" src="//cookieinfoscript.com/js/cookieinfo.min.js">
    </script>

> [!NOTE]
> **Note** : If you [configured a content security policy](https://docs.apigee.com/api-platform/publish/portal/csp), you may need to add an exception to the content security policy to enable download of an external file.