Adding custom scripts

This pageapplies toApigee andApigee hybrid.

View Apigee Edge documentation.

Warning: Custom scripts are supported forintegrating third-party services such as web metrics or feedback tools. Avoidusing custom scripts to manipulate the DOM to override or otherwise changebuilt-in behaviors of the portal.

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

  1. In the Apigee in Cloud console, go to theDistribution > Portals page.

    Go to Portals

  2. ClickSettings in the navigation menu.

  3. In theCustom Scripts section, enter the custom JavaScript code in thetext box. You can include multiple scripts.Warning: Make sure to enclose your customcode in<script> and</script> tags.Invalid JavaScript code has the potential to impact the correct display ofcontent or introduce security vulnerabilities across your entire site.

  4. ClickSave.

The following sections provide examples of custom scripts:

See alsoConfiguring analytics tracking.

Executing 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 theonLoad JavaScript event.
  • Is navigated away from using theonUnload JavaScript event.

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

Both theonLoad andonUnload events receive as their first parameters the current path of the page (/quickstart, for example). TheonUnload function receives as its second parameter the return value from theonLoad call enabling context to be passed between the two events. UseonUnload 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'){     //Changetextcontentoffirst<p>elementtosomething     //else.(DOMmustbeloadedwhenonLoadiscalled)     document.getElementsByTagName('p')[0].textContent=         'Welcome to the quick start! Be sure to send us your feedback.';     //printacustommessagetotheconsoleeverysecondwhileuserison     //quickstartpage.     constinterval=         window.setInterval(()=>console.log('Hello'),1000);     returninterval;   }   returnundefined; }, onUnload:(path,contextReturnedFromOnLoad)=>{   if(contextReturnedFromOnLoad!=null){     //Stopprintingcustommessagetoconsoleeverysecond.     window.clearInterval(contextReturnedFromOnLoad)   } },};</script>

Adding 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 inJavaScript; select one that meets your specific compliance requirements.

For example, the following script uses theCookie Info Script.

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

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.