Skip to content

Commit

Permalink
Merge pull request #13 from stape-io/user-identity-data-check
Browse files Browse the repository at this point in the history
User identity data check
  • Loading branch information
kHorozhanov authored Mar 1, 2024
2 parents 558c1dd + e6c4f01 commit 59c368f
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 94 deletions.
2 changes: 2 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
homepage: "https://stape.io/"
versions:
- sha: c06fed2c219ba1eb812de229fa2c2241dbaa4617
changeNotes: Added user identity check.
- sha: 2e8b17a8cf3a8739fb767f734278bbc7ba8a53b7
changeNotes: Add active on site data.
- sha: 81bc683597af8e808531b31f4ec84dc96c461b3d
Expand Down
112 changes: 65 additions & 47 deletions template.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,44 +50,11 @@ function sendEvent() {
let eventName = data.type === 'active_on_site' ? '__activity__' : data.event;
let eventNameLogs = data.type === 'active_on_site' ? 'page_view' : data.event;

let klaviyoEventData = {
data: {
type: 'event',
attributes: {
properties: {},
metric: {
data: {
type: 'metric',
attributes: {
name: eventName
}
}
},
profile: {
data: {
type: 'profile',
attributes: {}
}
}
}
}
};

if (data.value) klaviyoEventData.data.attributes.value = data.value;
else if (eventData.value) klaviyoEventData.data.attributes.value = data.value;

if (data.uniqueId) klaviyoEventData.data.attributes.unique_id = data.uniqueId;
else if (eventData.unique_id)
klaviyoEventData.data.attributes.unique_id = data.unique_id;

klaviyoEventData.data.attributes.properties = getProperties();
addViewedItemsIfNeeded(eventName, klaviyoEventData);

klaviyoEventData.data.attributes.profile.data.attributes =
getCustomerProperties();
if (data.klaviyoUserId)
klaviyoEventData.data.attributes.profile.data.id = data.klaviyoUserId;
let klaviyoEventData = getKlaviyoEventData(eventName);

if (!hasUserIdentificationData(klaviyoEventData)) {
return data.gtmOnSuccess();
}
let url = 'https://a.klaviyo.com/api/events/';

if (isLoggingEnabled) {
Expand Down Expand Up @@ -145,7 +112,52 @@ function sendEvent() {
}
}

function getProperties() {
function getKlaviyoEventData(eventName) {
return {
data: {
type: 'event',
attributes: getAttributes(eventName)
}
};
}

function getAttributes(eventName) {
const attributes = {
properties: getProperties(eventName),
metric: {
data: {
type: 'metric',
attributes: {
name: eventName
}
}
},
profile: {
data: getProfileData()
}
};

const uniqueId = data.unique_id || eventData.unique_id;
if (uniqueId) attributes.unique_id = uniqueId;

const value = data.value || eventData.value;
if (value) attributes.value = value;

return attributes;
}

function getProfileData() {
const profileData = {
type: 'profile',
attributes: getCustomerProperties()
};
if (data.klaviyoUserId) {
profileData.id = data.klaviyoUserId;
}
return profileData;
}

function getProperties(eventName) {
let klaviyoProperties = {};

if (data.forwardAllProperties) {
Expand Down Expand Up @@ -178,6 +190,14 @@ function getProperties() {
}
}

if (eventName === 'Viewed Product') {
let viewedItems = getViewedItems();

if (viewedItems.length) {
klaviyoProperties['$viewed_items'] = viewedItems;
}
}

return klaviyoProperties;
}

Expand Down Expand Up @@ -415,13 +435,11 @@ function hasItem(arr, item) {
return false;
}

function addViewedItemsIfNeeded(eventName, klaviyoEventData) {
if (eventName === 'Viewed Product') {
let viewedItems = getViewedItems();

if (viewedItems.length > 0) {
klaviyoEventData.data.attributes.properties['$viewed_items'] =
getViewedItems();
}
}
function hasUserIdentificationData(klaviyoEventData) {
const profileData = klaviyoEventData.data.attributes.profile.data;
return (
!!profileData.id ||
!!profileData.attributes.email ||
!!profileData.attributes._kx
);
}
112 changes: 65 additions & 47 deletions template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -504,44 +504,11 @@ function sendEvent() {
let eventName = data.type === 'active_on_site' ? '__activity__' : data.event;
let eventNameLogs = data.type === 'active_on_site' ? 'page_view' : data.event;
let klaviyoEventData = {
data: {
type: 'event',
attributes: {
properties: {},
metric: {
data: {
type: 'metric',
attributes: {
name: eventName
}
}
},
profile: {
data: {
type: 'profile',
attributes: {}
}
}
}
}
};

if (data.value) klaviyoEventData.data.attributes.value = data.value;
else if (eventData.value) klaviyoEventData.data.attributes.value = data.value;

if (data.uniqueId) klaviyoEventData.data.attributes.unique_id = data.uniqueId;
else if (eventData.unique_id)
klaviyoEventData.data.attributes.unique_id = data.unique_id;

klaviyoEventData.data.attributes.properties = getProperties();
addViewedItemsIfNeeded(eventName, klaviyoEventData);

klaviyoEventData.data.attributes.profile.data.attributes =
getCustomerProperties();
if (data.klaviyoUserId)
klaviyoEventData.data.attributes.profile.data.id = data.klaviyoUserId;
let klaviyoEventData = getKlaviyoEventData(eventName);
if (!hasUserIdentificationData(klaviyoEventData)) {
return data.gtmOnSuccess();
}
let url = 'https://a.klaviyo.com/api/events/';

if (isLoggingEnabled) {
Expand Down Expand Up @@ -599,7 +566,52 @@ function sendEvent() {
}
}

function getProperties() {
function getKlaviyoEventData(eventName) {
return {
data: {
type: 'event',
attributes: getAttributes(eventName)
}
};
}

function getAttributes(eventName) {
const attributes = {
properties: getProperties(eventName),
metric: {
data: {
type: 'metric',
attributes: {
name: eventName
}
}
},
profile: {
data: getProfileData()
}
};

const uniqueId = data.unique_id || eventData.unique_id;
if (uniqueId) attributes.unique_id = uniqueId;

const value = data.value || eventData.value;
if (value) attributes.value = value;

return attributes;
}

function getProfileData() {
const profileData = {
type: 'profile',
attributes: getCustomerProperties()
};
if (data.klaviyoUserId) {
profileData.id = data.klaviyoUserId;
}
return profileData;
}

function getProperties(eventName) {
let klaviyoProperties = {};

if (data.forwardAllProperties) {
Expand Down Expand Up @@ -632,6 +644,14 @@ function getProperties() {
}
}

if (eventName === 'Viewed Product') {
let viewedItems = getViewedItems();
if (viewedItems.length) {
klaviyoProperties['$viewed_items'] = viewedItems;
}
}

return klaviyoProperties;
}

Expand Down Expand Up @@ -869,15 +889,13 @@ function hasItem(arr, item) {
return false;
}

function addViewedItemsIfNeeded(eventName, klaviyoEventData) {
if (eventName === 'Viewed Product') {
let viewedItems = getViewedItems();
if (viewedItems.length > 0) {
klaviyoEventData.data.attributes.properties['$viewed_items'] =
getViewedItems();
}
}
function hasUserIdentificationData(klaviyoEventData) {
const profileData = klaviyoEventData.data.attributes.profile.data;
return (
!!profileData.id ||
!!profileData.attributes.email ||
!!profileData.attributes._kx
);
}


Expand Down

0 comments on commit 59c368f

Please sign in to comment.