Skip to content

addRawJs

Add custom JavaScript code that Htag will execute. This is useful for implementing custom logic, integrating with third-party services, or adding compatibility layers for legacy code.

// Basic usage
window.htag.api('1').addRawJs({
id: 'my-custom-script',
code: 'console.log("Hello from Htag!");',
runOnce: true,
});
// Using macros
window.htag.api('1').addRawJs(
{
id: 'website-specific-script',
code: 'console.log("Website: {{websiteName}}");',
runOnce: true,
},
{
websiteName: 'My Website',
},
);
  • rawJsObject (required): An object with the following properties:

    • id: A unique identifier for the script
    • code: The JavaScript code to execute
    • runOnce: If true, the script runs only once when Htag initializes. If false, it runs on every page transition.
  • macros (optional): An object with key-value pairs for macro substitution in the code. Each key corresponds to a macro name in the script, and the value replaces the macro.

Returns the Htag API object, allowing for method chaining.

For more information about how raw JS works in Htag, see the RawJS concept documentation.