0
\$\begingroup\$

Hey everyone so i want to add a subscription form on my game. so far i can send data to a database and display them on a leaderboard, i found a similar question here but its a bit outdated. Ideally i would like to use klaviyo but since this is the only lead i have i might want to get familiar with how it works!

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class MailingScript : MonoBehaviour
{

    public InputField emailField;
    public InputField nameField;

    public void onClick()
    {
        StartCoroutine(SendToMailChimp());
    }

    private IEnumerator SendToMailChimp()
    {
        WWWForm form = new WWWForm();
        form.AddField("EMAIL", emailField.text);
        form.AddField("b_0f079b7af1cef143021ff4236_6cd74c0a6a", nameField.text);
        //form.AddField("subscribe", "Subscribe");
        UnityWebRequest www = UnityWebRequest.Post("https://slaga-games.us1.list-manage.com/subscribe/post?u=0f079b7af1cef143021ff4236&id=6cd74c0a6a", form);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Registered");
        }
    }
}

this is my code so far but it wont do anything. i have a same code for the database which works!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ I never used MailChimp, but I would assume that they don't want people to use their web interface for automation and would rather prefer if you used the API instead. \$\endgroup\$ – Philipp Mar 7 at 10:14
  • \$\begingroup\$ well i did manage to make it work but not with this script. i found this one on hithub github.com/fiftytwo/MailChimpSubscriber/blob/main/… and i just changed some variables to work for my case. the bad thing is since i do want to use klaviyo too is that klaviyo wont work! but hey at least i got mailchimp working! \$\endgroup\$ – stratos la Mar 7 at 14:13
  • \$\begingroup\$ The script you found uses the API endpoint I linked to, by the way. \$\endgroup\$ – Philipp Mar 7 at 16:42
0
\$\begingroup\$

found another script here that works for mailchimp https://github.com/fiftytwo/MailChimpSubscriber/blob/main/MailChimpSubscriber.cs Yeah of course so this is the script that i use at the moment for mailchimp

 #if UNITY_EDITOR || DEVELOPMENT_BUILD
#define ENABLE_MAILCHIMP_ERROR_LOG
#define ENABLE_MAILCHIMP_SUCCESS_LOG
#endif

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Text;
using System.Net.Mail;
using JetBrains.Annotations;
using UnityEngine.Networking;


namespace Fiftytwo
{
    [PublicAPI]
    public class MailChimpSubscriber : MonoBehaviour
    {
        private const string UrlFormat = "https://{0}.api.mailchimp.com/3.0/lists/{1}/members";
        private const string JsonFormat = "{{\"email_address\":\"{0}\", \"status\":\"subscribed\"}}";

        public MailChimpEvent SubscribeSuccess = new MailChimpEvent();
        public MailChimpEvent SubscribeError = new MailChimpEvent();

        [SerializeField] private string _apiKey = string.Empty;
        [SerializeField] private string _listId = string.Empty;
        public InputField text;

        public void Subscribe ()
        {
           

            if( text.text == null )
            {
#if ENABLE_MAILCHIMP_ERROR_LOG
                Debug.LogError( "MailChimp — No UI Text found at this GameObject" );
#endif
                SubscribeError.Invoke( null );
                return;
            }

            Subscribe( text.text );
        }

        public void Subscribe ( string email )
        {
            try
            {
                var url = BuildUrl();
                if( string.IsNullOrEmpty( url ) )
                {
#if ENABLE_MAILCHIMP_ERROR_LOG
                    Debug.LogError( "MailChimp — Invalid API Key format" );
#endif
                    SubscribeError.Invoke( email );
                    return;
                }

                var mailAddress = new MailAddress( email );

                StartCoroutine( SendToMailChimp( url, mailAddress.Address ) );
            }
            catch( Exception ex )
            {
#if ENABLE_MAILCHIMP_ERROR_LOG
                Debug.LogError( "MailChimp — Invalid email: " + ex.Message );
#endif
                SubscribeError.Invoke( email );
            }
        }

        private string BuildUrl ()
        {
            var separatorIndex = _apiKey.LastIndexOf( '-' );
            if( separatorIndex < 0 )
                return null;

            var urlPrefix = _apiKey.Substring( separatorIndex + 1 );
            if( string.IsNullOrEmpty( urlPrefix ) )
                return null;

            return string.Format( UrlFormat, urlPrefix, _listId );
        }

        private IEnumerator SendToMailChimp ( string url, string email )
        {
            var request = new UnityWebRequest( url, UnityWebRequest.kHttpVerbPOST );

            request.SetRequestHeader( "Content-Type", "application/json" );
            request.SetRequestHeader( "Authorization", "apikey " + _apiKey );
            
            var json = string.Format( JsonFormat, email );
            var jsonBytes = Encoding.UTF8.GetBytes( json );
            request.uploadHandler = new UploadHandlerRaw( jsonBytes );

            request.downloadHandler = new DownloadHandlerBuffer();

            yield return request.SendWebRequest();

            if( request.isNetworkError )
            {
#if ENABLE_MAILCHIMP_ERROR_LOG
                Debug.LogErrorFormat( "MailChimp — Network error: {0}", request.error );
#endif
                SubscribeError.Invoke( email );
                yield break;
            }

            if( request.isHttpError )
            {
#if ENABLE_MAILCHIMP_ERROR_LOG
                Debug.LogErrorFormat( "MailChimp — Subscribe error {0}: {1}\n{2}",
                    request.responseCode, request.error, request.downloadHandler.text );
#endif
                SubscribeError.Invoke( email );
                yield break;
            }

#if ENABLE_MAILCHIMP_SUCCESS_LOG
            Debug.LogFormat( "MailChimp — Subscribe success {0}\n{1}", request.responseCode, request.downloadHandler.text );
#endif
            SubscribeSuccess.Invoke( email );
        }

        [Serializable]
        public class MailChimpEvent : UnityEvent<string>
        {
        }
    }
}
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.