npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

marketfaux

v1.0.1

Published

Send Marketo form data without actually using a Marketo form

Downloads

6

Readme

Marketfaux

For when you want to use Marketo forms, but not its API.

Why not use Marketo's Forms API?

Marketo's Forms API is opinionated in terms of style and behavior. In some cases, those opinions are terrible and outdated. Additionally, sometimes you just want to break the mold and do something that would not normally be possible, such as utilizing a JavaScript framework like React.

What Marketfaux does (and what it doesn't do)

Marketfaux just provides functions to send data as if you've filled out a standard Marketo form. That way, you can implement the form the way you want and still have your form's results passed on to Marketo.

Marketfaux, however, is not a drop-in replacement for the Marketo Forms API. It does not generate the form's HTML or validate input—those bits will have to be implemented by you.

API

Return type

Marketfaux uses Wretch to send requests using the Fetch API. Please see their documentation to learn more about error handling and responses.

All functions return Wretch's ResponseChain type.

Base URL, Munchkin ID, and form ID

The first three parameters for each function are the same as in Marketo's loadForm function:

  1. Base URL (ex. "//app-sjqe.marketo.com")
  2. Munchkin ID (ex. "718-GIV-198")
  3. Form ID (ex. 621)

Functions

sendForm(baseUrl: string, munchkinId: string, formId: string | number, form: HTMLFormElement | string): ResponseChain

Bundle up the data from a <form /> element and submit it. Accepts either the element itself or a query selector for the element.

Examples
Marketfaux.sendForm("//app-sjqe.marketo.com", "718-GIV-198", 621, "form#my-id")
Marketfaux.sendForm("//app-sjqe.marketo.com", "718-GIV-198", 621, document.querySelector("form#my-id"))

sendData(baseUrl: string, munchkinId: string, formId: string | number, form: FormData | Record<string, any>): ResponseChain

Sends provided data which can either be a FormData object or a standard key-value object.

Bracketization

Marketfaux will automatically bracketize keys with multiple values to conform to PHP's naming conventions. If those keys already have bracket notation, they will not be bracketized.

Input
{
  "key": ["value1", "value2"]
}
Output
key[]: value1
key[]: value2
Examples
Marketfaux.sendData("//app-sjqe.marketo.com", "718-GIV-198", 621, { key1: "value1", key2: "value2" })
const data = new FormData()
data.append('key1', 'value1')
data.append('key2', 'value2')

Marketfaux.sendData("//app-sjqe.marketo.com", "718-GIV-198", 621, data)

send(baseUrl: string, munchkinId: string, formId: string | number, payload: HTMLFormElement | string | FormData | Record<string, any>): ResponseChain

Convenience method that will automatically use either sendForm or sendData based on the type of the provided payload.

Examples

On the page

<form id="my-form">
  ...
</form>

<script src="/path/to/marketfaux.min.js"></script>
<script>
  var form = document.getElementById('my-form');
  form.addEventListener('submit', function (event) {
    event.preventDefault();
    Marketfaux.send("//app-sjqe.marketo.com", "718-GIV-198", 621, form)
      .badRequest(function () {
        // Handle 400 error
      })
      .timeout(function () {
        // Handle 408 error
      })
      .internalError(function () {
        // Handle 500 error
      })
      .json(function (result) {
        // Handle success
      })
  })
</script>

In an app

import { send } from 'marketfaux'

const formData = {
  // Key-value object
}

send("//app-sjqe.marketo.com", "718-GIV-198", 621, formData)
  .badRequest(() => {
    // Handle 400 error
  })
  .timeout(() => {
    // Handle 408 error
  })
  .internalError(() => {
    // Handle 500 error
  })
  .json((result) => {
    // Handle success
  })

What type of errors should I expect?

Marketo is surprisingly loose when it comes to the data submitted to it.

It will throw errors if:

  • your base URL, Munchkin ID, or form ID is invalid
    • 400 Bad Request error
  • there are connectivity issues such as user being offline or Marketo being unreachable

It will not throw errors if you submit:

  • an incompatible value (such as a random string when something in date format is expected)
    • The value for this field will stay as it was before submitting
  • non-existent fields
    • Nothing happens
  • real fields that are not added to the form
    • Populates that field with the provided value
  • without required fields
    • Skips those fields