3
As PHP use in the backend, to configure the inline user id just give a echo
of the variable $userId
, for ex:
if (isset($userId)) {
$gacode = "ga('create', 'xxx', 'auto', {'userId': $userId});";
echo sprintf($gacode, $userId);
}
else {
$gacode = "ga('create', 'xxx', 'auto');";
echo sprintf($gacode);
}
But how I want to use the tracking code in a file .js
external (called at the bottom of the page’s HTML .php
), instead of using inline
, that doesn’t work.
The tracking code is basically this:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'XXXXXXX', { 'user_id': 'USER_ID' });
This code is in the file I created gtag_user.js
, I placed below the tagmanager lib (at the end of the page where the variable $userId
is declared).
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXX"></script>
<script src="https://www.site.com/view/js/gtag_user.js"></script>
Where USER_ID
must be equal to the variable $userId
PHP. I made a few attempts with ajax
but it didn’t work either. Is it possible? How?
For me to situate myself? Where is marked with
<script>
is PHP? The code that is not marked is JS? If so it would not be the case to change this seat marking?– Augusto Vasques
@Augusto Vasques Yes, I checked.. already removed. Thanks for telling. This php is in the middle of the JS, to write the js as the user is logged in or not.
– gustavox