# Clear Variables after request / Avoid automatic persistence of Custom Dimensions etc. into next Hit

**URL:** <https://community.piwik.pro/t/clear-variables-after-request-avoid-automatic-persistence-of-custom-dimensions-etc-into-next-hit/1610>\
**Category:** Data collection and tags\
**Created:** [February 4, 2023, 11:48am UTC](https://community.piwik.pro/t/clear-variables-after-request-avoid-automatic-persistence-of-custom-dimensions-etc-into-next-hit/1610 "2023-02-04T11:48:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![loldenburg](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/loldenburg/32/965_2.png) [@loldenburg](https://community.piwik.pro/u/loldenburg)\
**Post date:** [February 4, 2023, 11:48am UTC](https://community.piwik.pro/t/clear-variables-after-request-avoid-automatic-persistence-of-custom-dimensions-etc-into-next-hit/1610/1 "2023-02-04T11:48:34Z")

</div>

Example:

1. I fire a Custom Event with Custom Dimension 1 “Event Target URL” to “[http://theurl](http://theurl)”.
2. I then fire another Custom Event that does not have anything to do with a URL, so Custom Dimension 1 should NOT be set.  
However, by default, Piwik Pro persists Custom Dimensions into the second Event if I don’t explicitly DELETE them.

This leads to problems, especially in Single-Page Apps. I thus usually prefer setups where the logic what persists is in the Tag Management System and consistent across tools. Other tools like Adobe Analytics offer a “clearVars()” method which can simply be called after each Hit and all variables (e.g. custom dimensions) are reset. Tealium offers a similar option for Google Analytics.

I know that I can run “\_paq.push([“deleteCustomDimension”, 1]);”, but that gets a bit tedious with dozens of Custom Dimensions. So I wonder if I am missing anything here in the API documentation:

> **[API](https://developers.piwik.pro/en/latest/data_collection/web/javascript_tracking_client/api.html#custom-dimensions)**
>
> The following API allows the user to: track page views, track visits on multiple domains and subdomains, track e-commerce events (successful orders, cart changes, product and category views), track...

show

---

<div class="post-metadata">

**Author:** ![anthonybartczak](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/anthonybartczak/32/2430_2.png) [@anthonybartczak](https://community.piwik.pro/u/anthonybartczak)\
**Post date:** [February 6, 2023, 1:21pm UTC](https://community.piwik.pro/t/clear-variables-after-request-avoid-automatic-persistence-of-custom-dimensions-etc-into-next-hit/1610/2 "2023-02-06T13:21:18Z")

</div>

Hi Lukas!

As of now it is not possible to clean all of the custom dimensions (CDs) on each hit/request with a single function. On the other hand, it is possible to get the list of used custom dimensions, iterate through the list and delete the values.

I have used [setCustomRequestProcessing](https://developers.piwik.pro/en/latest/data_collection/web/javascript_tracking_client/api.html#setCustomRequestProcessing) to catch the request and retrieve the query string before it’s sent.

Below you can find a WIP piece of code that represents what I’m talking about. Ideally, this should be triggered on each page view and prioritized before all tags.

```auto
<script>
  var _paq = _paq || [];
  function getDimensionIDArray(params) {
    var dimensionIDs = [];
    params.split("&").forEach(function(p) {
        if (p.includes("dimension")) {
            dimensionIDs.push(p.split('dimension').pop().split('=')[0]);
        }
    });
    return dimensionIDs;
  }
  _paq.push(['setCustomRequestProcessing', function(request) {
    var IDs = getDimensionIDArray(request);
  	IDs.forEach(function(id) {
      _paq.push(["deleteCustomDimension", id]);
    });
    return request;
  }])
</script>

```

Do let me know whether this solution is something you were looking for :).

---

<div class="post-metadata">

**Author:** ![loldenburg](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/loldenburg/32/965_2.png) [@loldenburg](https://community.piwik.pro/u/loldenburg)\
**Post date:** [February 6, 2023, 5:46pm UTC](https://community.piwik.pro/t/clear-variables-after-request-avoid-automatic-persistence-of-custom-dimensions-etc-into-next-hit/1610/3 "2023-02-06T17:46:03Z")

</div>

Thanks. It is very useful to know that this method is available, I didn’t know it yet, thank you!  
I am doing sth similar now, unsetting the underlying custom dimensions based on the custom dimension dictionary (map) that we provide to our helper function for Piwik Hits anyway.

```auto
         /**
         * piwikSendHit: sends a hit to piwik and sets or unsets custom dimensions
         * @param {string} hitType - "event, "virtualPageview" or other - anything else will be treated as a "pageview"
         * @param {object} customDimensionMap - a js object (map-like key-value pairs) of custom dimensions to be set for the hit, e.g. {"1": "stringifiedValueforCD1", "2": "stringifiedValueforCD2"}
         * @param {object} [params] - parameters:
         * for "event" Hits: eventCategory, eventSubcategory OR eventAction, eventLabel, eventValue are expected
         * for "virtualPageview" Hits: pageTitle, pageUrl are expected
         * @module piwikSendHit
         */
        var piwikSendHit = function (hitType, customDimensionMap, params) {
            for (var key in customDimensionMap) {
                if (customDimensionMap[key] && customDimensionMap[key] !== "undefined") {
                    _paq.push(['setCustomDimensionValue', key, customDimensionMap[key]]);
                } else {
                    _paq.push(['deleteCustomDimension', key]);
                }
            }
            if (hitType === "event") {
                // category, action, name, value
                _paq.push(['trackEvent', params.eventCategory, (params.eventSubcategory || params.eventAction), params.eventLabel, params.eventValue]);
            } else if (hitType === "virtualPageView") {
                _paq.push(['setCustomUrl', params.pageUrl]);
                _paq.push(["trackPageView", params.pageTitle]);
            } else {
                _paq.push(["trackPageView"]);
            }
        };

```

---

<div class="post-metadata">

**Author:** ![loldenburg](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/loldenburg/32/965_2.png) [@loldenburg](https://community.piwik.pro/u/loldenburg)\
**Post date:** [December 27, 2023, 11:41am UTC](https://community.piwik.pro/t/clear-variables-after-request-avoid-automatic-persistence-of-custom-dimensions-etc-into-next-hit/1610/4 "2023-12-27T11:41:31Z")

</div>

@anthonybartczak this method runs BEFORE the request is actually executed, so I cannot use that to clear out dimensions AFTER the request (which is what I would need), right?

---

<div class="post-metadata">

**Author:** ![anthonybartczak](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/anthonybartczak/32/2430_2.png) [@anthonybartczak](https://community.piwik.pro/u/anthonybartczak)\
**Post date:** [December 27, 2023, 11:48am UTC](https://community.piwik.pro/t/clear-variables-after-request-avoid-automatic-persistence-of-custom-dimensions-etc-into-next-hit/1610/5 "2023-12-27T11:48:01Z")

</div>

It runs before the request is sent (`return request;`) but my code doesnt modify the request content. It only checks the content and removes saved custom dimension values from JavaScript Tracking Client.

---

<div class="post-metadata">

**Author:** ![loldenburg](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/loldenburg/32/965_2.png) [@loldenburg](https://community.piwik.pro/u/loldenburg)\
**Post date:** [December 27, 2023, 12:48pm UTC](https://community.piwik.pro/t/clear-variables-after-request-avoid-automatic-persistence-of-custom-dimensions-etc-into-next-hit/1610/6 "2023-12-27T12:48:13Z")

</div>

Ah that’s great, dzięks
