# Shopify app: set session custom dimensions

**URL:** <https://community.piwik.pro/t/shopify-app-set-session-custom-dimensions/5256>\
**Category:** Data collection and tags\
**Created:** [July 9, 2025, 7:12am UTC](https://community.piwik.pro/t/shopify-app-set-session-custom-dimensions/5256 "2025-07-09T07:12:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Anode\_FR](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/thomas_anode_fr/32/1425_2.png) [@Thomas\_Anode\_FR](https://community.piwik.pro/u/Thomas_Anode_FR)\
**Post date:** [July 9, 2025, 7:12am UTC](https://community.piwik.pro/t/shopify-app-set-session-custom-dimensions/5256/1 "2025-07-09T07:12:02Z")

</div>

Hello,

I have installed the Piwik PRO app on a Shopify shop. Everything works fine, and I understand how to send custom events: [the documentation](https://help.piwik.pro/support/getting-started/shopify-install-piwik-pro-app/) is clear. 🙂

That said, I wonder if it’s possible to define a custom session dimension with this configuration. Perhaps via code injection into the theme’s liquid files, or via a custom pixel?

Or do I have to use the other method, which is [manually installing the tracking code](https://help.piwik.pro/support/getting-started/shopify-install-a-tracking-code/) via a custom pixel?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Thomas\_Anode\_FR](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/thomas_anode_fr/32/1425_2.png) [@Thomas\_Anode\_FR](https://community.piwik.pro/u/Thomas_Anode_FR)\
**Post date:** [July 9, 2025, 7:49am UTC](https://community.piwik.pro/t/shopify-app-set-session-custom-dimensions/5256/2 "2025-07-09T07:49:28Z")

</div>

I think I’ve found a workaround.

I’ve inserted the following code to the `<head>` of the `theme.liquid` file:

```auto
<script>
(function waitForShopifyAnalytics() {
 if (sessionStorage.getItem('pp_custom_dimensions_sent')) return;

  const urlParams = new URLSearchParams(window.location.search);

 const adgroup_id = urlParams.get('adgroup_id');
 const ad_id = urlParams.get('ad_id');
 const meta_platform = urlParams.get('meta_platform');
 const meta_placement = urlParams.get('meta_placement');

  const dimensions = {};
 if (adgroup_id) dimensions[3] = adgroup_id;
 if (ad_id) dimensions[4] = ad_id;
 if (meta_platform) dimensions[5] = meta_platform;
 if (meta_placement) dimensions[6] = meta_placement;

  if (Object.keys(dimensions).length === 0) return;

  const interval = setInterval(() => {
 if (window.Shopify?.analytics?.publish) {
 Shopify.analytics.publish('piwik_pro_trackEvent', {
 category: 'Session Info',
 action: 'Init',
 name: 'Initial session dimensions',
 dimensions: dimensions
 });

      sessionStorage.setItem('pp_custom_dimensions_sent', 'true');
 clearInterval(interval);
 }
  }, 200);
})();
</script>

```

This script captures `adgroup_id`, `ad_id`, `meta_platform`, and `meta_placement` from the URL parameters, and sends them as custom dimensions **once per session** using a custom event (`piwik_pro_trackEvent`).

Since `Shopify.analytics.publish()` may not be available immediately when the page loads, the script uses a polling mechanism (`setInterval`) to wait until it’s ready. Once the event is sent, it stores a flag in `sessionStorage` to prevent multiple sends during the same session.

It’s injected into the `theme.liquid` file (just before `</head>`), so it executes on every page load, but only sends the event once per session — as early as possible.

Session dimensions appear as defined in the tracking debugger:

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/piwikpro/original/2X/3/361d47f6a91eb235d1aeefac82e8db80f8106806.png)

It seems to work, but a custom event is needed, which adds to the hits consumption…

So I’d be interested in another solution if there is one.

Thanks!

---

<div class="post-metadata">

**Author:** ![Thomas\_Anode\_FR](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/thomas_anode_fr/32/1425_2.png) [@Thomas\_Anode\_FR](https://community.piwik.pro/u/Thomas_Anode_FR)\
**Post date:** [July 16, 2025, 10:01am UTC](https://community.piwik.pro/t/shopify-app-set-session-custom-dimensions/5256/3 "2025-07-16T10:01:39Z")

</div>

Quick update!

I initially injected the script above into `theme.liquid` to push custom session dimensions using `Shopify.analytics.publish()`. However, I encountered a race condition: the event was being sent **before** the official Piwik PRO page view (triggered by the Shopify Piwik PRO app), which prevented proper attribution and caused data inconsistencies.

To solve this, I migrated to a **custom pixel** that waits for the `analytics.subscribe('page_viewed')` trigger. This guarantees that dimensions are sent **after** the official page view, ensuring correct session linkage. It also makes the setup more robust and easier to control.

Here is the custom pixel:

```auto
analytics.subscribe('page_viewed', async (event) => {
  if (sessionStorage.getItem('pp_custom_dimensions_sent')) return;

  const urlParams = new URLSearchParams(window.location.search);

  const adgroup_id = urlParams.get('adgroup_id');
  const ad_id = urlParams.get('ad_id');
  const meta_platform = urlParams.get('meta_platform');
  const meta_placement = urlParams.get('meta_placement');

  const dimensions = {};
  if (adgroup_id) dimensions[3] = adgroup_id;
  if (ad_id) dimensions[4] = ad_id;
  if (meta_platform) dimensions[5] = meta_platform;
  if (meta_placement) dimensions[6] = meta_placement;

  if (Object.keys(dimensions).length === 0) return;

  const bodyParams = new URLSearchParams({
    idsite: 'x',
    rec: '1',
    e_c: 'Session Info',
    e_a: 'Init',
    e_n: 'Initial session dimensions',
    url: window.location.href,
    action_name: document.title,
    rand: Math.random().toString().slice(2)
  });

  for (const [key, value] of Object.entries(dimensions)) {
    bodyParams.append(`dimension${key}`, value);
  }

  fetch('https://x.piwik.pro/ppms.php', {
    method: 'POST',
    mode: 'no-cors',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: bodyParams.toString()
  });

  console.log('📡 Piwik PRO - dimensions sent via POST/ppms', dimensions);
  sessionStorage.setItem('pp_custom_dimensions_sent', 'true');
});

```
