Expand my Community achievements bar.

How to test getVisitNum

Avatar

Level 3

Need to store getVisitNum in an event for this i followed below steps

 

1. copied getVisitNum plug-in code and pasted in adobe analytics extension -> custom code editor

2. created new data element (Session count)--> custom code --> add code return getVisitNum()

3. in Rule --> event8 = Session count

 

testing - 

 

event8 = undefined

 

guid me how to impliment and test getVisitNum

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

14 Replies

Avatar

Level 9

Hi @reddy_y-11 

are you using Adobe Launch? If so, you do not need to copy the plugin code, but can use "Common Analytics Plugins" extension. This will make sure to always keep the plugin versions up to date (as long as you keep the extension up to date).

bjoern__koth_0-1724316162414.png

Note: You must initialize the right plugin in a rule that loads as soon as the library is loaded

bjoern__koth_1-1724316219850.png

 

After that, you will be able to get the visit number through the data element

bjoern__koth_2-1724316270765.png

 

Hope that helps

 

 

Avatar

Level 3

Thanks! @bjoern__koth  for the reply

 

Note - not using the extension.

 

Adobe launch - Implimented through custom code in adobe analytics.

 

How can impliment?

 

 

Avatar

Level 9

is your analytics / AppMeasurement "s" instance globally accessible?

bjoern__koth_0-1724318335184.png

 

The problem you are likely facing is that throwing the function in the Analytics custom code will not necessarily attach it to the "s" object.

 

https://experienceleague.adobe.com/en/docs/analytics/implementation/vars/plugins/getvisitnum

 

If you take the code from there, you can see

bjoern__koth_1-1724318416238.png

 

Since you are trying to access the function from a custom code javascript data element, this will be unreachable unless

a) you define it as function on the "s" object OR

 

 

s.getVisitNum = function(rp,erp){...

 

 

and call is through s.getVisitNum()

b) define it as window scoped function (this case does not need a globally accesible "s" tracker object)

 

window.getVisitNum = function(rp,erp){...

 

and call it through window.getVisitNum()

 

If you really do not want to use the extension (which I would recommend), I would likely go for option b)

Avatar

Level 9

What keeps you from using the extension

It will be a no-code solution

Avatar

Community Advisor

I don't believe that Data Elements can interact with the s object...

 

I believe that both the extension code, and the setting of your event must be done in the custom code area using the "Do Plugins".

 

 

s.usePlugins = true;
function s_doPlugins(s) {
   // typically this is collected as a dimension:
   s.prop1 = getVisitNum();

   // but if you need to track as a numeric event, which I think is not going to work 
   quite right, since your event count will just keep grow exponentially, you would 
   have to append it to the end of your existing event list, so you don't overwrite 
   it....
   s.events = s.events ? s.events + ",event8=" + getVisitNum() : "event8=" + getVisitNum();
}


/******************************************* BEGIN CODE TO DEPLOY *******************************************/
/* Adobe Consulting Plugin: getVisitNum v4.2 */
function getVisitNum(rp,erp){var a=rp,l=erp;function m(c){return isNaN(c)?!1:(parseFloat(c)|0)===parseFloat(c)}function n(c){var b=new Date,e=isNaN(c)?0:Math.floor(c);b.setHours(23);b.setMinutes(59);b.setSeconds(59);"w"===c&&(e=6-b.getDay());if("m"===c){e=b.getMonth()+1;var a=b.getFullYear();e=(new Date(a?a:1970,e?e:1,0)).getDate()-b.getDate()}b.setDate(b.getDate()+e);"y"===c&&(b.setMonth(11),b.setDate(31));return b}if("-v"===a)return{plugin:"getVisitNum",version:"4.2"};var f=function(){if("undefined"!==typeof window.s_c_il)for(var c=0,b;c<window.s_c_il.length;c++)if(b=window.s_c_il[c],b._c&&"s_c"===b._c)return b}();"undefined"!==typeof f&&(f.contextData.getVisitNum="4.2");window.cookieWrite=window.cookieWrite||function(c,b,e){if("string"===typeof c){var a=window.location.hostname,d=window.location.hostname.split(".").length-1;if(a&&!/^[0-9.]+$/.test(a)){d=2<d?d:2;var h=a.lastIndexOf(".");if(0<=h){for(;0<=h&&1<d;)h=a.lastIndexOf(".",h-1),d--;h=0<h?a.substring(h):a}}g=h;b="undefined"!==typeof b?""+b:"";if(e||""===b)if(""===b&&(e=-60),"number"===typeof e){var f=new Date;f.setTime(f.getTime()+6E4*e)}else f=e;return c&&(document.cookie=encodeURIComponent(c)+"="+encodeURIComponent(b)+"; path=/;"+(e?" expires="+f.toUTCString()+";":"")+(g?" domain="+g+";":""),"undefined"!==typeof window.cookieRead)?window.cookieRead(c)===b:!1}};window.cookieRead=window.cookieRead||function(c){if("string"===typeof c)c=encodeURIComponent(c);else return"";var b=" "+document.cookie,a=b.indexOf(" "+c+"="),d=0>a?a:b.indexOf(";",a);return(c=0>a?"":decodeURIComponent(b.substring(a+2+c.length,0>d?b.length:d)))?c:""};a=a?a:365;l="undefined"!==typeof l?!!l:m(a)?!0:!1;var p=(new Date).getTime();f=n(a);if(window.cookieRead("s_vnc"+a))var d=window.cookieRead("s_vnc"+a).split("&vn="),k=d[1];if(window.cookieRead("s_ivc"))return k?(window.cookieWrite("s_ivc",!0,30),k):"unknown visit number";if("undefined"!==typeof k)return k++,d=l&&m(a)?p+864E5*a:d[0],f.setTime(d),window.cookieWrite("s_vnc"+a,d+"&vn="+k,f),window.cookieWrite("s_ivc",!0,30),k;d=m(a)?p+864E5*a:n(a).getTime();window.cookieWrite("s_vnc"+a,d+"&vn=1",f);window.cookieWrite("s_ivc",!0,30);return"1"};
/******************************************** END CODE TO DEPLOY ********************************************/

(Note: the event code checks if there is anything in events, if so it will append event8 to the end of that list, if there is nothing, then it will set s.events (without the separation comma)

 

Keep in mind, that this won't get the user's full Visit Number, this is merely code that sets a cookie and tracks the visits as of that cookie... if the cookie is removed, the values will reset... and right now, JS cookies like this tend to be flaky with all the "anti-cookie" stuff that browsers are doing... particularly JS set cookies vs cookies set by a server (which still have a little more authority....).

Avatar

Community Advisor

You can also access the s object within the Set Variables action, so if you don't want to use Do Plugins, you can set your event in your Rule, in the custom code area of your AA Set Variables....

Avatar

Level 9

It really depends on how it is approached and where you place the code.
I would definitely go for the extension and included data element type, then there is no discussion

Avatar

Community Advisor

Yeah, and if you aren't a coder, this will make things a lot simpler

Avatar

Level 9

Have you set the visibility of the s object to global in the extension? (one of my first screenshots)

Avatar

Level 9

Feels like we're turning in circles

What keeps you from using the extension with the plugin? It also has a bunch of other useful extensions that might be interesting for you. No need for copy+pasting code.

 

i would at least give it a try. That way, you can also keep the s object hidden

Avatar

Community Advisor

Without seeing the implementation or where the issue is I am not sure I can help diagnose... 

 

Are you testing with Incognito mode? If so, you won't have a pre-existing cookie to read from.. could that be the issue?

 

Can you first check if a cookie is being set by the plugin code?

 

Also, I agree with Bjorn... using the extension would be a lot easier.... is there a reason you are avoiding that?