addRawJs
Overview
Section titled “Overview”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.
Example
Section titled “Example”// Basic usagewindow.htag.api('1').addRawJs({ id: 'my-custom-script', code: 'console.log("Hello from Htag!");', runOnce: true,});
// Using macroswindow.htag.api('1').addRawJs( { id: 'website-specific-script', code: 'console.log("Website: {{websiteName}}");', runOnce: true, }, { websiteName: 'My Website', },);Parameters
Section titled “Parameters”-
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.
Return Value
Section titled “Return Value”Returns the Htag API object, allowing for method chaining.
Related Concepts
Section titled “Related Concepts”For more information about how raw JS works in Htag, see the RawJS concept documentation.