# Custom Event variable like in GTM

**URL:** <https://community.piwik.pro/t/custom-event-variable-like-in-gtm/3590>\
**Category:** Data collection and tags\
**Created:** [February 5, 2024, 11:38am UTC](https://community.piwik.pro/t/custom-event-variable-like-in-gtm/3590 "2024-02-05T11:38:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://community.piwik.pro/u/Martin)\
**Post date:** [February 5, 2024, 11:38am UTC](https://community.piwik.pro/t/custom-event-variable-like-in-gtm/3590/1 "2024-02-05T11:38:08Z")

</div>

Hi all,

for my SPA setting I used the custom event variable in GTM.  
Is there a possibility to have the same variable?

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

My code looks like this:

function(){  
var vpu = “Fallbackrechner”;  
if ( {{Custom Event}}.indexOf(“rlv”) == 0 ){  
vpu = “Angebotsrechner Risikoleben”;  
} else if ( {{Custom Event}}.indexOf(“pkw”) == 0 ){  
vpu = “Angebotsrechner PKW”; etc.

I am reading the events name that is pushed and extract the technical name like  
indexOf(“step1.load”)

For now the console says: Cannot read properties of undefined (reading ‘indexOf’)  
My guess is that is because the Variable is not set

Regards Martin

---

<div class="post-metadata">

**Author:** ![kamilb](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/kamilb/32/309_2.png) [@kamilb](https://community.piwik.pro/u/kamilb)\
**Post date:** [February 5, 2024, 1:56pm UTC](https://community.piwik.pro/t/custom-event-variable-like-in-gtm/3590/2 "2024-02-05T13:56:11Z")

</div>

Hi @Martin,

Please try using {{ Event }} instead of {{Custom Event}}.  
{{ Event }} is a built in variable which returns the value of the event key in the dataLayer.

Have a great day! 🙂

---

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://community.piwik.pro/u/Martin)\
**Post date:** [February 5, 2024, 3:14pm UTC](https://community.piwik.pro/t/custom-event-variable-like-in-gtm/3590/3 "2024-02-05T15:14:04Z")

</div>

Hi @kamilb

that worked. But I still get a console error Cannot read properties of undefined (reading ‘indexOf’).  
any ideas?  
it seems on the initial load that the variable is undefined. The error accures just once and the rest is as it should be

---

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://community.piwik.pro/u/Martin)\
**Post date:** [February 5, 2024, 3:17pm UTC](https://community.piwik.pro/t/custom-event-variable-like-in-gtm/3590/4 "2024-02-05T15:17:20Z")

</div>

Okay … Im getting an empty event where everything is undefined. Thats interessting

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/piwikpro/original/2X/0/048708fb046ab8a59ee713ad6dd392e01bab1fd6.png)

---

<div class="post-metadata">

**Author:** ![kamilb](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/kamilb/32/309_2.png) [@kamilb](https://community.piwik.pro/u/kamilb)\
**Post date:** [February 5, 2024, 3:42pm UTC](https://community.piwik.pro/t/custom-event-variable-like-in-gtm/3590/5 "2024-02-05T15:42:44Z")

</div>

Hi @Martin,

> [@Martin](#):
>
> that worked. But I still get a console error Cannot read properties of undefined (reading ‘indexOf’).  
> any ideas?

Just to make sure, this code is in a tag or a variable? -  
function(){  
var vpu = “Fallbackrechner”;  
if ( {{Custom Event}}.indexOf(“rlv”) == 0 ){  
vpu = “Angebotsrechner Risikoleben”;  
} else if ( {{Custom Event}}.indexOf(“pkw”) == 0 ){  
vpu = “Angebotsrechner PKW”;

If it is inside of a tag, what is the trigger?

> [@Martin](#):
>
> Okay … Im getting an empty event where everything is undefined. Thats interessting

Can you show me what you see in the dataLayer?

---

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://community.piwik.pro/u/Martin)\
**Post date:** [February 5, 2024, 3:56pm UTC](https://community.piwik.pro/t/custom-event-variable-like-in-gtm/3590/6 "2024-02-05T15:56:36Z")

</div>

Seems like i fixed it.

The code is in a variable . I changed Custom Event to Event.

I think the initial DataLayer pushes empty variables. Now that I know this case I set {{Event}} to be “default” if its not defined.

The initial empty push is just “[]” . Maybe some old stuff from the current Google setting.

Everything runs fine now, thanks for the help.
