# SPA环境中的PV重复计测防止设置

## SPA环境中的PV重复计测防止设置

## **现象**

在SPA中，即使页面改变，Ptengine基础标签也不会重新加载，导致PV无法正确计测，因此需要利用Ptengine的setPVTag手动发起PV计测。但是，在页面刷新或其他页面更新的情况下，基础标签和设置的setPVTag会同时发起，导致PV被重复计测的情况发生。

为了避免上述重复发起的现象，需要采取以下方法进行处理。

## **逻辑**

当基础标签或setPVTag发起时，利用变量pt\_spa\_trigger\_mark将地址栏的URL和发起时间记录在Cookie中。此外，在下次页面更新时，根据Cookie中记录的内容判断是否发起setPVTag，并进行控制。

## **GTM中的设置方法**

1. 按以下方式修改基础标签，并在GTM中替换已设置的标签。"xxxxxxxx"是项目id，每个项目都不同，请注意。

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

2. 按以下方式修改setPVTag，并在GTM中替换已设置的setPVTag。设置完成后保存并发布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>

```

## **手动配置**

以下标签也可以手动设置，但设置时请联系技术人员。

1. 按以下方式修改基础标签并将其粘贴到网站上。"xxxxxxxx"是项目id，每个项目都不同，请注意。

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

2. 按以下方式修改setPVTag，并直接添加到网站逻辑中，使其在每次页面更新时发起。

```
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/cn/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.
