# 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>
```

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

```
<script src="https://js.ptengine.jp/xxxxxxxx.js">
document.cookie="pt_spa_trigger_mark="+location.href+"ptengine"+new Date().getTime();
</script>
```

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

```
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)}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://helps.ptengine.com/en/faq/tag-and-domain/spa-tracking/spa-pv-duplicate.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
