Expand my Community achievements bar.

SOLVED

Is there a way to add (not include) a query string parameter on the URL from the target.

Avatar

Employee

Use case is as follows:

 

Created an activity with the expectation where in 50% of the users see the experience where the video automatically plays when user lands on the page whereas 50% of the users should click on the play button. User looked for the possible ways and found there are 2 ways to to implement auto play, in general and not specifically to target:
1. Add code directly at the page level.
2. To add (not include) parameter in the URL.
 
The question here is - 
Is there a way to add a query string parameter on the URL from the target.
1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi @MayuriK 

 

You can use custom code option in VEC. Add custom javascript to append query parameters in the URL. you can use following JS code.

function insertParam(key, value) {
key = encodeURIComponent(key);
value = encodeURIComponent(value);

// kvp looks like ['key1=value1', 'key2=value2', ...]
var kvp = document.location.search.substr(1).split('&');
let i=0;

for(; i<kvp.length; i++){
if (kvp[i].startsWith(key + '=')) {
let pair = kvp[i].split('=');
pair[1] = value;
kvp[i] = pair.join('=');
break;
}
}

if(i >= kvp.length){
kvp[kvp.length] = [key,value].join('=');
}

// can return this or...
let params = kvp.join('&');

// reload page with new params
document.location.search = params;
}

query.append("test","true");

 

let me know if this works.

 

 

View solution in original post

6 Replies

Avatar

Correct answer by
Employee Advisor

Hi @MayuriK 

 

You can use custom code option in VEC. Add custom javascript to append query parameters in the URL. you can use following JS code.

function insertParam(key, value) {
key = encodeURIComponent(key);
value = encodeURIComponent(value);

// kvp looks like ['key1=value1', 'key2=value2', ...]
var kvp = document.location.search.substr(1).split('&');
let i=0;

for(; i<kvp.length; i++){
if (kvp[i].startsWith(key + '=')) {
let pair = kvp[i].split('=');
pair[1] = value;
kvp[i] = pair.join('=');
break;
}
}

if(i >= kvp.length){
kvp[kvp.length] = [key,value].join('=');
}

// can return this or...
let params = kvp.join('&');

// reload page with new params
document.location.search = params;
}

query.append("test","true");

 

let me know if this works.

 

 

Avatar

Employee

Hi @Gaureshk_Kodag 

 

Thanks for the information. But here we are creating form based activity and trying to create an html offer to add parameter to the URL.

 

How can we do that in case of form based activity. 

 

Current implementation is as follows:

HTML offer code:

setTimeout(()=>{
let videoAutoPlay = document.querySelector('video-js');
videoAutoPlay.player.muted(true);
videoAutoPlay.player.play();
},1000);

 

I am not sure if this is the correct way.

Avatar

Employee Advisor

Hi @MayuriK ,

Just add the code which I shared earlier after your code. It'll work in for based as well.

Also, im not sure if @MihneaD's solution will work for you cause you have your own code to run as well. So using redirect offer might not work here. Please correct me if im wrong @MihneaD 

 

Thanks.

Avatar

Employee Advisor

@MayuriK,

 

If you are still searching for alternatives it would also be possible to accomplish this via a redirect:

ExpA: Default
ExpB: Redirect from url1.com --to--> url1.com/?playnowparameter

The only caveat is you have to test to validate you aren't setting up an infinite redirect loop. If the activity is configured to allow access only from url1.com it should work however if it's configured to allow access from url that contains url1 it will result in an infinite redirect loop. Hope this helps.

 

--Docea

Avatar

Employee

This method cannot be used as there is not one url where we need auto play functionality. There are multiple page URL

Avatar

Employee Advisor

Oh okay. I believe using code which I shared should resolve your problems.