Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

202406 update viewed product #41

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ bumped for multiple releases during one month.

#### Added
- Added `siteID` key/value to client-side events
- New `ProductID` field to Viewed Product events

### Changed
- Excludes Viewed Product and Add/Added to Cart events from optional event branded available in site configuration. This is to make sure that Klaviyo's internal recommendation engine will pick up these events for Recommendation product feeds.

### [24.1.0] - 2024-01-18

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function getData(productID) {

data['SiteID'] = siteId;
data['Product ID'] = product.ID;
data['ProductID'] = product.ID; // additive field to allow compatability with Viewed Product Recommendations
data['Product Name'] = product.name;
data['Product Page URL'] = URLUtils.https('Product-Show', 'pid', product.ID).toString();
data['Product Image URL'] = product.getImage(KLImageSize).getAbsURL().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ var EVENT_NAMES = {
if (Site.getCurrent().getCustomPreferenceValue('klaviyo_atc_override')) {
EVENT_NAMES.addedToCart = 'Add To Cart';
}
// events that can be branded if the klaviyo_sendEventsAsSFCC site preference is set
var BRANDED_EVENTS_NAMES = [
EVENT_NAMES.viewedCategory,
EVENT_NAMES.searchedSite,
EVENT_NAMES.startedCheckout,
EVENT_NAMES.orderConfirmation
];

var klaviyoEnabled = Site.getCurrent().getCustomPreferenceValue('klaviyo_enabled') || false;
var KLImageSize = Site.getCurrent().getCustomPreferenceValue('klaviyo_image_size') || 'large';
Expand Down Expand Up @@ -186,7 +193,7 @@ function trackEvent(exchangeID, data, event, customerEmail) {
this site that did not label events with SFCC as provider there will be a break in reporting and functionality between past events that were not
labelled with SFCC as provider and the new events that are. If in doubt, leave the site preference set to No and contact Klaviyo technical support.
*/
if (Site.getCurrent().getCustomPreferenceValue('klaviyo_sendEventsAsSFCC')) {
if (Site.getCurrent().getCustomPreferenceValue('klaviyo_sendEventsAsSFCC') && BRANDED_EVENTS_NAMES.includes(event)) {
metricObj.service = 'demandware';
}

Expand Down
Binary file modified metadata.zip
Binary file not shown.
1 change: 1 addition & 0 deletions test/src/tests/unit/viewed-product.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe('int_klaviyo_core/cartridge/scripts/klaviyo/eventData => viewedProduct'
{
"SiteID": "KlaviyoSFRA",
"Product ID": 'NG3614270264405',
"ProductID": 'NG3614270264405',
"Product Name": 'Belle de Teint',
"Product Page URL": "https://production-sitegenesis-dw.demandware.net/s/RefArch/home?lang=en_US",
"Product Image URL": "https://sforce.co/43Pig4s",
Expand Down
31 changes: 11 additions & 20 deletions test/src/utils/v3-api-helper.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import fetch from 'node-fetch'
import * as dotenv from 'dotenv'
import { should } from 'chai'

dotenv.config()

const { KLAVIYO_SFRA_PRIVATE_KEY, KLAVIYO_SITEGEN_PRIVATE_KEY, KLAVIYO_V3_URL } = process.env

const apiEndpoint = `https://${KLAVIYO_V3_URL}`
const metricsEndpoint = '/metrics/'
const eventsEndpoint = '/events/'
const sortFilter = '?sort=-datetime'

const sfraOpts = {
method: 'GET',
Expand All @@ -26,37 +29,25 @@ const sgOpts = {
},
}

let sfIntegrations
let integrationData

const getLatestMetricId = async () => {
const metricsFilter = '?fields[metric]=integration'
const integrationName = 'Salesforce Commerce Cloud'
const endpoint = `${apiEndpoint}${metricsEndpoint}${metricsFilter}`
let endpoint = `${apiEndpoint}${eventsEndpoint}${sortFilter}`

try {
const response = await fetch(endpoint, sfraOpts)
const body = await response.json()
const data = body.data
sfIntegrations = data.filter(
(integration) => integration.attributes.integration.name === integrationName
)
integrationData = body.data

} catch {
const response = await fetch(endpoint, sgOpts)
const body = await response.json()
const data = body.data
sfIntegrations = data.filter(
(integration) => integration.attributes.integration.name === integrationName
)
integrationData = body.data
}
const firstMetric = integrationData[0]
const metricData = firstMetric.relationships.metrics.data[0]

const mostRecentMetric = await sfIntegrations.reduce((latest, current) => {
const latestDate = new Date(latest.attributes.created)
const currentDate = new Date(current.attributes.created)

return latestDate > currentDate ? latest : current
})

return mostRecentMetric.id
return metricData.id
}

const getLatestMetricData = async (id) => {
Expand Down
Loading