Usando variables de Discourse para personalizar Typeform embebido

In @sam’s example of an embedded Typeform, the url for the Typeform is:

https://basvanleeuwen1.typeform.com/to/NzdRpx?typeform-embed=embed-widget

That’s great, but I would like to personalize my Typeforms using hidden fields. If the Discourse username could be added to the url, then I could use it in the form. So when I visit, the url would be:

https://basvanleeuwen1.typeform.com/to/NzdRpx?typeform-embed=embed-widget&username=markschmucker

Is there a way to do that? Is it a candidate for a plugin?

Note you need a paid Typeform account for this to work.

Sounds a bit too fancy to be in the core onebox, the good news is that you will probably not need a plugin here a theme component can do the trick.

2 Me gusta

Thanks Sam. So something like this?

<script type="text/discourse-plugin" version="0.8">
  $( document ).ready(function() {
    const user = api.getCurrentUser(),
      username = user.username;

    $('h1.hello-world').html('Hello there '+username+"!")
  });
</script>

Except find and replace the url instead of saying Hello?

Yeah this is not the approach I would take, what you are looking for is decorate cooked in the plugin API.

2 Me gusta

Here is my (somewhat clumsy) solution, in case it helps someone. I added this code under Admin > Customize > Themes > Edit CSS/HTML > /head:

    <script type="text/discourse-plugin" version="0.8">
        api.decorateCooked($elem => {
            var i, j;
            var selector = 'https://example.typeform.com';
            // seems like this should work but it doesn't:
            // var typeforms = $elem[0].querySelectorAll('src^=[' + selector + ']');
            var ps = $elem[0].querySelectorAll('p');
            for (i=0; i<ps.length; i++) {
                p = ps[i];
                var iframes = p.querySelectorAll('iframe');
                for (j=0; j<iframes.length; j++) {
                    var iframe = iframes[j];
                    if (iframe.src.startsWith(selector)) {
                        var oldsrc = iframe.getAttribute('src');
                        var username = api.getCurrentUser()['username'];
                        var newsrc = oldsrc + '?username=' + username;
                        iframe.setAttribute('src', newsrc);
                    }
                }
            }
        });
    </script>

Then add a Hidden Field “username” in your Typeform. The username can be used in your form, and will also be sent to the destination (integrations or webhook) when the user hits Submit.

When you get the Typeform link to embed in Discourse, it will be something like:

https://example.typeform.com/to/dr8oAa?username=xxxxx

Remove the ?username=xxxxx before embedding; the js above will add it back, with the proper Discourse username.

1 me gusta

Cualquier otra persona que encuentre esto más tarde, tengo una versión actualizada de la publicación increíble de @mark78:

Agrega este código en Administrador > Personalizar > Temas > Editar CSS/HTML > HEAD

️ ¡Si estás en la versión empresarial, tendrás que crear un componente de tema vacío para agregar esto.

<script type="text/discourse-plugin" version="0.8.25">
    api.decorateCooked(($elem) => {
        const iframes = $elem[0].querySelectorAll('iframe[src^="https://form.typeform.com"]');
        const userName = api.getCurrentUser()?.username;
        
        if(!userName){ return; }
        
        for (let i = 0; i < iframes.length; i++) {
            iframes[i].setAttribute('src', `${iframes[i].getAttribute('src')}&username=${userName}`);
        }
    });
</script>
  • Agrega un campo oculto de ‘username’ en tu Typeform
  • Incrusta tu Typeform en tu publicación simplemente compartiendo el enlace. No uses la función de incrustación de Typeform. También necesitarás permitir la URL de Typeform https://forms.typeform.com como incrustación en tu configuración de administrador.