Skip to content

Instantly share code, notes, and snippets.

@arrenv
Forked from jamesmusgrave/campaign-monitor-angular.html
Last active October 4, 2018 10:19
Show Gist options
  • Save arrenv/fd99e1b0a8ea323152746d915f11d1f4 to your computer and use it in GitHub Desktop.
Save arrenv/fd99e1b0a8ea323152746d915f11d1f4 to your computer and use it in GitHub Desktop.
Angular 5/6 method to handle Campaign Monitor's secure subscribe link signup
<form [formGroup]="subForm" (ngSubmit)="submitsubForm()">
<mat-form-field>
<input matInput type="text" name="email" placeholder="Email Address" formControlName="emailFormControl">
</mat-form-field>
<button mat-flat-button>Submit</button>
</form>
import { FormControl, Validators } from '@angular/forms';
export class AppComponent implements {
cm_id = '';
cm_emailfield = 'cm-';
subForm = new FormGroup({
emailFormControl: new FormControl('', [ Validators.required, Validators.email ])
});
submitsubForm(): void {
let subdata = {
'email' : this.subForm.controls.emailFormControl.value,
'data' : this.cm_id
};
// Get secure subscribe URL from Campaign Monitor
this.apiService.campaignMonitorToken(subdata)
.subscribe( token => {
console.log(token);
// Subscribe user with new secure subscribe URL from Campaign Monitor
this.apiService.campaignMonitorSub(token, this.subForm.controls.emailFormControl.value, this.cm_emailfield)
.subscribe( result => {
console.log(result);
});
});
}
}
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { HttpErrorHandler, HandleError } from '../http-error-handler.service';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'lgc-auth-token'
})
};
@Injectable()
export class ApiService {
private handleError: HandleError;
constructor(
private http: HttpClient,
httpErrorHandler: HttpErrorHandler) {
this.handleError = httpErrorHandler.createHandleError('ApiService');
}
/** Campaign Monitor Get Token */
campaignMonitorToken (data): Observable<any> {
return this.http.post('https://createsend.com//t/getsecuresubscribelink', `email=${data.email}&data=${data.data}`, { headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded'}), responseType: 'text'})
.pipe(
catchError(this.handleError('campaignMonitorToken', data))
);
}
/** Campaign Monitor Add Subscriber */
campaignMonitorSub (secureUrl, email, emailfield): Observable<any> {
return this.http.post(secureUrl, `${emailfield}=${email}`, { headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded'}), responseType: 'text'})
.pipe(
catchError(this.handleError('campaignMonitorSub', email))
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment