Preventing Duplicate PV Measurement in SPA Environments

Preventing Duplicate PV Measurement in SPA Environments

Phenomenon

In SPA environments, even when the page changes, the Ptengine base tag is not reloaded, so PV is not measured correctly. Therefore, it's necessary to manually fire PV using Ptengine's setPVTag to measure it properly. However, in cases such as page refresh or other situations where the page is updated, the base tag and the configured setPVTag fire simultaneously, resulting in duplicate PV measurements.

To avoid the above duplicate firing, it's necessary to handle it using the following method.

Logic

When the base tag or setPVTag fires, the cookie stores the URL from the address bar and the firing timing using the variable pt_spa_trigger_mark. Furthermore, on the next page update, based on the contents recorded in the cookie, we control whether to fire setPVTag or not.

Configuration Method in GTM

  1. Modify the base tag as follows and replace the tag configured in GTM. Please note that "xxxxxxxx" is the project ID, which differs for each project.

<script src="https://js.ptengine.jp/xxxxxxxx.js">
document.cookie="pt_spa_trigger_mark="+location.href+"ptengine"+new Date().getTime();
</script>
  1. Modify setPVTag as follows and replace the setPVTag configured in GTM. After configuration is complete, save and publish the GTM.

<script>
try{
    function getCookie(key){
      var str = document.cookie;
      var arr = str.split(";");
        for(var i=0;i<arr.length;i++){
            var data = arr[i].split("=");
              if(data[0].trim() === key){
                    return data[1];
               }
        }
    }
    if(getCookie("pt_spa_trigger_mark")){
        var pt_mark = getCookie("pt_spa_trigger_mark").split("ptengine");
        if(pt_mark[0] === location.href && (new Date().getTime() - parseInt(pt_mark[1])) > 3500){
            window._pt_sp_2 && window._pt_sp_2.push('setPVTag,'+ location.href +',replace');
            document.cookie = "pt_spa_trigger_mark="+location.href+"ptengine"+new Date().getTime();
        }else if(pt_mark[0] != location.href && (new Date().getTime() - parseInt(pt_mark[1])) > 3000){
            window._pt_sp_2 && window._pt_sp_2.push('setPVTag,'+ location.href +',replace');
            document.cookie = "pt_spa_trigger_mark="+location.href+"ptengine"+new Date().getTime();
        }
    }else{console.log("not found pt_trigger")}
}catch(e){console.log(e)}
</script>

Manual Configuration

The following tag can also be configured manually, but please request a technical person to configure it when setting it.

  1. Modify the base tag as follows and paste it on the site. Please note that "xxxxxxxx" is the project ID, which differs for each project.

  1. Modify setPVTag as follows and add it directly to the site logic so that it fires every time the page is updated.

最終更新