One of the best ways to get new email subscribers is with a sign up form. Forms give you the opportunity to ask every person who visits your website to subscribe to your email list. And this not only allows you to grow your list, it helps you turn casual site visitors into email subscribers you can communicate with over and over again.
And with the release of AWeber's tagging feature, we can automatically give subscribers a tag when they join a list through a sign up form, and then send automated email series or one-time emails to subscribers who have that tag.
This is great! But often, when a subscriber completes a form, you may want to attribute different tags to them based on their interests, preferences or choice. You may even want to add them to different lists depending on their choices!
This can help you send even more targeted and relevant email content to a subscriber, and as a result, improve email engagement.
So, to help you accomplish this and get a glimpse of how powerful the AWeber API is, I’ve created a Python script that shows how to use the AWeber API to not only apply different tags to subscribers based off the selections they make on a sign up form, but also to subscribe them to different email lists! Let’s dive in.
When would you want to use this?
Pretend you’re an online store owner, and each of your email lists is associated with a different department. You have three departments: books, t-shirts and handmade cat bows. And you have an AWeber email list for each department.
You frequently put on sales for items based on popularity, and the upcoming discount will be on the most popular cat bow, The Purple-Spotted Mouse.
You don't want to bother your book readers with an email about cat bows – that wouldn’t make sense! So you decide to make sure all your incoming subscribers are put on the correct department list and then given a tag that signifies which specific product they expressed interest in when they signed up.
For this scenario, the first thing we need is a sign up form. I am choosing a form that includes a drop down where the subscriber will choose a product they are interested in when they fill out the form. This choice will then correspond to a particular department list and tag.
The code
Before we can run the Python script, there are a few things you need to do first:
- Install the following packages: flask, requests and requests_oauthlib.
- Copy and paste your account id, list ids (you need three lists!) and OAuth tokens at the top of the script.
- Save the code example below as
app.py
.
Now keep in mind this is in no way production ready and is only shown in a single file for simplicity. To run this app, open a terminal, locate the file and run python app.py
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
import os from flask import Flask, redirect, render_template_string, request import requests from requests_oauthlib import OAuth1 app = Flask(__name__) API_URL = 'https://api.aweber.com/1.0/accounts/{}/lists/{}/subscribers' ############################ # Replace the following constants # with your AWeber credentials # #ACCOUNT_ID=XXXXXXXXXXX #CAT_LIST_ID=XXXXXXXXXXX #BOOK_LIST_ID=XXXXXXXXXXX #TSHIRT_LIST_ID=XXXXXXXXXXX # #CONSUMER_KEY='XXXXXXXXXXX' #CONSUMER_SECRET='XXXXXXXXXXX' #ACCESS_TOKEN='XXXXXXXXXXX' #ACCESS_SECRET='XXXXXXXXXXX' ############################ AUTH = OAuth1(CONSUMER_KEY, client_secret=CONSUMER_SECRET, resource_owner_key=ACCESS_TOKEN, resource_owner_secret=ACCESS_SECRET, signature_type='auth_header') @app.route('/') def webform(): return render_template_string(""" <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>My Form</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <div class="container-fluid"> <div class="jumbotron"> <h1>Sign up to receive discounts on your favorite product!</h1> </div> <div class="row"> <div class="col-xs-12 col-md-8"> <form class="form-horizontal" method="POST"> <div class="form-group"> <label for="inputName" class="col-sm-2 control-label">Name</label> <div class="col-sm-10"> <input name="name" type="name" class="form-control" id="inputName" placeholder="Name"> </div> </div> <div class="form-group"> <label for="inputEmail" class="col-sm-2 control-label">Email</label> <div class="col-sm-10"> <input name="email" type="email" class="form-control" id="inputEmail" placeholder="Email"> </div> </div> <div class="form-group"> <label for="inputProduct" class="col-sm-2 control-label">Select a product you are interested in receiving discounts for!</label> <div class="col-sm-10"> <select name="tag" id="inputProduct" class="form-control"> <option disabled selected value> -- Select a Product -- </option> <optgroup label="Cat Bow Ties"> <option value="{{ CAT_LIST_ID }}:purple-spotted-mouse" class="form-control">Purple Spotted Mouse</option> </optgroup> <optgroup label="Books"> <option value="{{ BOOK_LIST_ID }}:hitchikers-guide" class="form-control">Hitchiker's Guide to the Galaxy</option> </optgroup> <optgroup label="T-Shirts"> <option value="{{ TSHIRT_LIST_ID }}:cat-horse-vneck" class="form-control">Cat on horse V-neck</option> <option value="{{ TSHIRT_LIST_ID }}:doggle-copter" class="form-control">Doggle-copter!</option> </optgroup> </select> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">Sign Up!</button> </div> </div> </form> </div> </div> </div> </body> </html> """, CAT_LIST_ID=CAT_LIST_ID, BOOK_LIST_ID=BOOK_LIST_ID, TSHIRT_LIST_ID=TSHIRT_LIST_ID) @app.route('/', methods=['POST']) def submit(): list_id, tag = request.form['tag'].split(':') data = {'email': request.form['email'], 'name': request.form['name'], 'tags': [tag]} response = requests.post(API_URL.format(ACCOUNT_ID, list_id), headers={'User-Agent': 'AWeber-Blog-Example/1.0'}, auth=AUTH, json=data) if response.ok: return 'Thanks!' return response.content if __name__ == '__main__': app.debug = True app.run() |
*This script was tested on python 2.7 and 3.5.
Now you’re ready to level up your automation!
With this new script in place, you are now ready to start your sale on the Purple-Spotted Mouse Cat Bow! The script determines the list and tag to add to the subscriber based on the subscriber’s selection when they sign up.
Now when subscribers fill out your sign up form and choose the cat bow, they will be tagged with “purple-spotted-mouse” and be added to the cat bow department list in your AWeber account.
Once your subscriber is tagged and added to a list, you can then trigger automation series based on that tag or create a segment of subscribers with that specific tag and send a one-time broadcast email to them.
That’s it!
That’s just one of the ways you can use the AWeber API to customize your automation. The options are nearly limitless! If you have any requests for other scenarios where you might use tags, automation and the AWeber API, comment below to let me know.
If you’re having trouble getting the access keys necessary to run this script, please email our Applications Support team. You can also comment here demanding another spectacular blog post about it or check out our php getting started guide.