Tutorial on Working with the Manager of MarTech Agents
âšī¸ Status:
đĸ Production
The Martech Agents Manager is a tool to manage the loading of different marketing technology agents.
Table of Contents
đ Investigating the Object/Instance
When we load the library, then there is an object of the MartechAgentsManager class on the page. This object serves all the functionalities via methods, e. g. to put agents, the communication and the orchestration between them on the page.
Task
- Go to UAO and open the console.
- Type
MartechAgentsManagerand take a look at the object. - What can you find there?
- Try to make sense of the methods. What is their purpose?
Solution
{
// some meta data
"version": "v2.0.3",
"build_date": "2024-06-07T14:23:08.792Z",
"build_env": "production",
"env": "default"
// Prototype with utility functions
[[Prototype]]: {
currentEnv: void,
getAgents: void
push: void // pushing Agents to be handled by MTAM
// ...
}
}
đđŊ Pushing Agents
Since the promise of the MTAM is to run agents defined by JavaScript code on the page, it needs to get the information about the agent's code.
Pushing code works like this: MartechAgentsManager.push(() => /* functionality */)).
Task
- Try to push a custom agent that logs "Hello World!" into the console.
Solution
const customAgent = () => console.log("Hello World!");
MartechAgentsManager.push(customAgent) // Hello World!
đĄ Why can't I just append a <script> to the body?
You might question this: Isn't that overengineered? Why can't I just append a <script> to the body? Well, you might be right but there are two significant features of MTAM you will learn about in just a second which will challenge this view.
Task
You might already saw the function MartechAgentsManager.getRuntime(). The Runtime is a shared store for all the agents where they can store information and can subscribe themselves to updates on the information.
âšī¸ Note
For starters, make sure to be logged out and stay on UAO. It might be nice to clear the console once in a while with
clear().
- Try to get the
RuntimeObject- 2. Try to access the
Hint
Via the `MartechAgentsManager` object"userId"from theRuntime. What value do you expect? 3. Now, log in and try it again. 4. In this step, try to subscribe the runtime itself to changes regarding the variable"martech". It should log"Hello MarTech!"if the value changes. -6. Now, set the variableHint
Via the `runtime.subscribe(string, () => void)` functionmartechto"xyz"in the runtime. -Hint
Via the `runtime.set(string, value)` function
Solution
const runtime = MartechAgentsManager.getRuntime();
runtime.get("userId"); // undefined, because not logged in
// logs in
runtime.get("userId"); // number, because logged in
runtime.subscribe("martech", () => console.log("Hello MarTech!"));
runtime.set("martech", "xyz"); // Hello MarTech!
âšī¸ Note
Congrats! Now you know the basics of MTAM!
đđŧ Final Assignment
- Create a custom agent that pushes the follwing object into the
dataLayer:{ event: "MarTech", value: "Hello World!" }- 2. Configure that agent to also set the runtime variable
Hint
Push to the `dataLayer` with `dataLayer.push(object)`"MarTechPushed"totrue. 3. Subscribe theRuntimeto changes regarding the variableMarTechPushed. It should log"done"on changes. 4. Push your agent to theMartechAgentsManager5. Open thedataLayerobject to see your changes and note"done"being logged.
Solution
const runtime = MartechAgentsManager.getRuntime();
const customAgent = () => {
dataLayer.push({ event: "MarTech", value: "Hello World!" });
runtime.set("MarTechPushed", true);
}
runtime.subscribe("MarTechPushed", () => console.log("done"));
MartechAgentsManager.push(customAgent)
/*
"done"
dataLayer = [{...}, {
event: "MarTech",
gtm.uniqueEventId: number,
value: "Hello World!"
}]
*/
đ Congrats!
Congrats on grasping the most significant functionality of MTAM!
âšī¸ Note
One additional thing is
loadAfter, which we could not capture here. This is an array that we can give to the MTAM for a particular agent. The MTAM then ensures that the agent subscribes to the status of all agents noted in the array. Once these agents have indicated that they are loaded, our agent can also run. This for example is crucial for Onetrust. Onetrust must first be loaded and saves the consent information in theRuntime. After onetrust has been loaded, the other agents can subscribe to the consent information.Authors: Phil Pieper