Expand my Community achievements bar.

Profile script visit count incrementation

Avatar

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

Avatar

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?

Avatar

Level 9

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... 

Avatar

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 ?

Avatar

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

Avatar

Level 9

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;

    }

}

Avatar

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..

Avatar

Level 9
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!!

Avatar

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.. 

Avatar

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