Skip to content
Open
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: 3 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"singleQuote": true,
"trailingComma": "none"
"trailingComma": "none",
"tabWidth": 2,
"printWidth": 100
}
3 changes: 3 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
homepage: 'https://stape.io/'
documentation: 'https://github.com/stape-io/hubspot-tag'
versions:
- sha: b15bfc8acc7c69ac94e6795ecdb617034a26bbfc
changeNotes: Updated consent section.
- sha: 8b14d492116d07d82478da9277ba7d107649ec95
changeNotes: Fix endpoint to associate Custom Object and Contact.
- sha: c7d260bbedb081ed41c4aaa84e3810d210102e41
Expand Down
130 changes: 42 additions & 88 deletions template.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const sendHttpRequest = require('sendHttpRequest');
const JSON = require('JSON');
const encodeUriComponent = require('encodeUriComponent');
const getAllEventData = require('getAllEventData');
const getContainerVersion = require('getContainerVersion');
const getRequestHeader = require('getRequestHeader');
const makeTableMap = require('makeTableMap');
const JSON = require('JSON');
const logToConsole = require('logToConsole');
const makeInteger = require('makeInteger');
const makeNumber = require('makeNumber');
const makeTableMap = require('makeTableMap');
const Promise = require('Promise');
const getAllEventData = require('getAllEventData');
const encodeUriComponent = require('encodeUriComponent');
const logToConsole = require('logToConsole');
const getContainerVersion = require('getContainerVersion');
const sendHttpRequest = require('sendHttpRequest');

/******************************************************************************/
/*==============================================================================
==============================================================================*/

const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = getRequestHeader('trace-id');
Expand Down Expand Up @@ -45,34 +46,25 @@ if (handler) {
data.gtmOnFailure();
}

/******************************************************************************/
/*==============================================================================
Vendor related functions
==============================================================================*/

function trackPageViewEvent() {
let url =
'https://track.hubspot.com/__ptq.gif?k=1&v=1.1&ct=' +
encodeUriComponent('standard-page');
'https://track.hubspot.com/__ptq.gif?k=1&v=1.1&ct=' + encodeUriComponent('standard-page');
const clientId = eventData.client_id;
if (data.accountId) url = url + '&a=' + encodeUriComponent(data.accountId);
if (clientId) {
url =
url +
'&vi=' +
encodeUriComponent(clientId) +
'&u=' +
encodeUriComponent(clientId);
url = url + '&vi=' + encodeUriComponent(clientId) + '&u=' + encodeUriComponent(clientId);
}
if (eventData.ga_session_id)
url = url + '&b=' + encodeUriComponent(eventData.ga_session_id);
if (eventData.page_referrer)
url = url + '&r=' + encodeUriComponent(eventData.page_referrer);
if (eventData.page_title)
url = url + '&t=' + encodeUriComponent(eventData.page_title);
if (eventData.page_location)
url = url + '&pu=' + encodeUriComponent(eventData.page_location);
if (eventData.ga_session_id) url = url + '&b=' + encodeUriComponent(eventData.ga_session_id);
if (eventData.page_referrer) url = url + '&r=' + encodeUriComponent(eventData.page_referrer);
if (eventData.page_title) url = url + '&t=' + encodeUriComponent(eventData.page_title);
if (eventData.page_location) url = url + '&pu=' + encodeUriComponent(eventData.page_location);
if (eventData.screen_resolution)
url = url + '&sd=' + encodeUriComponent(eventData.screen_resolution);
if (eventData.page_encoding)
url = url + '&cs=' + encodeUriComponent(eventData.page_encoding);
if (eventData.page_encoding) url = url + '&cs=' + encodeUriComponent(eventData.page_encoding);

logRequest('page_view', 'GET', url, '');

Expand Down Expand Up @@ -104,11 +96,9 @@ function trackCustomBehavioralEvent() {
: {}
};

if (data.customBehavioralEventUtk)
bodyData.utk = data.customBehavioralEventUtk;
if (data.customBehavioralEventUtk) bodyData.utk = data.customBehavioralEventUtk;
if (data.email) bodyData.email = data.email;
if (data.customBehavioralEventObjectId)
bodyData.objectId = data.customBehavioralEventObjectId;
if (data.customBehavioralEventObjectId) bodyData.objectId = data.customBehavioralEventObjectId;
if (data.customBehavioralEventOccurredAt)
bodyData.occurredAt = data.customBehavioralEventOccurredAt;

Expand All @@ -117,12 +107,7 @@ function trackCustomBehavioralEvent() {
sendHttpRequest(
url,
(statusCode, headers, body) => {
logResponse(
statusCode,
headers,
body,
data.customBehavioralEventEventName
);
logResponse(statusCode, headers, body, data.customBehavioralEventEventName);

if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
Expand Down Expand Up @@ -192,12 +177,9 @@ function createDealLineItems(dealId, products) {

const lineItem = products[i];

if (products[i].quantity)
lineItem.quantity = makeInteger(products[i].quantity);
if (products[i].quantity)
lineItem.num_items = makeInteger(products[i].quantity);
if (products[i].quantity)
lineItem.quantity_per_line = makeInteger(products[i].quantity);
if (products[i].quantity) lineItem.quantity = makeInteger(products[i].quantity);
if (products[i].quantity) lineItem.num_items = makeInteger(products[i].quantity);
if (products[i].quantity) lineItem.quantity_per_line = makeInteger(products[i].quantity);

if (products[i].price) lineItem.price = makeNumber(products[i].price);
if (products[i].price) lineItem.amount = makeNumber(products[i].price);
Expand All @@ -210,9 +192,7 @@ function createDealLineItems(dealId, products) {
sendEcommerceRequest(
'product_get',
'GET',
'https://api.hubapi.com/crm/v3/objects/products/' +
products[i].id +
'?idProperty=hs_sku',
'https://api.hubapi.com/crm/v3/objects/products/' + products[i].id + '?idProperty=hs_sku',
''
).then((productId) => {
lineItem.sku = makeInteger(products[i].id);
Expand Down Expand Up @@ -251,8 +231,7 @@ function removeDealLineItems(dealId, products) {
sendEcommerceRequest(
'line_item_delete',
'DELETE',
'https://api.hubapi.com/crm/v3/objects/line_items/' +
currentLineItems[l].id,
'https://api.hubapi.com/crm/v3/objects/line_items/' + currentLineItems[l].id,
''
);
}
Expand Down Expand Up @@ -285,10 +264,7 @@ function associateDealToLineItem(dealId, lineItemId) {
}

function getCurrentLineItems(dealId) {
const url =
'https://api.hubapi.com/crm/v3/objects/deals/' +
dealId +
'/associations/line_items';
const url = 'https://api.hubapi.com/crm/v3/objects/deals/' + dealId + '/associations/line_items';

logRequest('get_current_line_item_ids', 'GET', url, '');

Expand All @@ -300,12 +276,7 @@ function getCurrentLineItems(dealId) {
},
''
).then((result) => {
logResponse(
result.statusCode,
result.headers,
result.body,
'get_current_line_item_ids'
);
logResponse(result.statusCode, result.headers, result.body, 'get_current_line_item_ids');

if (result.statusCode >= 200 && result.statusCode < 300) {
const currentLineItemsIds = JSON.parse(result.body).results;
Expand All @@ -315,8 +286,7 @@ function getCurrentLineItems(dealId) {
inputs: currentLineItemsIds,
properties: ['hs_product_id', 'hs_sku']
};
const url =
'https://api.hubapi.com/crm/v3/objects/line_items/batch/read';
const url = 'https://api.hubapi.com/crm/v3/objects/line_items/batch/read';

logRequest('get_current_line_items', 'POST', url, bodyData);

Expand All @@ -328,12 +298,7 @@ function getCurrentLineItems(dealId) {
},
JSON.stringify(bodyData)
).then((result) => {
logResponse(
result.statusCode,
result.headers,
result.body,
'get_current_line_items'
);
logResponse(result.statusCode, result.headers, result.body, 'get_current_line_items');

if (result.statusCode >= 200 && result.statusCode < 300) {
return JSON.parse(result.body).results;
Expand Down Expand Up @@ -394,8 +359,7 @@ function createOrUpdateDeal() {
dealId = sendEcommerceRequest(
'deal_update',
'PATCH',
'https://api.hubapi.com/crm/v3/objects/deals/' +
parsedBody.results[0].id,
'https://api.hubapi.com/crm/v3/objects/deals/' + parsedBody.results[0].id,
dealData
);
} else {
Expand Down Expand Up @@ -440,12 +404,7 @@ function createOrUpdateContact() {
},
JSON.stringify(bodyData)
).then((result) => {
logResponse(
result.statusCode,
result.headers,
result.body,
'contact_search'
);
logResponse(result.statusCode, result.headers, result.body, 'contact_search');

if (result.statusCode >= 200 && result.statusCode < 300) {
const parsedBody = JSON.parse(result.body);
Expand All @@ -456,12 +415,9 @@ function createOrUpdateContact() {
};

if (data.email) contactData.properties.email = data.email;
if (data.contactFirstName)
contactData.properties.firstname = data.contactFirstName;
if (data.contactLastName)
contactData.properties.lastname = data.contactLastName;
if (data.contactPhone)
contactData.properties.mobilephone = data.contactPhone;
if (data.contactFirstName) contactData.properties.firstname = data.contactFirstName;
if (data.contactLastName) contactData.properties.lastname = data.contactLastName;
if (data.contactPhone) contactData.properties.mobilephone = data.contactPhone;

if (makeInteger(parsedBody.total) > 0) {
const contactID = parsedBody.results[0].id;
Expand Down Expand Up @@ -520,8 +476,7 @@ function createOrUpdateCustomObject() {
properties: {}
};
for (let i in customObjectParameters) {
bodyData.properties[customObjectParameters[i].key] =
customObjectParameters[i].value;
bodyData.properties[customObjectParameters[i].key] = customObjectParameters[i].value;
}

logRequest('createCustomObject', 'POST', url, bodyData);
Expand Down Expand Up @@ -569,12 +524,7 @@ function associateCustomObjectWithContact(customObjectId, contactId) {
sendHttpRequest(
url,
(statusCode, headers, body) => {
logResponse(
statusCode,
headers,
body,
'associateCustomObjectWithContact'
);
logResponse(statusCode, headers, body, 'associateCustomObjectWithContact');

if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
Expand Down Expand Up @@ -615,6 +565,10 @@ function identifyVisitorEvent() {
);
}

/*==============================================================================
Helpers
==============================================================================*/

function getRequestHeaders() {
return {
'Content-Type': 'application/json',
Expand Down
Loading
Loading