Data Elements and Transformation Functions are two of the core building blocks of data when implementing your tags with MagicPixel. Data Elements helps us specify where our data will come from (like cookies, data layer, server headers etc..) but thats where the similarities end.
Why Transformation Functions?
There are situations where you will have the data layer value return an object and need to convert it into a variety of other formats to be used within tags or provider attributes. The normal approach for other tag managers is to create more data elements and format the value each time. This results in the same piece of data being redefined in various places, and if the source data changes, you will have to update all the places where it is being read and massaged to different formats.
This is where transformation functions play a major role. A transformation function can take any number of data elements as inputs and format the data and return it in a different format required for a dynamic template parameter.
Creating Transformation Function
Let's create a transformation function example and see how this can be used.
For creating a transformation function, we will be following the below steps,
Navigate to "Transformers" in the left side navigation
Click on the "Add Transformer" - "for Browser" option. (Learn more about client side and server side transformation functions)
Enter a function name and description for your transformation function
Name = demoFunction
Description = This transformation function does the following, 1) Transforms the url to upper case 2) Reads the document title and appends to the url with a separator 3) Returns the final value
Under the function arguments section, choose data elements which will be provided as an input to your transformation function. All the data elements passed here will be available as input arguments inside the functions - Choose the function inputs shown in the demo video below
Under the code section of transformation function you can write simple transformation function or a complex one including array manipulations to format, change or combine the input values being sent to the function