CJS Product Generator
This section provides a Custom JavaScript (CJS) function that transforms your e-commerce data layer into the standardized product format required by Neotag events. This is particularly useful when working with Google Tag Manager or other tag management systems.
What is CJS?
Custom JavaScript (CJS) is a feature in tag management systems that allows you to create dynamic variables by writing JavaScript functions. In the context of Neotag, CJS helps you transform your existing e-commerce data structure into the required product format.
Key Benefits:
- Automatically converts your data layer format to Neotag’s standardized structure
- Reduces manual coding and potential errors
- Maintains consistency across all product-related events
- Works seamlessly with Google Analytics 4 Enhanced Ecommerce data
CJS Function Template
Use this CJS function to create a custom variable in your tag manager. This function reads from your e-commerce data layer and creates the products array in Neotag’s expected format.
function() {
var productsArray = [];
var originalProductsArray = {{dl.ecommerce - items}};
for(i=0;i<originalProductsArray.length;i++) {
productsArray.push({
name: originalProductsArray[i].item_name,
sku: originalProductsArray[i].item_id,
price: originalProductsArray[i].price,
quantity: originalProductsArray[i].quantity || 1,
category: originalProductsArray[i].item_category,
category2: originalProductsArray[i].item_category2,
category3: originalProductsArray[i].item_category3,
category4: originalProductsArray[i].item_category4,
category5: originalProductsArray[i].item_category5,
item_list_id: originalProductsArray[i].item_list_id,
item_list_name: originalProductsArray[i].item_list_name,
variant: originalProductsArray[i].item_variant,
brand: originalProductsArray[i].item_brand
})
}
return productsArray;
}
How to Use This CJS:
- In Google Tag Manager, create a new Variable of type “Custom JavaScript”
- Copy and paste the CJS function above into the Custom JavaScript field
- Name your variable (e.g., “CJS – Neotag Products”)
- Save the variable
- Use this variable in your Neotag event tags as the products parameter
Data Layer Mapping
The CJS function maps common e-commerce data layer properties to Neotag’s standardized format:
Data Layer Property | Neotag Property | Description |
---|---|---|
item_name | name | Product name (required) |
item_id | sku | Product SKU/ID (required) |
price | price | Product price |
quantity | quantity | Product quantity (defaults to 1) |
item_category | category | Primary product category |
item_brand | brand | Product brand |
item_variant | variant | Product variant (size, color, etc.) |
Using CJS in Neotag Events
Once you’ve created your CJS variable, you can use it in your Neotag event tracking. Here’s how to implement it:
// In your Neotag event tag, use the CJS variable
neotag.track('add_to_cart', {
currency: '{{Currency}}', // Another GTM variable
value: {{Total Value}}, // Another GTM variable
products: {{CJS - Neotag Products}} // Your CJS variable
});
// Example with purchase event
neotag.track('purchase', {
transaction_id: '{{Transaction ID}}',
currency: '{{Currency}}',
value: {{Purchase Value}},
products: {{CJS - Neotag Products}}
});
// Example with view_item event
neotag.track('view_item', {
currency: '{{Currency}}',
value: {{Item Value}},
products: {{CJS - Neotag Products}}
});
Pro Tips:
- Test your CJS function in GTM’s preview mode to ensure it’s working correctly
- Use GTM’s debug console to inspect the generated products array
- Customize the CJS function to match your specific data layer structure
- Consider creating separate CJS functions for different product contexts if needed
Customizing the CJS Function
You may need to modify the CJS function to match your specific data layer structure. Here are common customizations:
Different Data Layer Path:
If your products are stored in a different data layer path:
var originalProductsArray = {{dl.custom_ecommerce.items}}
Additional Product Properties:
Add custom properties to the products array:
custom_field: originalProductsArray[i].custom_property
Data Transformation:
Transform data types or formats:
price: parseFloat(originalProductsArray[i].price_string)