Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Profile script visit count incrementation

Tienzen
Level 2
Level 2

Hi,

 

I'm trying the code below in a profile script but it is never incremented.

 

if(user.sessionId!=user.getLocal('lastSessionId')) {
  user.setLocal('lastSessionId', user.sessionId);
  return (user.get('visitCount') || 0) + 1;
}

 

For test it faster, i override the sessionIdLifetime at 30 sec.

The sessionId change works fine.

But visitCount is always blocked at 1.

 

Do you know why ?

 

9 Replies
Tienzen
Level 2
Level 2

It seems the value of user.getLocal('lastSessionId') always start in the IF condition with "null"

 

in the mboxTrace this variable is identified with user.123456789.lastSessionId

 

I don't know how to get the right lastSessionId stored in the IFcondition?

Pawan_Gupta_
Community Advisor
Community Advisor

did you set 'visitCount' first before getting the value?? because first time it will start with 0 but before return you have to set it to user or somewhere in local storage so that next time 'visitCount' will return saved value rather going to 0 you can read for user.

 

also refer - https://experienceleague.adobe.com/docs/target/using/audiences/visitor-profiles/variables-profiles-p... 

Tienzen
Level 2
Level 2

Hi, i tried to store the value in local storage and it's work fine when user is in the same session.

But when a new session starts, the storage is deleted and comes back to 0...

Have you got an idea ?

LordOfTheRings
Level 7
Level 7
Hi , can you try this to increment the profile script visit count >>>> var count = user.get("tgtvisitCount") || 0; count+=1; return count;
Pawan_Gupta_
Community Advisor
Community Advisor

why don't you try the combination of these three to achieve your requirement (something as below, just coded roughly)

 

if(user.isFirstSession){

    user.setLocal("lastsession",user.sessionId);

   return (user.get('visitCount') || 0) + 1;

}else if(user.isNewSession){

    if(user.getLocal("lastsession")!=user.sessionId){

           user.setLocal("lastsession",user.sessionId);

           return (user.get('visitCount') || 0) + 1;

    }

}

Tienzen
Level 2
Level 2
Thanks for your help. Unfortunatly, i didn't see improvement in my test. Maybe i have got a problem with the way i'm doing my test and the result expected. Because all visit count sample script i found on the web or yours, seems to work in theory..
Pawan_Gupta_
Community Advisor
Community Advisor
Did you check the setting call for 'visitCount' because you are using user.get('visitCount') so this value should be save before you get it. i mentioned same in my first reply!!
Tienzen
Level 2
Level 2

I think i don't really know how to check the setting...

For me the user.get('visitCount') is set by the "return" statement and is reachable from the second time.

 

If i put just this line in the profile script, the incrementation will work :

return (user.get('visitCount') || 0) + 1;

 

But once i add session instruction the incrementation is down.. 

Tienzen
Level 2
Level 2
Thx. The incrementation works but this does not solve my problem, because it increment at each page loading, and i only want to increment just in case a new session starts