Last active
January 25, 2021 13:41
-
-
Save zuzannamj/912e82b075f97cd3dbfd2e43a2942f0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%[ | |
SET @Submit = RequestParameter("Submit") | |
IF @Submit != "Success" THEN | |
]%% | |
<h2>Email:</h2> | |
<form action="%%=RequestParameter('PAGEURL')=%%" method="GET"> | |
<label for="email">Email address:</label> | |
<input type="email" id="email" name="email" required="yes"> | |
<button type="submit" name="Submit" id="Submit" value="Success">Submit</button> | |
</form> | |
%%[ | |
ENDIF | |
]%% | |
%%[ | |
SET @email = RequestParameter("email") | |
IF @email != "" THEN | |
]%% | |
<script runat="server"> | |
Platform.Load("Core", "1"); | |
try { | |
//authenticate to get access token | |
var authEndpoint = 'https://xxxx.auth.marketingcloudapis.com/' //provide API endpoint | |
var payload = { | |
client_id: "xxxxxx", //pass Client ID | |
client_secret: "xxxxxx", //pass Client Secret | |
grant_type: "client_credentials" | |
}; | |
var url = authEndpoint + '/v2/token' | |
var contentType = 'application/json' | |
var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload)); | |
if (accessTokenRequest.StatusCode == 200) { | |
var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]); | |
var accessToken = tokenResponse.access_token | |
var rest_instance_url = tokenResponse.rest_instance_url | |
}; | |
//make api call to inject contact to a journey | |
if (email != null && accessToken != null) { | |
var email = Request.GetQueryStringParameter("email"); | |
var headerNames = ["Authorization"]; | |
var headerValues = ["Bearer " + accessToken]; | |
var jsonBody = { | |
"ContactKey": email, | |
"EventDefinitionKey": "APIEvent-xxxxxxx", //provide journey EVENT DEFINITION KEY | |
"Data": { | |
"email_field": email | |
} | |
}; | |
var requestUrl = rest_instance_url + "/interaction/v1/events"; | |
var fireEntryEvent = HTTP.Post(requestUrl, contentType, Stringify(jsonBody), headerNames, headerValues); | |
if (fireEntryEvent.StatusCode == 201) { | |
Write("Success"); | |
} | |
}; | |
} catch (error) { | |
Write("Error"); | |
} | |
</script> | |
%%[ | |
ENDIF | |
]%% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment