Web Personalization (RTP) dynamically updating text

Aaron_Krug
Level 2

Web Personalization (RTP) dynamically updating text

Is there a way in RTP to dynamically update the text in a web campaign with the name of the state that the visitor is currently in? I know I could create 50 different segments and campaigns for each state but was hoping not to have to do that.

4 REPLIES 4
Anonymous
Not applicable

Re: Web Personalization (RTP) dynamically updating text

See: User Context API

You can use rtp.userContext.location.state to retrieve the state name and then develop a javascript that adds it into your campaign text/link.

<div id="state-name">Your State</div>

<script>

$('#state-name').text(rtp.userContext.location.state);

</script>

Aaron_Krug
Level 2

Re: Web Personalization (RTP) dynamically updating text

Thanks for the response but I am not able to get it to work. In the console I am just getting

Uncaught TypeError: Cannot read property 'location' of undefined.

I have also had User Context API enabled for my account.

Am I missing something?

Yanir_Calisar2
Level 3

Re: Web Personalization (RTP) dynamically updating text

Could you send me a link to your website? I'll take a look.

Basically you need to wrap this script with some function that waits for the rtp.userContext object to be ready and only then replace the content.

Also, it's recommended to validate that  rtp.userContext.location.state is not undefined (rtp.userContext.location.state != undefined).

This to avoid an error when people are visiting from countries that don't have states.

SanfordWhiteman
Level 10 - Community Moderator

Re: Web Personalization (RTP) dynamically updating text

Think you want

  rtp('userContextReady', function() {

    var state = rtp.userContext.location.state;

    if ( state !== undefined) {

      // do something with state

    }

  });

(Apparently Jive's Syntax Highlighter is broken at the moment, which is just wonderful.)