Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DOI for Mailchimp #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[*.ts]
quote_type = single
max_line_length = 120
5 changes: 4 additions & 1 deletion docs/content/3.providers/2.mailchimp.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Mailchimp
description: 'Learn more about the Mailchimp provider integration for newsletter module.'
description: "Learn more about the Mailchimp provider integration for newsletter module."
---

Get down to business and grow sales. Engage your customers and boost your business with Mailchimp's advanced, yet easy‑to‑use marketing platform.
Expand All @@ -21,10 +21,13 @@ export default defineNuxtConfig({
apiKey: process.env.MAILCHIMP_API_KEY,
serverPrefix: process.env.MAILCHIMP_SERVER_PREFIX,
audienceId: process.env.MAILCHIMP_AUDIENCE_ID,
memberStatus: 'pending' // Set this to 'pending', if you want to use Double Opt-In. Default: subscribed
component: true // optional
}
}
})
```

The `memberStatus` can either one of these values: "subscribed", "unsubscribed", "cleaned", "pending", or "transactional". See [Mailchimp API docs](https://mailchimp.com/developer/marketing/api/list-members/add-member-to-list/) for details.

You can then use the built in `NewsletterForm.vue` component or `useNewsletterSubscribe` composable to handle adding new email to subscribers list.
11 changes: 9 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { defineNuxtModule, addComponentsDir, addServerHandler, addImportsDir } f
import defu from 'defu'

type Provider = 'buttondown' | 'mailchimp' | 'revue' | string;
type McMemberStatus = 'pending' | 'subscribed' | 'unsubscribed' | 'cleaned' | 'transactional' | string;

export type ModuleOptions = {
[key in Provider]: {
apiKey: string;
component?: boolean;
serverPrefix?: string; // Mailchimp only
audienceId?: string; // Mailchimp only
memberStatus?: McMemberStatus; // Mailchimp only
};
};

Expand All @@ -26,9 +28,9 @@ export default defineNuxtModule<ModuleOptions>({
configKey: 'newsletter',
compatibility: {
nuxt: '3',
}
},
},
setup (options, nuxt) {
setup(options, nuxt) {
if (Object.keys(options).length > 1) {
throw new Error('`[nuxt-newsletter]` More than one newsletter provider registered. Please choose only one')
}
Expand All @@ -47,6 +49,11 @@ export default defineNuxtModule<ModuleOptions>({
throw new Error('`[nuxt-newsletter]` Missing `mailchimp.server` value in module configuration')
}

// Setting 'subscribed' for new member subscribing with Mailchimp
if (options.mailchimp && !options.mailchimp.memberStatus) {
options.mailchimp.memberStatus = 'subscribed'
}

nuxt.options.runtimeConfig.newsletter = defu(nuxt.options.runtimeConfig.newsletter, {
...options
})
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/api/mailchimp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default defineEventHandler(async (event) => {
try {
const response = await mailchimp.lists.addListMember(newsletterConfig[providerName].audienceId, {
email_address: email,
status: 'subscribed'
status: newsletterConfig[providerName].serverPrefix.memberStatus
});

result = { message: `Email ${response.email_address} subscribed to Mailchimp`, status: 200 }
Expand Down