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

Example:

  1. I fire a Custom Event with Custom Dimension 1 “Event Target URL” to “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:

show

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

<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 :).

1 Like

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.

         /**
         * 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"]);
            }
        };

@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?

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.

Ah that’s great, dzięks