Marketing

Newsletter signups in Go

A Go package for the signup box: who asked to hear from the store, and who asked to stop. It keeps each address with where it came from and when, lists and exports them for an operator, and unsubscribes by token. It sends nothing; in its own words, that is a marketing tool’s job.

What it does

In the package’s words, “a subscription is an email address and a consent.” The storefront posts the address to POST /x/newsletter/subscribe, with an optional name and a source such as footer. The store keeps it lower-cased, with the date, and answers with the status alone: “an address and a token are not a storefront’s to show.”

An operator reads the list on the Newsletter screen, types in an address for a customer who asked at the counter, removes one on request, and exports the list as CSV. The package says it plainly: “Nothing here sends a newsletter: that is a marketing tool’s job, and the export is how the list reaches one.”

Each address gets a random token, and the unsubscribe route takes that token rather than the address, so a forwarded link cannot be edited into somebody else’s. Signing up again after leaving puts the same row back to subscribed.

Configuration

Nothing to configure in Go: Config has no fields. The Plugins screen holds the three lines of copy a storefront’s signup box can show.

Newsletter module settings — 3 settings, 0 required
SettingEnvironment variableRequiredWhat it does
heading
Signup heading
— No “Get the news” by default. Public, for the storefront to read. Panel only.
blurb
Signup text
— No “New arrivals and offers, now and then. Unsubscribe any time.” by default. Public. Panel only.
thanks
After signing up
— No “Thanks — you’re on the list.” by default. Public. Panel only.

The module reads its Config struct, not the environment. The variable names are the ones the package’s own example or the reference binary uses; in your own main() you choose where each value comes from. Where a setting has a panel label, the admin’s settings drawer can hold it too, and a value typed there wins over Config.

Setting it up

  1. Install the modulePass newsletter.New to gocommerce.New, as below, or start the reference binary with -newsletter. It adds one table, and the plugin starts switched on.
  2. Post the signupThe storefront posts email, name and source to POST /x/newsletter/subscribe, with a hidden website field that people leave empty.
  3. Export for the mailerExport CSV on the Newsletter screen, or GET /api/admin/x/newsletter/subscriptions.csv, downloads the subscribed addresses with name, source and dates.
  4. Unsubscribe by tokenGET /x/newsletter/unsubscribe?token=…, or a POST with the token in the body, marks the address unsubscribed. The token lives in the table only; see the limits.

main.go

import (
	"github.com/itswadesh/gocommerce/core"
	"github.com/itswadesh/gocommerce/ext/newsletter"
)

app, err := gocommerce.New(cfg, newsletter.New(newsletter.Config{}))

The package doc has no example of its own; this is how the reference binary’s -newsletter flag and examples/store/main.go install it. cfg is your gocommerce.Config. Import path github.com/itswadesh/gocommerce/ext/newsletter.

How it works

What it does not do

Read these before an order depends on it. No store is known to run GoCommerce in production yet, so these come from the code, not from anyone’s experience.

FAQ

Questions about the Newsletter module

Does it send newsletters?

No. It collects signups and unsubscribes and keeps the list. Sending is left to a mailing tool, which gets the list from the CSV export.

What is in the export?

One row per address, with email, name, source, status, subscribed_at and unsubscribed_at, the dates in RFC 3339 and UTC. By default only subscribed addresses; ?status=unsubscribed gives the ones who left.

How does a shopper unsubscribe?

Through a link to /x/newsletter/unsubscribe carrying their token, opened or posted. The token is created at signup and kept in the store’s table; no route returns it, so building the link means reading it from the database.

Are bot signups kept?

Not those that fill the hidden website field: they are told they are subscribed and nothing is stored. Other bots are not stopped by the module.

Source

Everything on this page is read from ext/newsletter in the GoCommerce repository, MIT licensed. When this page and the code disagree, the code is right and this page is out of date.

Try it against a store of your own

The backend + admin quick start runs GoCommerce’s API and admin on your own machine, without a storefront. Add this module to it and try it on test orders before a real one depends on it.

Chat on WhatsApp