Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d5c5dff
Fix compiler
marinhero Sep 8, 2021
c785e13
Add onBody dependency to guarantee DOM readyness when inserting the form
marinhero Sep 9, 2021
a101b83
Add new tags
marinhero Sep 9, 2021
564bb0c
Initialize and submit form
marinhero Sep 9, 2021
9b594a1
Add form settings validation
marinhero Sep 10, 2021
f5a92fc
Remove unused code
marinhero Sep 10, 2021
b06b48e
Update HISTORY
marinhero Sep 10, 2021
9d8d30e
Remove unused dependencies
marinhero Sep 10, 2021
8ff8084
WIP - TESTS
marinhero Sep 10, 2021
21d9ff9
Add new options to integration export
marinhero Sep 10, 2021
3620e0c
Remove onbody dependency
marinhero Sep 10, 2021
b9ab9fa
Move this reasign
marinhero Sep 10, 2021
0f60cc5
Fix tests
marinhero Sep 11, 2021
febec39
Fix unit test
marinhero Sep 13, 2021
439f773
Address comments
marinhero Sep 13, 2021
e3a7b99
Source stable script
marinhero Sep 13, 2021
89350f0
Add form test
marinhero Sep 13, 2021
4202c39
Reset compiler changes
marinhero Sep 13, 2021
85fb4ee
Remove beta file from test suite
marinhero Sep 13, 2021
d6d4ac6
Flag as beta
marinhero Sep 14, 2021
03fa58f
Update URL schema
marinhero Sep 14, 2021
8e03e41
Target an specific form so Marketo does not submit all forms
marinhero Sep 17, 2021
8782ebf
Dont use this inside a function because the scope changes
marinhero Sep 23, 2021
492ed6e
Setup no-redirects on form initialize to mitigate multiple form side …
marinhero Sep 25, 2021
c62b13e
Consider whenReady runs N times. N being the number of forms in a page
marinhero Sep 27, 2021
ace7938
update to 4.0.1 to prevent npm publish conflicts
marinhero Oct 5, 2021
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
8 changes: 8 additions & 0 deletions integrations/marketo-v2/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
4.0.0 / 2021-09-09
=================

* Deprecate associateLead method
* Remove email hash method
* Add support for Marketo forms
* Add support for marketoFormId and marketoHostUrl settings

3.0.7 / 2018-02-23
==================

Expand Down
117 changes: 68 additions & 49 deletions integrations/marketo-v2/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* Module dependencies.
*/

var fmt = require('@segment/fmt');
var integration = require('@segment/analytics.js-integration');
var is = require('is');
var jsonp = require('jsonp');
var url = require('component-url');
var when = require('do-when');
var each = require('@ndhoule/each');

// mapping of Standard Marketo API names: restAPIName: soapAPIName
var apiNameMapping = {
annualRevenue: 'AnnualRevenue',
Expand Down Expand Up @@ -82,11 +81,29 @@ var apiNameMapping = {
var Marketo = (module.exports = integration('Marketo V2')
.assumesPageview()
.global('Munchkin')
.global('MktForms2')
.option('host', 'https://api.segment.io')
.option('accountId', '')
.option('projectId', '')
.option('marketoHostUrl', '')
.option('marketoFormId', '')
.option('traits', [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add the marketoFormId option somewhere here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I'm not sure but I will learn about this. There's also marketoHostURL and it should be added in here as well

.tag('<script src="//munchkin.marketo.net/munchkin.js">'));
.tag('<script src="//munchkin.marketo.net/munchkin.js">')
.tag(
'forms',
'<script src="{{marketoHostUrl}}/js/forms2/js/forms2.min.js">'
));

var disableFormSubmitRedirects = function(form) {
if (form) {
// *** Do not remove this callback ***
// This ensures there are no page refreshes after the form is submitted and handles
// the side effects of multiple form submissions in the same page.
form.onSuccess(function() {
return false;
});
}
};

/**
* Initialize.
Expand All @@ -97,18 +114,51 @@ var Marketo = (module.exports = integration('Marketo V2')
*/

Marketo.prototype.initialize = function() {
var munchkinId = this.options.accountId;
var marketoHostUrl = this.options.marketoHostUrl;
var marketoFormId = parseInt(this.options.marketoFormId, 10);

var identifySettingsAreInvalid =
marketoHostUrl === undefined ||
marketoHostUrl === '' ||
Number.isNaN(marketoFormId) ||
marketoFormId <= 0;

if (identifySettingsAreInvalid) {
console.warn(
'Invalid settings for identify method. Please review your Marketo V2 destination settings.'
);
return;
}

var self = this;
this.load(function() {
window.Munchkin.init(self.options.accountId, {
window.Munchkin.init(munchkinId, {
asyncOnly: true
});

// marketo integration actually loads a marketo snippet
// and the snippet loads the real marketo, this is required
// because there's a race between `window.mktoMunchkinFunction = sinon.spy()`
// and marketo's real javascript which overrides `window.mktoMunchkinFunction`
// and deletes the spy.
when(self.loaded, self.ready);
});

this.load('forms', { marketoHostUrl: marketoHostUrl }, function() {
var marketoForm = document.createElement('form');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we only create the form id marketoHostUrl is defined ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a validation for that already. The code won't reach this point if marketoHostUrl is undefined

marketoForm.setAttribute('id', 'mktoForm_' + marketoFormId);
marketoForm.setAttribute('style', 'display:none');
document.body.appendChild(marketoForm);
window.MktoForms2.loadForm(
marketoHostUrl,
munchkinId,
marketoFormId,
function(form) {
disableFormSubmitRedirects(form);
}
);
});
};

/**
Expand Down Expand Up @@ -141,6 +191,12 @@ Marketo.prototype.page = function(page) {
});
};

Marketo.prototype.setupAndSubmitForm = function(traits, form) {
form.addHiddenFields(traits, form);
disableFormSubmitRedirects(form);
form.submit();
};

/**
* Identify.
*
Expand All @@ -154,6 +210,7 @@ Marketo.prototype.identify = function(identify) {
return;
}

var self = this;
var settings = this.options;

// we _must_ have an email
Expand Down Expand Up @@ -216,50 +273,12 @@ Marketo.prototype.identify = function(identify) {
}
}, settings.traits);

// associate the lead on the client-side so that
// we can track to the same user
this.requestHash(traitsToSendMarketo.Email, function(err, hash) {
var marketoFn = window.mktoMunchkinFunction;
if (marketoFn) marketoFn('associateLead', traitsToSendMarketo, hash);
});
};

/**
* Generate the URL to the Segment endpoint that hashes Marketo emails.
*
* @api private
* @param {string} email
* @return {string}
*/

Marketo.prototype.emailHashUrl = function(email) {
var host = this.options.host;
var projectId = this.options.projectId;
return fmt(
'%s/integrations/marketo/v1/%s/%s/hash-v2',
host,
projectId,
email
);
};

/**
* Marketo requires that users' requests come with a hashed version of their
* email address. It's a hash that can't be done on the client-side for some
* reason.
*
* Man, it'd have been great if someone had documented this in the first place.
*
* See https://github.com/segmentio/marketo-hash-app for more details.
*
* TODO: Improve this documentation
*
* @api private
* @param {string} email
* @param {Function} callback
*/
window.MktoForms2.whenReady(function(form) {
var marketoFormId = parseInt(settings.marketoFormId, 10);
var validFormId = !(Number.isNaN(marketoFormId) || marketoFormId <= 0);

Marketo.prototype.requestHash = function(email, callback) {
var url = this.emailHashUrl(email);
jsonp(url, callback);
if (validFormId && form.getId() === marketoFormId) {
self.setupAndSubmitForm(traitsToSendMarketo, form);
}
});
};
2 changes: 1 addition & 1 deletion integrations/marketo-v2/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@segment/analytics.js-integration-marketo-v2",
"description": "The Marketo analytics.js integration.",
"version": "3.0.8",
"version": "4.0.1",
"keywords": [
"analytics.js",
"analytics.js-integration",
Expand Down
Loading