Custom Code
Advanced
It is also possible to write your own JavaScript code and apply it to a given DMP Pixel in order to collect custom data attributes. The script can then be added at the first step, without the <script> tags.
JavaScript Template
Below the template that can be used for defining and sending custom data attributes to the DMP.
var_statement ; conditional_statement {flx1.data('variable_key', attribute_key, true)}
Elements | |
---|---|
var_statement | The variable statement declares a variable, optionally initializing it to a value. |
conditional_statement | The conditional statement executes a statement if a specified condition is true |
variable_key | This represents the variable_key that needs to be specified by the implementation specialist using JavaScript. |
attribute_key | This represents the attribute_key that will be send to the DMP. |
true | This is the outcome of the conditional statement as part of the JavaScript that initiates the custom data pass. |
Simple Example
A step-by-step guide on how populate custom data via the custom code. Note that step 3 from the below guideline is optional and not required.
- Define a name for variable: var var_name
- Include the css path to the new variable: $('var_location').text()
- Add the trim method to remove whitespaces from both ends of the string: $('var_location').text().trim()
- Write a conditional statement to only capture the variable if the condition is true: if (var_name.length > 0)
Define the custom data attribute according to its conditional statement: flx1.data('var_name', attribute_key, true)
Add the JavaScript code to the relevant On-Site Pixel in the Data Collection user interface.
For the example the complete JavaScript code that captures the 'Lager' value from the breadcrumb look as follows:
var TopLevelCategoryBreadcrumb = $('body > div.wrapper.site-container.snap-content > nav > div > ol > li:nth-child(2) > a > span.breadcrumb-name').text().trim(); if (TopLevelCategoryBreadcrumb.length > 0) { flx1.data('top_level_cat', TopLevelCategoryBreadcrumb, true); }
Advanced Example
Based on the guidance from the step-by-step guide, an even more advanced example will be given below. It shows the custom code configuration to capture the fifth element (URL) as mentioned in the example at the top of this wiki entry. The use case is to strip and capture the product ID from the following URL structure: https://drizly.com/corona-extra/p2822.
- Define a name for variable: var urlParts
- Define the path to the urlParts by a JavaScript expression: document.URL.split('/');
- Define a new variable name to consolidate only the last part of the urlParts: var lastPart
- Define the path to the lastPart variable by a JavaScript expression: urlParts[urlParts.length - 1];
- Write a conditional statement to only capture the variable if the 'p' is the first character of the last URL part: if (lastPart.indexOf('p') === 0)
- Define a new variable name to consolidate the ID and strip the 'p' part: var productId
- Write function to remove the 'p' parameter since we only want the iD: lastPart.replace('p', '');
Define the custom data attribute according to its conditional statement: flx1.data('product_item_id', productId, true);
Add the JavaScript code to the relevant On-Site Pixel in the Data Collection user interface.
The complete JavaScript code that captures the '2822' value from the URL looks as follows:
var urlParts = document.URL.split('/'); var lastPart = urlParts[urlParts.length - 1]; if (lastPart.indexOf('p') === 0) { var productId = lastPart.replace('p', ''); flx1.data('product_item_id', productId, true); }