Answered

How can I automatically target different forms between dev and production environments?

  • 16 December 2022
  • 5 replies
  • 156 views

Userlevel 1
Badge

I use WordPress and copy my dev to prd environment, which copies the Typeform id from dev to prd too. But I’d like to have two Typeforms each for dev and prd. Is there some clever way to configure the Typeform to use the desired form id depending on the domain or environment? I’m thinking it could be harder where I’d have to write script code to do this on my page.

icon

Best answer by Ted Cohn 16 December 2022, 18:00

View original

5 replies

Userlevel 7
Badge +5

Hi @Ted Cohn Happy Friday! We don’t have this feature in Typeform, so you would need to write a code on your website that can do this. 

@mathio may possibly have some examples of this, but I believe this is very specific to your website and how you’ve built it. 

Userlevel 1
Badge

@Liz Thanks. Are there any code samples ideally for Wordpress that simply insert the current site’s host or URL where the form is presented? I have two websites, a staging site and production site each with different URLs. If I can plug into the hidden source field, my backend can then differentiate where the user is entering data. 

Userlevel 7
Badge +6

@Ted Cohn - this may not be exactly what you want to achieve, but for a couple of clients, when i created the form for the dev environment, we customized the link to a naming convention that was formname-dev and when we duplicated the form for the prod site, we changed the name to -prod … 

your web developers should be able to ‘dynamically’ create-push a variable string to replace the -dev/-prod portion of the url string, based on which instance is being viewed by the user.. 

just a thought.. 

 

des

Userlevel 1
Badge

Ok, I’ve discovered I can simply fetch the current web page’s location using document.location.host and insert the result into the hidden source field. Then my back end can differentiate where the form is filled out and take appropriate action.

Here’s my current embedded HTML script that works for me. It inserts both the currently logged in Wordpress user id and the source. 

<!-- Present Typeform with hidden user_id and source fields -->
<div id="tf"
data-tf-widget="<your form id here>"
data-tf-iframe-props="title=<Your form title>"
data-tf-medium="snippet"
data-tf-hidden="user_id=xxxxx,source=xxxxx"
style="width:100%;height:600px;">
</div>
<script src="//embed.typeform.com/next/embed.js"></script>
<script>
const user_id = document.cookie.match(/portal_user=([^;]+);/)[1]
const host = document.location.host
const div = document.querySelector('#tf')
div.dataset.tfHidden = "user_id=" + user_id + ",source=" + host
</script>

 

Userlevel 7
Badge +5

Ah, thanks for sharing this, @Ted Cohn !! 

Reply