Shared code is JavaScript that is automatically executed on every page load on a website. Unlike modifications, shared code does not use placements. By default, shared code is executed on every page that includes the Frosmo scripts. You can use a trigger to limit on which pages and when a piece of shared code is executed.

Shared code on a site

Figure: Shared code on a site

You can use shared code on a site to, for example:

For more information about shared code, see:

Shared code vs. modifications

Both shared code and modifications allow you to run new code on a site. Which solution is right for you depends on what kind of code you want to run and what you want to achieve with that code.

Use shared code...

Do not use shared code to programmatically inject HTML into a page, since the Frosmo Platform cannot track such content for clicks, displays, and true displays. If you want to use HTML, create a modification instead.

Use a modification...

How shared code works

The process for creating and executing shared code flows as follows:

  1. A user creates a piece of shared code in the Frosmo Control Panel (or directly through the Graniitti API). The Frosmo Platform stores the code in the Frosmo back end.
  2. The Frosmo Platform adds the shared code to the site's custom script.
  3. When a visitor views a page on the site:
    1. The browser loads the Frosmo scripts.
    2. The browser starts initializing the scripts.
    3. If the custom script contains shared code that does not use triggers, Frosmo Core executes the code immediately.
    4. Frosmo Core initializes the triggers defined for the site.
    5. The browser completes initializing the scripts.
  4. When a trigger fires on the page, Frosmo Core executes the shared code bound to the trigger, if any.

    This can happen even before the scripts are fully initialized, since a trigger can be bound to an event that occurs before the initialization completes.

The following figure shows the data flow for shared code, from creating the code in the Control Panel to executing the code on the page.

Data flow for shared code

Figure: Data flow for shared code (click to enlarge)

Shared code is effectively the first Frosmo feature to get initialized on a page. In other words, Frosmo Core executes shared code (without triggers) before initializing most other features, including triggers, modifications, and context.

For executing a piece of shared code, Frosmo Core automatically wraps the code inside an immediately invoked function expression (IIFE). This has the added advantage of preventing the variables in the code from leaking into the global scope.

Where to go next